Role bases authentication with Github Provider using next auth
I want to implement a role based authentication like admin, author and reader using next auth and providers like github, so far I've found doing the same using credentials, is it possible with providers like github etc?
12 Replies
Yes it's possible
If you went through the code of a default t3-app you'd see roles already implemented, you'd just have to uncomment some lines.
Will check that out, thanks!
I'm getting this error while trying to sign in
So first of all I don't recommend using JWT's... just don't, leave that be, use sessions when you're beginning
so what should I do?
Idk
I just uncommented the code and this happened
🤷
okay so its working now, how can I add roles tho??
To add Roles, You first need to modify the User Model (considering you're using Prisma), you can do that by adding a
role String @default("USER")
to User Mode.
and then modify the Next Auth Options like this:
I'm using Credential Provider, but you might have to change it to Github and remove the pages section and the jwt callback as well, cuz you might (in my opinion, you should) use database session strategy.
The only thing you have to figure out here is, how are you going to modify the role of a userthanks for the reply!
I also came up with similar solution
the user that comes from the callback methods, they comes directely from the database?
I believe so, because I inserted some values in database and the session values were same