#nextauth discord provider user id

29 messages · Page 1 of 1 (latest)

mortal perch
#

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;
    }
},
}
bronze hedgeBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)
nimble zealot
nimble zealot
#

Please provide more details about the problem.

#

Like, just dont post a screenshot .

mortal perch
nimble zealot
#

can you go to localhost:3000/api/auth/session and show me the reponse.

nimble zealot
#

logout and login again

#

and then check

mortal perch
nimble zealot
#

localhost:3000/api/auth/session

does this endpoint returns the samething?

mortal perch
#

yes

#

and does token expire at time which is given by discord or by nextauh

nimble zealot
#

can you show me your callbacks again?

mortal perch
# nimble zealot can you show me your callbacks again?

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;
    }
},
}
nimble zealot
#

This should work.

Can you log user and token in jwt callback and also log token in session callback.

mortal perch
nimble zealot
#

you have sub property in the token. isnt that what you want?

mortal perch
nimble zealot
#

if (user) { // change user to token

        session.user.id = token.sub as string;
}
mortal perch
#

Oh my bad

#

Thanks so much zia

nimble zealot
#

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.

bronze hedgeBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer

[Click here](#1135988549959827487 message)

pearl stump
#

@gusty fulcrum