next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

SvelteKitAuth Discord Provider does not return id field from profile

Open math280h opened this issue 3 years ago • 3 comments

Provider type

Discord

Environment

	"@auth/core": "^0.10.0",
	"@auth/sveltekit": "^0.3.6",

Reproduction URL

https://gist.github.com/math280h/c6d1807ee0137dec22e2b5e34363808c

Describe the issue

sveltekitauth Discord provider does not return id field.

How to reproduce

Define:

    SvelteKitAuth({
        providers: [
            Discord({
                clientId: DISCORD_CLIENT_ID,
                clientSecret: DISCORD_CLIENT_SECRET,
                authorization: { params: { scope: "identify" } },
            }),
        ],
    }),

Now try accessing it like:

    const session = await event.locals.getSession();
    const user = session?.user as DiscordProfile;
    console.log(user)

Only these fields are returned:

{
  name: '<name>',
  image: 'https://cdn.discordapp.com/avatars/<user id works here???>/<avatar>.png'
}

Expected behavior

id is returned from profile function.

math280h avatar Jul 19 '23 21:07 math280h

In my case I also get the email, but yeah, the id is missing.

Allavaz avatar Aug 06 '23 16:08 Allavaz

I just found out that the session object doesn't return the id by design. The session callback docstring says the following:

/**
   * This callback is called whenever a session is checked.
   * (Eg.: invoking the `/api/session` endpoint, using `useSession` or `getSession`)
   *
   * ⚠ By default, only a subset (email, name, image)
   * of the token is returned for increased security.
   *
   * If you want to make something available you added to the token through the `jwt` callback,
   * you have to explicitly forward it here to make it available to the client.
   *
   * @see [`jwt` callback](https://authjs.dev/reference/core/types#jwt)
   */

Which is kinda confusing because the profile function says it defaults to id, email, name, image. I guess that means those are the fields that are saved into the token (or DB), but that doesn't necessarily mean all those fields are returned in the session object?

Anyway, changing this line to include the id like this:

user: { id: token.sub, name: token.name, email: token.email, image: token.picture },

does the trick. But I'm not sure that's the proper way of solving the problem.

Allavaz avatar Aug 06 '23 19:08 Allavaz

i noticed this as well. you can also get the user's id from the avatar url thats provided, thats how im adding it to my object

vunsh avatar Oct 05 '23 08:10 vunsh