NextAuth user in middleware
Hi, I want to get user in the middleware so I can do conditional redirection of the users that don't have specific property yet defined. Like an onboarding screen. How can i get user in the middleware to have something like this:
https://nextjs.org/docs/advanced-features/middleware#conditional-statements
Advanced Features: Middleware | Next.js
Learn how to use Middleware to run code before a request is completed.
14 Replies
dont think you can unless doing jwt solution
Currently I'm doing this in layout that I'm using on every page
But this for some reason, when I don't have
divisionId
, it properly goes to /start
and when I set it like this
it goes to /
and goes back to /start
. I need to refresh the page to properly go to /
and stay there
Do you where else I could do something like this?
I thought that If i move that to middleware, it will solve the issue
Ok, I managed to solve that by creating another query (hasAlreadyOnboarded
), that I invalidate on mutationUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
Same ^
Think #Type Error - can't find role on User model is the same issue!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Mhmm yep same idea I'll probs use whatever key is shared within the two objects (in that case ill try it with the user id)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Oh yeah, nah doesn't seem too hacky to me?
The hackyness feels for me more about editing the .d.ts files in the node modules folder...
why would you edit those
Ahh it was in the other thread
Hang on
id not being uncluded in the session type?
Found the thread, but seems I got a little confused about how people were going about it... (The .d.ts wasn't in the node modules it was in the types folder) - MY BAD
Either way seems we've found some good solutions to this now
doesnt have to be in a types folder either just in the project but yh
Nooo I meant they were editing the next auth file scaffolded by create t3
Which is in src/types
Not the .d.ts in next auth in node_modules
But yea