Getting id of user with next-auth
I'm trying to set up next-auth with discord, but I can't get the user ID and discriminator.
[..nextauth].ts
This is what it prints out.
How can i get the user's ID and discriminator?
24 Replies
@Børge Can you try to change it to this and see if the user object prints out the discord id and discriminator now?
Seems like nothing changed
Ah then I think ik why, lemme try something
@Børge
Don't really understand? Is that not the same session callback as mine?
You need to add the fields to the user model in the database when the user signs up / logs in
Okay, but the
session
and user
in the session callback, are they not still going to be the same (without id and discriminator) or...?user
is the user object returned from the database
If you have a discordId and discriminator in your db model it's gonna be in the object as wellI don't really understand
This is my prisma model for user, if you mean that:
And when i print the user in session callback, i just get null
Did you set it in the signIn method?
Update the signIn method to first check if the user exists. If not, create a new user including the discordId and discriminator. Then remove the user from your database and sign up again.
@Børge
Im trying to set it up, but i keep getting this error.
This is my current code:
Looks like something is wrong with the id
Oh yea
const id = profile.id
is the discord id
You should use the discordId
field when querying the user since that's the field you're assigning the profile.id
to.
@BørgeOkay that worked. So now I got it working, where it creates the user in the DB, but it redirects me to
/api/auth/signin?error=OAuthAccountNotLinked
.That probably means there is another user with the same email in the db already
@Børge Check if you can find another user with the same email and remove it
I'm the only one in the database
🤔
I also just tried to delete my self, and then login again, and i got redirected to the same page
My full code at the moment, if that can help
Do you have an Account model in your schema?
Account + User
Yes, Account, Session and User
Ah you need to remove it too
Remove Account?
Wait is there a
allowDangerousEmailAccountLinking
option for DiscordProvider?
If so, try to set it to trueI just tried it, and it seems to be redirecting me correctly now.
Everything seems to be working - thx a lot!
👍