Next.js Discord

Discord Forum

NextAuth: Not getting Discord username user ID in session object

Answered
Bumble bee posted this in #help-forum
Open in Discord
Bumble beeOP
I'm trying out the Discord provider in the example project https://github.com/nextauthjs/next-auth-example

The session object does not contain the username or the ID of the user as you can see. but I want the data that is shown here: https://github.com/nextauthjs/next-auth/blob/aa44904bd63640aee75304bac248036426a520d7/packages/next-auth/src/providers/discord.ts#L3-L19

how can I get this working?

this is my auth.ts:

import NextAuth from "next-auth";

import Discord from "next-auth/providers/discord";
import type { NextAuthConfig } from "next-auth";

export const config = {
  theme: {
    logo: "https://next-auth.js.org/img/logo/logo-sm.png",
  },
  providers: [
    Discord({
      clientId: process.env.DISCORD_CLIENT_ID,
      clientSecret: process.env.DISCORD_CLIENT_SECRET,
    })
  ],
  basePath: "/auth",
  callbacks: {
    authorized({ request, auth }) {
      const { pathname } = request.nextUrl;
      if (pathname === "/middleware-example") return !!auth;
      return true;
    },
  },
} satisfies NextAuthConfig;

export const { handlers, auth, signIn, signOut } = NextAuth(config);
Answered by Bumble bee
nvm! i played around with soemthing from earlier in that thread and it works:


  ...
  callbacks: {
    async jwt({ token, account, profile }) {
      if (account) {
        token.accessToken = account.access_token;
      }

      if (profile) {
        token.id = profile.id;
        token.name = profile.username as string;
        token.email = null;
      }

      return token;
    },
    async session({ session, token }) {
      if (token) {
        if (token?.picture?.includes("discord")) {
          session.user.id = token.id as string;
        }
      }
      return session;
    },
  },
View full answer

4 Replies

Toyger
@Toyger probable solution at least for username https://github.com/nextauthjs/next-auth/issues/7122#issuecomment-1696543062
Bumble beeOP
i tried this. i get this ID which is not a snowflake, i have no idea what it is
Bumble beeOP
nvm! i played around with soemthing from earlier in that thread and it works:


  ...
  callbacks: {
    async jwt({ token, account, profile }) {
      if (account) {
        token.accessToken = account.access_token;
      }

      if (profile) {
        token.id = profile.id;
        token.name = profile.username as string;
        token.email = null;
      }

      return token;
    },
    async session({ session, token }) {
      if (token) {
        if (token?.picture?.includes("discord")) {
          session.user.id = token.id as string;
        }
      }
      return session;
    },
  },
Answer
Bumble beeOP
thank you