0

I'm running into an error accessing the "/checkout" page using "www.example.com" but it works on "example.com".

The user is being returned as null probably because here, I'm fetching the user from "example.com".

const meUserReq = await fetch(`${process.env.NEXT_PUBLIC_SERVER_URL}/api/users/me`, {
    headers: {
      Authorization: `JWT ${token}`,
    },
  })

Shouldn't "www.example.com "also work since I have included it on cors & csrf?

I tried adding "www.example.com" as a second URL like so, but it didn't work.

 const meUserReq = await fetch(`${process.env.NEXT_PUBLIC_SERVER_URL}/api/users/me` && "https://www.example.com", {
   headers: {
     Authorization: `JWT ${token}`,
   },
 })

1 Answer 1

0

CSRF only applies to cookie-based tokens. If you want, instead of attaching a JWT, you can "include" your token along with your fetch request if you a cookie is present on your frontend.

You also need to include the content-type in your request as per the Payload docs - https://payloadcms.com/docs/rest-api/overview

try {
  const req = await fetch('{process.env.NEXT_PUBLIC_SERVER_URL}/api/users/me', {
    credentials: "include",
    headers: {
      "Content-Type": "application/json",
    },
  })
  const data = await req.json()
} catch (err) {
  console.log(err)
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.