Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

(Payload V2) Multi-tenant Payload auth cookie not sent to NextJS frontend

default discord avatar
markatomniux11 months ago
8

I have a multi-tenant payload app along with a NextJS frontend.



When I perform a log-in on my NextJS frontend, no cookie is visible from my browser's cookies window. I log in as a guest account to access my frontend which is behind a password protection page. I know that the log-in is successful because the request is a 200 and I have access to my frontend. However, no cookie gets generated in my app.



The log in request is being made using NextJS server action



I have no csrf settings in my Payload instance, and CORS is set to accept all endpoints as my frontend is multi-tenant. My NextJS frontend is running on a different domain to my Payload backend



Is there something I need to do on the payload side to allow my cookie to become available for my user?



For reference, this is what my fetch request looks like



'use server'

const res = await fetch(
            `${process.env.NEXT_PUBLIC_SERVER_URL}/api/v1/users/login`,
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      password,
      email,
    }),
  }).then((res) => res.json())
    .catch((err) => createServerErrorResponse(undefined));

if (res.errors) {
  if (res.errors[0].message.includes('The email or password provided is incorrect')) {
      return createServerErrorResponse('Incorrect password');
  }
  return createServerErrorResponse(undefined);
} else {
  console.log('logging in');

  draftMode().enable();
  redirect(`/`);
}
  • default discord avatar
    paulpopus11 months ago
    The log in request is being made using NextJS server components

    whats the setup here exactly? my understanding is that server components cannot set browser cookies, only read them



    a server action on the other hand will

  • default discord avatar
    markatomniux11 months ago

    ah, server action is the correct phrase

  • default discord avatar
    paulpopus11 months ago
    My NextJS frontend is running on a different domain to my Payload backend


    this might be your issue then



    cookies are only automatically set per domain

  • default discord avatar
    markatomniux11 months ago

    Is there maybe a way to change the cookie's cross-site without implementing custom middleware?

  • default discord avatar
    paulpopus11 months ago

    honestly? not easily if you want it dynamic

    https://payloadcms.com/docs/authentication/config


    you can set it here for

    cookies


    another way might be for you to setup a proxy endpoint in nextjs



    so frontend speaks to proxy, proxy speaks to server



    if you need a more dynamic approach...proxy logins gets the token, sets the auth cookie header

  • default discord avatar
    markatomniux11 months ago

    hmm, the proxy would need the same domain as the one Payload is currently using



    or a subdomain of



    but then would that work? Would that cookie be allowed to be passed to the frontend via the proxy

  • default discord avatar
    paulpopus11 months ago

    you have your proxy read your returned token and manually set it



    maybe the express middleware way is cleaner though

  • default discord avatar
    markatomniux11 months ago

    thank god for you Paul 🙏



    manually setting it was the missing piece

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.