nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.98k stars 3.53k forks source link

Discord override provider not working #10162

Open kratess opened 8 months ago

kratess commented 8 months ago

Provider type

Discord

Environment

System: OS: Windows 10 10.0.19045 Memory: 4.51 GB / 15.96 GB Binaries: Node: 20.10.0 - C:\Program Files\nodejs\node.EXE npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD pnpm: 8.6.0 - ~\AppData\Roaming\npm\pnpm.CMD Browsers: Edge: Chromium (122.0.2365.52) Internet Explorer: 11.0.19041.3636 npmPackages: next: latest => 14.1.0 next-auth: beta => 5.0.0-beta.13 react: ^18.2.0 => 18.2.0

Reproduction URL

https://github.com/kratess/next-auth-example

Describe the issue

I followed the docs https://next-auth.js.org/providers/discord and overrided the return of the profile in order to return more info. Even the default id is not returned. So maybe there is something missing.

How to reproduce

override discord provider and see. it is not working

import type { OAuthConfig, OAuthUserConfig } from "."

export interface DiscordProfile extends Record<string, any> {
  accent_color: number
  avatar: string
  banner: string
  banner_color: string
  discriminator: string
  email: string
  flags: number
  id: string
  image_url: string
  locale: string
  mfa_enabled: boolean
  premium_type: number
  public_flags: number
  username: string
  verified: boolean
}

export default function Discord<P extends DiscordProfile>(
  options: OAuthUserConfig<P>
): OAuthConfig<P> {
  return {
    id: "discord",
    name: "Discord",
    type: "oauth",
    authorization:
      "https://discord.com/api/oauth2/authorize?scope=identify+email",
    token: "https://discord.com/api/oauth2/token",
    userinfo: "https://discord.com/api/users/@me",
    profile(profile) {
      if (profile.avatar === null) {
        const defaultAvatarNumber = parseInt(profile.discriminator) % 5
        profile.image_url = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarNumber}.png`
      } else {
        const format = profile.avatar.startsWith("a_") ? "gif" : "png"
        profile.image_url = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`
      }
      return {
        test: "test",
        banner: profile.banner,
        id: profile.id,
        name: profile.username,
        email: profile.email,
        image: profile.image_url,
      }
    },
    style: { logo: "/discord.svg", bg: "#5865F2", text: "#fff" },
    options,
  }
}

Expected behavior

Overrided profile info

kratess commented 8 months ago

In my repository i put

      return {
        test: "test",
        banner: profile.banner,
        id: profile.id,
        name: profile.username,
        email: profile.email,
        image: profile.image_url,
      }

but it doesn't return it. Cattura

NotDemonix commented 8 months ago

So i figured out u need to do it like

providers: [
    Discord({
      clientId: process.env.DISCORD_CLIENT_ID ?? "",
      clientSecret: process.env.DISCORD_CLIENT_SECRET ?? "",
      authorization: "https://discord.com/api/oauth2/authorize?scope=identify+guilds",
      profile: (profile) => {
        ...
        return {
          ...
        };
      }
    }),
  ],

but I still cant add more fields, even the original code doesn't provide all of the fields it should, like user id

NotDemonix commented 8 months ago

https://next-auth.js.org/configuration/callbacks#session-callback

Vasolix commented 8 months ago

Same here this provider can't be overide and the user id as not returned

Zofren commented 7 months ago

Add async :

providers: [
    Discord({
      clientId: process.env.DISCORD_CLIENT_ID ?? "",
      clientSecret: process.env.DISCORD_CLIENT_SECRET ?? "",
      authorization: "https://discord.com/api/oauth2/authorize?scope=identify+guilds",
      async (profile) => {
        ...
        return {
          ...
        };
      }
    }),
  ],