Skip to content

Instantly share code, notes, and snippets.

@ariscript
Created April 12, 2021 12:53
Show Gist options
  • Save ariscript/e0a92fb531a51c6cd81ff14ea5b15ef9 to your computer and use it in GitHub Desktop.
Save ariscript/e0a92fb531a51c6cd81ff14ea5b15ef9 to your computer and use it in GitHub Desktop.
Discord "identify guilds" NextAuth.js implementation
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
export default NextAuth({
session: {
jwt: true,
},
callbacks: {
jwt: async (token, user, account, profile) => {
// adding profile items to the token
if (user) {
token.profile = profile;
token.accessToken = account.accessToken;
token.refreshToken = account.refreshToken;
}
return Promise.resolve(token);
},
// @ts-ignore
session: async (session, token) => {
// copy token information into session
// @ts-ignore
session.user.accessToken = token.accessToken;
// @ts-ignore
session.user.refreshToken = token.refreshToken;
// @ts-ignore
session.user.profile = token.profile;
// add user's guilds into session
const res = await fetch(
"https://discord.com/api/v8/users/@me/guilds",
{
// @ts-ignore
headers: { Authorization: `Bearer ${token.accessToken}` },
}
);
const data = await res.json();
// @ts-ignore
session.user.profile.guilds = data;
return session;
},
},
// configure Discord provider
providers: [
Providers.Discord({
clientId: process.env.DISCORD_ID,
clientSecret: process.env.DISCORD_SECRET,
scope: "identify guilds",
// @ts-ignore
profile(profile) {
return profile;
},
}),
],
});
@BossDaily
Copy link

Thanks, I got this working yesterday by doing something similar

// [...nextauth].ts
import NextAuth from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions = {
  // Configure one or more authentication providers
  providers: [
    DiscordProvider({
      // @ts-ignore
      clientId: process.env.DISCORD_CLIENT_ID,
      // @ts-ignore
      clientSecret: process.env.DISCORD_CLIENT_SECRET,
      authorization: {
        params: {
          scope: "identify guilds email connections",
        },
      },
      profile(profile) {
        console.log(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 {
          id: profile.id,
          name: profile.username,
          discriminator: profile.discriminator,
          image: profile.image_url,
          accentColor: profile.accentColor,
        };
      },
      // ...add more providers here
    }),
  ],
  callbacks: {
    //@ts-ignore
    jwt: async ({ token, account, profile }) => {
      if (account) {
        token.accessToken = account.access_token;
        token.tokenType = account.token_type;
      }
      if (profile) {
        token.profile = profile;
      }
      return token;
    },
    // @ts-ignore
    session: async ({ session, token }) => {
      // @ts-ignore
      session.accessToken = token.accessToken;
      // @ts-ignore
      session.refreshToken = token.refreshToken;
      // @ts-ignore
      session.tokenType = token.tokenType;
      // @ts-ignore
      session.discordUser = token.profile;
      // @ts-ignore
      
      return session;
    },
  },
};
// @ts-ignore
export default NextAuth(authOptions)
// page 
export const getServerSideProps: GetServerSideProps = async (context) => {
  const session = await unstable_getServerSession(
    context.req,
    context.res,
    authOptions
  );
  const guildFetch = await fetch(
    `https://discord.com/api/v10/users/@me/guilds`,
    {
      headers: {
        // @ts-ignore
        Authorization: `Bearer ${session?.accessToken}`,
      },
    }
  );
  const guilds = await guildFetch.json();
  
  console.log(session)
  return {
    props: {
      guilds,
    },
  };
};

@BossDaily
Copy link

BossDaily commented Nov 9, 2022

https://github.com/ana-log/analog-tsx repo im making that is a template that allows you to make discord bots with a nextjs dashboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment