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

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