Created
April 12, 2021 12:53
-
-
Save ariscript/e0a92fb531a51c6cd81ff14ea5b15ef9 to your computer and use it in GitHub Desktop.
Discord "identify guilds" NextAuth.js implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}, | |
}), | |
], | |
}); |
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
Thanks, I got this working yesterday by doing something similar