User and session are null after successful login and showing user as active in dashboard - any ideas why?

I am creating an app with NextJS and need to handle my authentication on the server (api route), as I will want to make db updates when a user is created.

I followed the instructions for setting up stytch auth on a nextjs app as closely as possible. However, after what appears to be a successful login (the user shows up as active in the dashboard), I am still getting a null for both user and session on the client side. Any ideas why?

There are no errors or warnings on the client or server.

I created a small test app that reproduces the issue - https://github.com/andersr/stytch-auth-test

Any tips or insights would be appreciated!

Hey Anders,

Thanks so much for posting and providing that test app!

I pulled down the example app locally and was able to get it to work by changing SESSION_COOKIE in src/pages/api/auth/authenticate.ts to "stytch_session" rather than "stytch_cookie". After doing so, the user data is non-null and logged to the console (and my name shows up in the UI):

This is because our SDK by default stores (and looks for) the session_token in a cookie called stytch_session. Alternatively, you can initialize the JS SDK with a configuration that looks for the session_token in a different cookie (e.g. stytch_cookie) instead - more info on how our JS SDK uses cookies and how to add cookie configuration can be found in our docs here.

Another way to generally hydrate a frontend with a session generated by a backend is with our session.updateSession() endpoint, where the backend returns the token for the frontend to hydrate.

Please let us know if you have any further questions about this! I’d be happy to push up a PR to the example app if it’s helpful (I’d just need access to the repo).