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

Discord OAuth, unsure how to save extra information #2334

Closed
mah51 opened this issue Jul 9, 2021 · 1 comment
Closed

Discord OAuth, unsure how to save extra information #2334

mah51 opened this issue Jul 9, 2021 · 1 comment
Labels
question Ask how to do something or how something works

Comments

@mah51
Copy link

mah51 commented Jul 9, 2021

Question 💬

When I use the discord oauth it signs me in perfectly, however it only saves email & image in the user object in mongoDB.

I added the following code to my provider to make it save more info, but it changed nothing and the user object still only has the email and image properties.

/api/auth/[...nextauth].js

providers: [
    Providers.Discord({
      clientId: process.env.DISCORD_ID,
      clientSecret: process.env.DISCORD_SECRET,
      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: 'This is a test',
          id: profile.id,
          username: profile.username,
          avatar: profile.avatar,
          discriminator: profile.discriminator,
          image: profile.image_url,
          email: profile.email,
          isAdmin: false,
          isReviewer: false,
          last_updated: Date.now(),
          public_flags: profile.public_flags,
          flags: profile.flags,
          locale: profile.locale,
          mfa_enabled: profile.mfa_enabled,
          premium_type: profile.premium_type,
          ban_reason: '',
          isBanned: false,
        };
      },
    }),
  ],

How to reproduce ☕️

shown above

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

@mah51 mah51 added the question Ask how to do something or how something works label Jul 9, 2021
@mah51
Copy link
Author

mah51 commented Jul 9, 2021

i fixed this issue with the following code:

 callbacks: {
    async session(session, user) {
      if (session.user && user) {
        session.user = { ...session.user, ...user };
      }
      return session;
    },
    async jwt(token, user, account, profile, isNewUser) {
      if (profile) {
        token = { ...token, ...profile };
      }
      return token;
    },
  },
  providers: [
    Providers.Discord({
      clientId: process.env.DISCORD_ID,
      clientSecret: process.env.DISCORD_SECRET,
    }),
  ],

i think the documentation for this could be a bit clearer, as it seems on the discord page to imply that you should be using the profile function to add extra data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

2 participants