You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm following these docs and I'm trying to add an API route that can access the user session data from Blitz CTX from an API route from Next.js middleware, but the session is not populated. The query getCurrentUser(), called from a component, returns the data as expected.
My intention is to create auth middleware to redirect a user if they don't have a valid userId and role. My preference was to handle this on the server rather than redirecting on the client using a hook or Page.authenticate.
Given you're now signed in I would expect the session to be populated with the signed in session data, but it's outputting: ctx.session { userId: null, publicData: { userId: null } }
It's populating the userId on the client from queries without issue.
ourmaninamsterdam
changed the title
Blitz CTX session not populated in api route called from Next.js middleware
Blitz Auth: Blitz CTX session not populated in api route called from Next.js middleware
May 15, 2024
I tried the same with an App router project as well and saw the same. Is my implementation/understanding of the api handler exported from blitz-server.ts wrong? Or is this a bug?
As all I want to do is check the auth status (against ctx.session.userId or ctx.session.$isAuthorized) in the API route and return that to the Next.js middleware, we can then chooser to redirect if they're unauthorized.
Calling the api route from a component returns the correct data, it's just from middleware there's an issue.
What is the problem?
I'm following these docs and I'm trying to add an API route that can access the user session data from Blitz CTX from an API route from Next.js middleware, but the session is not populated. The query
getCurrentUser()
, called from a component, returns the data as expected.My intention is to create auth middleware to redirect a user if they don't have a valid userId and role. My preference was to handle this on the server rather than redirecting on the client using a hook or
Page.authenticate
.Paste all your error logs here:
None
Paste all relevant code snippets here:
Middleware: /src/middleware.ts:
API route from /src/pages/api/auth/index.ts:
What are detailed steps to reproduce this?
Given you're now signed in I would expect the session to be populated with the signed in session data, but it's outputting:
ctx.session { userId: null, publicData: { userId: null } }
It's populating the userId on the client from queries without issue.
Run
blitz -v
and paste the output here:Please include below any other applicable logs and screenshots that show your problem:
No response
The text was updated successfully, but these errors were encountered: