Middleware + Next Auth
Answered
Exzotic posted this in #help-forum
ExzoticOP
Hello! Im using nextjs 13 app dir and next auth. I have lots of different routes that require authentication and more specific things like if a user owns that service they are viewing, what would be the best way to do this? Would it be best to just add a check in the
page.tsx and route.ts files or is there some way I can use middleware?Answered by riský
you can do something like this: https://github.com/shadcn-ui/taxonomy/blob/main/middleware.ts
20 Replies
ExzoticOP
I have my app setup like this, was thinking it could be easiest if I could just apply a general requires auth on the
(core) routes and pages?you can do something like this: https://github.com/shadcn-ui/taxonomy/blob/main/middleware.ts
Answer
ExzoticOP
Ill take a look, thanks
ExzoticOP
For admin protected pages where I need to have the db user, should I just add a check on each admin page that the user is admin?
you could check in middlewire, but you could also do it there
ExzoticOP
Ah, would you happen to know if I could save like the users prisma id in the next auth token?
yeah you can do that
but that seems scuffed
ExzoticOP
ah thanks
yeah i was a little annoyed for a while, but then i saw this and was happier
ExzoticOP
Yeah ive asked before here but didnt really get a good response / example but that looks great
ahh, they seem to get burried sometimes (i don't look that often)
but has your question been answered?
@Exzotic has it been answered as can close this thread then? (and make me feel better that I have successfully helped)
ExzoticOP
Yep should be all good, appreciate the help :)
I did encounter a small error when using prisma in the middleware but I’ll be gone for a bit so I’ll check later
Original message was deleted
folow this to add the solved tag 🙂
@Exzotic I did encounter a small error when using prisma in the middleware but I’ll be gone for a bit so I’ll check later
feel free to create a new thread for this issue...