#discord userid
1 messages · Page 1 of 1 (latest)
Yes, you can add a custom callback to your NextAuth configuration to get the user ID from Discord. In the jwt callback, you can add the user ID to the token. Then, in the session callback, you can add the user ID to the session. Here is an example from a GitHub issue:
async jwt({ token, user }) {
if (user?.id) {
token.uid = user.id;
}
return token;
},
async session({ session, token }) {
if (token.uid) {
session.user = {
id: token.uid,
// add other user properties here
};
}
return session;
},
You can add these callbacks to your NextAuth configuration like this:
const handler = NextAuth({
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID as string,
clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
}),
],
secret: process.env.NEXTAUTH_SECRET as string,
callbacks: {
jwt: async ({ token, user }) => {
if (user?.id) {
token.uid = user.id;
}
return token;
},
session: async ({ session, token }) => {
if (token.uid) {
session.user = {
id: token.uid,
// add other user properties here
};
}
return session;
},
},
})
export { handler as GET, handler as POST }
Please replace the comment // add other user properties here with the actual properties you want to add to the session.
This code is based on the Github Issue (open): No response is returned from route handler (Response differs due to polyfill!) > Comments Part 3 (https://github.com/vercel/next.js/issues/58611).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@viral jasper kapa.ai is still learning and improving, please let us know how it did by reacting below