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
![](https://utfs.io/f/a2803d1c-0deb-401f-b282-ca25fb91e8bc-1nq2cb.png)
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
.![](https://utfs.io/f/aa6403a0-8493-438e-90a2-9d0f65fc9230-1nq2cb.png)
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
![](https://utfs.io/f/14fdd495-6695-45b4-8b9b-774078c5dbb8-jlo1ca.png)