Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsure how to get data from scopes other than the default ones #614

Closed
daniellwdb opened this issue Aug 27, 2020 · 10 comments
Closed

Unsure how to get data from scopes other than the default ones #614

daniellwdb opened this issue Aug 27, 2020 · 10 comments
Labels
docs Relates to documentation question Ask how to do something or how something works stale Did not receive any activity for 60 days

Comments

@daniellwdb
Copy link

daniellwdb commented Aug 27, 2020

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",
      },
    }),
  ],
};
@daniellwdb daniellwdb added the question Ask how to do something or how something works label Aug 27, 2020
@GiampaoloF95
Copy link

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

@shaunchander
Copy link

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).

@daniellwdb
Copy link
Author

Would be nice to have a working example

@LoriKarikari LoriKarikari added the docs Relates to documentation label Aug 29, 2020
@pbshgthm
Copy link

pbshgthm commented Sep 8, 2020

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

@archywillhe
Copy link

archywillhe commented Sep 27, 2020

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?

@stale
Copy link

stale bot commented Dec 5, 2020

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!

@stale stale bot added the stale Did not receive any activity for 60 days label Dec 5, 2020
@stale
Copy link

stale bot commented Dec 12, 2020

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!

@stale stale bot closed this as completed Dec 12, 2020
@nodgear
Copy link

nodgear commented Apr 11, 2021

Guys, the id is called sub as user prop:

  callbacks: {
    session: async (session, user) => {
      session.user.id = user.sub
      console.log("CALLBACK SESSION", session, user)
      return Promise.resolve(session)
    }
  }

this will add ID to the session user object, which is available site-wise.
Then we can use the normal endpoints + token data to fetch everything else. (Except that i can't recover the token either..)

@simxnet
Copy link

simxnet commented Jan 6, 2022

Guys, the id is called sub as user prop:

  callbacks: {
    session: async (session, user) => {
      session.user.id = user.sub
      console.log("CALLBACK SESSION", session, user)
      return Promise.resolve(session)
    }
  }

this will add ID to the session user object, which is available site-wise. Then we can use the normal endpoints + token data to fetch everything else. (Except that i can't recover the token either..)

Well, how it's possible to fetch user's guilds with it?

@nodgear
Copy link

nodgear commented Jan 8, 2022

Guys, the id is called sub as user prop:

  callbacks: {
    session: async (session, user) => {
      session.user.id = user.sub
      console.log("CALLBACK SESSION", session, user)
      return Promise.resolve(session)
    }
  }

this will add ID to the session user object, which is available site-wise. Then we can use the normal endpoints + token data to fetch everything else. (Except that i can't recover the token either..)

Well, how it's possible to fetch user's guilds with it?

Discord API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Relates to documentation question Ask how to do something or how something works stale Did not receive any activity for 60 days
Projects
None yet
Development

No branches or pull requests

8 participants