email provider - set user role during signing in process #4538
Unanswered
b-novikov-ipersonality
asked this question in
Help
Replies: 1 comment
-
You've already find a way to expose
async jwt({token, user}) {
if (user) {
return {
...token,
// id: user.id,
role: user.role,
}
} else {
return token
}
},
async session({session, token}) {
return {
...session,
user: {
...session.user,
// id: token.id,
role: token.role,
},
}
}, The above assumes you use JWT-based session, not database-based. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys.
I'm using email provider and I need two roles for my application - product owners and product consumers.
Ideally I want two separate registration processes - one for consumer and the other for owner - when user chooses to register as consumer his role becomes consumer, same with owner. I couldn't figure out how to pass some data that would identify role to next-auth handler.
I was thinking about adding query to
callbackUrl
that would containrole=consumer
orrole=owner
but that's not safe, anyone who registered as a consumer can change the query manually toowner
unless there's some kind of verification token that is specific to selected role included.Is there a way to tinker
NextAuth
handler so it would accept data? Like an additional field inbody
or maybe query parameter or cookie or something? Maybe there's some other way?upd:
I found an answer to my "how to pass data" question here
But simply passing data to
signIn
function won't work becauserole
is only provided on initial sign in request when verification email is sent. When user verifies email there's no role.I'm currently looking for a workaround and I thought I could use cookies to store
role
but cookies get overwritten when I confirm email.My
signIn
callback looks like this:I think I can save user email and associated role to database but I really don't want to do that.
How can I preserve
role
in thesignIn
callback? Maybe there's some other way to achieve roles?Beta Was this translation helpful? Give feedback.
All reactions