Next.js Discord

Discord Forum

nextauth discord provider user id

Answered
Bombay posted this in #help-forum
Open in Discord
BombayOP
what am i doing wrong cant get user.id
import DiscordProvider from "next-auth/providers/discord";
import {NextAuthOptions} from "next-auth"

declare module "next-auth" {
    interface Session {
        user: User;
    }
}

interface User {
    id: string;
    name: string;
    image: string;
    email: string;
}
// code here
export const authOptions: NextAuthOptions =  { providers: [
    DiscordProvider({
        clientId: process.env.CLIENT_ID as string,
        clientSecret: process.env.CLIENT_SECRET as string,
        authorization: {
            params: {
                scope: "identify guilds"
            }
        }
    }),
],
callbacks: {
    async jwt({token, user}) {
        if (user) {
            token.sub = user.id;
        }
        return token;
    },
    async session({session, user}) {
if (user) {
        session.user.id = user.id;
}
        return session;
    }
},
}
Answered by Bigheaded ant
if (user) { // change user to token

        session.user.id = token.sub as string;
}
View full answer

26 Replies

Bigheaded ant
Please provide more details about the problem.
Like, just dont post a screenshot .
@Bigheaded ant Please provide more details about the problem.
BombayOP
i modified session.user.id to be token.sub put i am not getting id
Bigheaded ant
can you go to localhost:3000/api/auth/session and show me the reponse.
@Bigheaded ant can you go to `localhost:3000/api/auth/session` and show me the reponse.
BombayOP
Bigheaded ant
logout and login again
and then check
@Bigheaded ant logout and login again
BombayOP
did tht result is same
Bigheaded ant
localhost:3000/api/auth/session

does this endpoint returns the samething?
BombayOP
yes
and does token expire at time which is given by discord or by nextauh
Bigheaded ant
can you show me your callbacks again?
@Bigheaded ant can you show me your callbacks again?
BombayOP
Sure
import DiscordProvider from "next-auth/providers/discord";
import {NextAuthOptions} from "next-auth"

declare module "next-auth" {
    interface Session {
        user: User;
    }


interface User {
    id: string;
    name: string;
    image: string;
    email: string;
}
}
// code here
export const authOptions: NextAuthOptions =  { providers: [
    DiscordProvider({
        clientId: process.env.CLIENT_ID as string,
        clientSecret: process.env.CLIENT_SECRET as string,
        authorization: {
            params: {
                scope: "identify guilds"
            }
        }
    }),
],
callbacks: {
    async jwt({token, user}) {
        if (user) {
            token.sub = user.id;
        }
        return token;
    },
    async session({session, user, token}) {
if (user) {
        session.user.id = token.sub as string;
}
        return session;
    }
},
}
Bigheaded ant
This should work.

Can you log user and token in jwt callback and also log token in session callback.
@Bigheaded ant This should work. Can you log `user` and `token` in `jwt` callback and also log `token` in `session` callback.
BombayOP
token:[object Object] userundefined
{
name: 'jagrit_thukral',
picture: 'https://cdn.discordapp.com/avatars/883280307636289567/f97860d86d409b0f25d1a0c1bf6d6c1a.png',
sub: '883280307636289567',
iat: 1690916952,
exp: 1693508952,
jti: '12b69c0d-* // idk what is this so i censored
}
Bigheaded ant
you have sub property in the token. isnt that what you want?
Bigheaded ant
if (user) { // change user to token

        session.user.id = token.sub as string;
}
Answer
BombayOP
Oh my bad
Thanks so much zia
Bigheaded ant
you are checking for user in session callback, user is only available in session callback when you are using database sessions.
No problem buddy. have fun coding
remember to mark this as solved.
Transvaal lion
@Western yellowjacket