Role is undefined when using NextAuth
Hello I am trying to create role based authorization and attempting to access the user's role from useSession in Next and from protected routes in trpc.
I have added a UserRole to the user model in the prisma schema using an enum, and defaulted it to USER. Also in the server/auth ive added the role to Session and user interfaces and to the session callback.
I have seen other people using a signIn or jwt callback and have no clue what they do or mean.
When I do use access the role in useSession in NextJS or throuhg ctx in protected routes, the role is undefined. Can anybody give me some insight into how all of this works and the flow of data for the session.
Thankyou
4 Replies
Does the user actually have a role value in the db? 👀 I did the same not too long ago (not with an enum tho), and I am not seeing anything different. Perhaps, try and re-authenticate the user?
Yes the all the users have the USER role in the db, since I put that as default in the prisma schema
I have tried reaunenticate the user multiple times
What did u use instead of an enum?
turns out i just had to restart everything, including next and prisma studio and everything is working now
thanks
😮 Nice
I noticed that I have to restart my vscode a lot after even prisma generate. A simple reset typescript doesn’t do the job.
Ohhh that could be it! I’ll try next time! It doesn’t make sense!