Next-auth: Unsure how to get data from scopes other than the default ones

Created on 27 Aug 2020  路  7Comments  路  Source: nextauthjs/next-auth

Using the Discord provider I added a scope option to include "guilds", I am not sure how I can access the guilds after they have been allowed, below is my attempt, I also tried to just add a new property guilds with the same value outside of the profile property

const options = {
  providers: [
    Providers.Discord({
      clientId: process.env.DISCORD_CLIENT_ID,
      clientSecret: process.env.DISCORD_CLIENT_SECRET,
      scope: "identify guilds",
      profile: {
        guilds: "https://discordapp.com/api/users/@me/guilds",
      },
    }),
  ],
};
documentation question stale

Most helpful comment

This happens because on subsequent invocation (every time session is called), profile is not passed to jwt callback ( user, account, profile, isNewUser is passed only the first time ie. when the signin happens )

jwt: async (token, user, account, profile, isNewUser) => {
    if (profile) { token.profile = profile}
    return Promise.resolve(token)
  }

You needn't add the profile method to the Provider options to add them to jwt.
This should hopefully solve the problem

All 7 comments

You'll need to expand your session using next-auth's callbacks.

You can add data to your JWT using the JWT callback:
https://next-auth.js.org/configuration/callbacks#jwt-callback
then you can get your data from the sign in objects you see in the documentation and pass them on to the token to make them persistent through the session.

Then to expand the session you can do the same using the session callback:
https://next-auth.js.org/configuration/callbacks#session-callback

You'll need to expand your session using next-auth's callbacks.

You can add data to your JWT using the JWT callback:
https://next-auth.js.org/configuration/callbacks#jwt-callback
then you can get your data from the sign in objects you see in the documentation and pass them on to the token to make them persistent through the session.

Then to expand the session you can do the same using the session callback:
https://next-auth.js.org/configuration/callbacks#session-callback

I've tried this but to no avail?

I've changed the profile method on my provider (Discord) to this:

profile: (profile) => {
    return {
        ...profile,
    }
},

And changed my jwt to be like this

jwt: async (token, user, account, profile, isNewUser) => {
 token.profile = profile
    return Promise.resolve(token)
},

However, in the browser returns the JWT with the original user object, only this time the name and image fields are null (email exists as a property in ...profile so it's already assigned back to token like normal).

Would be nice to have a working example

This happens because on subsequent invocation (every time session is called), profile is not passed to jwt callback ( user, account, profile, isNewUser is passed only the first time ie. when the signin happens )

jwt: async (token, user, account, profile, isNewUser) => {
    if (profile) { token.profile = profile}
    return Promise.resolve(token)
  }

You needn't add the profile method to the Provider options to add them to jwt.
This should hopefully solve the problem

hmm I'm getting some really strange behavior for users that already have accounts created in the db:

when jwt: async (token, user, account, profile, isNewUser) is called with account, etc (i.e. signin happens), I'm able to inject something based on the account to my token (e.g. token.prop = account.prop), get it to be encoded in jwt.encode, and later on decodes it i.e. jwt.decode & pass it to session (e.g. session.prop = token.prop) in the session callback.

but after the user logs in and gets directed to the landing page, when i check session (in the broswer) sometimes the prop is there, sometimes it is not i.e. as if in a quantum state.

anyone running into similiar things?

Hi there! It looks like this issue hasn't had any activity for a while. It will be closed if no further activity occurs. If you think your issue is still relevant, feel free to comment on it to keep ot open. Thanks!

Hi there! It looks like this issue hasn't had any activity for a while. To keep things tidy, I am going to close this issue for now. If you think your issue is still relevant, just leave a comment and I will reopen it. (Read more at #912) Thanks!

Was this page helpful?
0 / 5 - 0 ratings