Back

[SOLVED] email password session doesn't work in SSR

  • 0
  • Accounts
  • Web
Drake
6 Oct, 2023, 18:44

sounds like client isn't sending the cookie? is it set?

TL;DR
Title: [SOLVED] Email password session doesn't work in SSR TL;DR: The issue was resolved by removing the `domain` option from the code that sets the cookies. The middleware did not have access to the Appwrite cookies, and removing the `domain` option fixed the problem. Solution: Remove the `domain` option from the code that sets the cookies.
manazo
6 Oct, 2023, 18:46

didn't get your qs. what is set?

Drake
6 Oct, 2023, 18:47

The cookie

manazo
6 Oct, 2023, 18:49

yes it is. here's the code that set's the cookie

TypeScript
  const cookieStore = cookies();

  for (const cookie of cookiesParsed) {
    cookieStore.set(cookie.name, cookie.value, {
      domain: cookie.domain,
      secure: cookie.secure,
      sameSite: cookie.sameSite as any,
      path: cookie.path,
      maxAge: cookie.maxAge,
      httpOnly: cookie.httpOnly,
      expires: cookie.expires,
    });
  }
manazo
6 Oct, 2023, 18:51

another thing I just noticed is that I can see all the appwrite cookies from any other route, but can't from middleware. that's weird

Drake
6 Oct, 2023, 18:57

How are you extracting the cookie?

manazo
6 Oct, 2023, 18:58

a simple example of getting all the cookies

TypeScript
    const { cookies } = await import("next/headers");
    const cookieStore = cookies();
    cookieStore.getAll().forEach((cookie) => {
      console.log(cookie);
    });
Drake
6 Oct, 2023, 18:58

What's your middleware code?

manazo
6 Oct, 2023, 19:00

just a simple check for auth status

TypeScript
export async function middleware(request: NextRequest) {
  const currentUser = await getCurrentUser();
  const path = request.nextUrl.pathname;

  /*
    If the user is not logged in
    and is trying to access an authenticated route,
    redirect to the home page
  */
  if (!currentUser && AUTH_ROUTES.includes(path)) {
    return NextResponse.redirect(new URL("/login", request.url));
  }

  /*
    If the user is logged in
    and is trying to access an unauthenticated route,
    redirect to dashboard
    */
  if (currentUser && UNAUTH_ROUTES.includes(path)) {
    return NextResponse.redirect(new URL("/dashboard", request.url));
  }
}
Drake
6 Oct, 2023, 19:00

Try checking request.cookies

manazo
6 Oct, 2023, 19:01

same thing. only the test cookies are there

Drake
6 Oct, 2023, 19:01

It seems it's not being sent by the client then

manazo
6 Oct, 2023, 19:04

well well well. ig you were right in the text u removed some min ago. the middleware doesn't have access to the appwrite cookies. i just tried removing the domain option from the code that sets my cookies and it started working magically

Drake
6 Oct, 2023, 19:05

what's the domain of the cookie compared to your nextjs domain?

manazo
6 Oct, 2023, 19:05

tho i'm still confused, why domain was preventing it from being read by middleware, while all other routes were reading it successfully

manazo
6 Oct, 2023, 19:06

cloud.appwrite.io

Drake
6 Oct, 2023, 19:07

the nextjs middleware DOES have access to cookies...the domain is something that restricts when cookies are sent by the client

Drake
6 Oct, 2023, 19:07

that's not good. that doesn't work. you need to use a custom domain where your endpoint is a subdomain of your nextjs

Drake
6 Oct, 2023, 19:08

and then the cookie domain should be set for your nextjs app

manazo
6 Oct, 2023, 19:10

but I'm using the cloud version of appwrite and not really thinking about hosting it on a custom domain. if i just don't pass the option domain, will it be insecure?

manazo
6 Oct, 2023, 19:11

then why the other routes were allowed to read it? cuz afaik, middleware and any other routes are running from the same host

Drake
6 Oct, 2023, 19:34

you might run into other problems. the best thing to do is to set up the domains as we suggested

manazo
6 Oct, 2023, 19:38

any idea what might the other problems be?

Drake
6 Oct, 2023, 19:39

nope

manazo
6 Oct, 2023, 19:46

[SOLVED] email password session doesn't work in SSR

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more