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.dev
mac.dev2y 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
Scot2y 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
Scot2y ago
can't you just use this
mac.dev
mac.dev2y ago
I’m not sure how or where I’d query it
Scot
Scot2y 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
Scot2y 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.dev
mac.dev2y ago
I’ve tried this and several variations For hours
Scot
Scot2y 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.dev
mac.dev2y 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
Scot2y ago
sorry mean get the guild for your specific user?
mac.dev
mac.dev2y ago
I’m not sure what you mean sorry I’m trying to map all of the guilds the user is in
Scot
Scot2y ago
yeah so start simple, are you able to do it for your user
mac.dev
mac.dev2y ago
No Fetching User or guilds would be the same function with a slightly different API ?
Scot
Scot2y ago
thats what it should be yeah
mac.dev
mac.dev2y ago
I did it via nextAuth profile lol
Want results from more Discord servers?
Add your server