Next.js Discord

Discord Forum

Middleware + Next Auth

Answered
Exzotic posted this in #help-forum
Open in Discord
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?

20 Replies

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?
Answer
Ill take a look, thanks
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
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
as it seems it only really has email which I can use to fetch the user
but that seems scuffed
ah thanks
yeah i was a little annoyed for a while, but then i saw this and was happier
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)
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 🙂