NextAuth Discord Getting Guilds

I've been trying to do this for hours now, how do I fetch the users guilds via NextAuth ? I then want to store the guilds within the guilds within the token. Spoon Feeding would be amazing, I have depression because of this.
20 Replies
mac
macOP3y ago
import NextAuth from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions = {
// Configure one or more authentication providers
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
callbackUrl: process.env.NEXTAUTH_URL,
authorization: {
params: {
scope:
"identify email guilds applications.commands.permissions.update",
},
},
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,
};
},
}),
],
callbacks: {
async jwt({ token, account, profile }) {
if (account) {
token.accessToken = account.access_token;
token.tokenType = account.token_type;
}
if (profile) {
token.profile = profile;
}
return token;
},

async session({ session, token, user }) {
if (session) {
session.accessToken = token.accessToken;
session.tokenType = token.tokenType;
session.discordUser = token.profile;
}

return session;
},
},
};
export default NextAuth(authOptions);
import NextAuth from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions = {
// Configure one or more authentication providers
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
callbackUrl: process.env.NEXTAUTH_URL,
authorization: {
params: {
scope:
"identify email guilds applications.commands.permissions.update",
},
},
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,
};
},
}),
],
callbacks: {
async jwt({ token, account, profile }) {
if (account) {
token.accessToken = account.access_token;
token.tokenType = account.token_type;
}
if (profile) {
token.profile = profile;
}
return token;
},

async session({ session, token, user }) {
if (session) {
session.accessToken = token.accessToken;
session.tokenType = token.tokenType;
session.discordUser = token.profile;
}

return session;
},
},
};
export default NextAuth(authOptions);
that is my [...nextauth].js
import {Session} from "next-auth";
import {useSession as nextAuthUseSession} from "next-auth/react";
import {DiscordProfile} from "next-auth/providers/discord";

export function useSession() {
return nextAuthUseSession() as {data: Session & {discordUser: DiscordProfile} | null}
}
import {Session} from "next-auth";
import {useSession as nextAuthUseSession} from "next-auth/react";
import {DiscordProfile} from "next-auth/providers/discord";

export function useSession() {
return nextAuthUseSession() as {data: Session & {discordUser: DiscordProfile} | null}
}
That is my UseSession.ts (hook)
Scot
Scot3y ago
Discord Developer Portal
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
Scot
Scot3y ago
can't you just use this
mac
macOP3y ago
I’m not sure how or where I’d query it
Scot
Scot3y ago
GitHub
Getting Discord Guilds · Discussion #3774 · nextauthjs/next-auth
Question 💬 Hello, I've been reading the docs and trying stuff out for sometime now, with callbacks, changing scopes, playing with prisma and so one. Despite all of this, I just can'...
Scot
Scot3y ago
this was the second result on google? maybe that slight yidfferent in scopes might help? otherwise i think you can do it on the clientside?
mac
macOP3y ago
I’ve tried this and several variations For hours
Scot
Scot3y ago
id try and write a piece of code that gets this for your specific user and then see if you can generalise it
mac
macOP3y ago
I have the code to get the user That works fine I use the nextAuth profile for that I have but it does the fetch via nextAuth Profile *
Scot
Scot3y ago
sorry mean get the guild for your specific user?
mac
macOP3y ago
I’m not sure what you mean sorry I’m trying to map all of the guilds the user is in
Scot
Scot3y ago
yeah so start simple, are you able to do it for your user
mac
macOP3y ago
No Fetching User or guilds would be the same function with a slightly different API ?
Scot
Scot3y ago
thats what it should be yeah
mac
macOP3y ago
I did it via nextAuth profile lol
Leonidas
Leonidas3y ago
Wild idea: how about you store the guilds of each user in the database, add a relation between guilds and users and you can directly query your db for the guilds of a user. Would that work for you? This way you would not need to fetch the guilds from discord for auth
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mac
macOP3y ago
I think in most situations that would work, however I need to map out: - All guilds user is in - All guilds user is in with bot I’m trying to make a page which displays the different guilds you can configure (or add my discord bot too)
Leonidas
Leonidas3y ago
How about you move this responsibilty to a new endpoint which is regularely invoked, either by the client when a user loads the page or a cron job
mac
macOP3y ago
I mean probably, I just want to know how you can create different ‘profiles’ and how to configure the endpoints if there is a way
Want results from more Discord servers?
Add your server