Oauth rest

export async function getUserGuilds(token) {
const rest = new REST({ version: "10", authPrefix: "Bearer" }).setToken(token);

try {
return await rest.get(Routes.userGuilds());
} catch (error) {
console.error(error);
return [];
}
}
export async function getUserGuilds(token) {
const rest = new REST({ version: "10", authPrefix: "Bearer" }).setToken(token);

try {
return await rest.get(Routes.userGuilds());
} catch (error) {
console.error(error);
return [];
}
}
im passing the accessToken from oauth in as the token here and im getting this error
DiscordAPIError[0]: 401: Unauthorized
DiscordAPIError[0]: 401: Unauthorized
what am i doing wrong here? im using next auth to get the users access token
7 Replies
d.js toolkit
d.js toolkit9mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
${RVX}
${RVX}9mo ago
import DiscordProvider from "next-auth/providers/discord";
import { OAuth2Scopes } from "discord-api-types/v10"
const { NEXTAUTH_SECRET, CLIENT_ID, CLIENT_SECRET } = process.env;

export const authOptions = {
secret: NEXTAUTH_SECRET,
providers: [
DiscordProvider({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
})
],
authorization: {
params: {
scope: [
OAuth2Scopes.Identify,
OAuth2Scopes.Email,
OAuth2Scopes.Guilds
]
}
},
callbacks: {
session: async ({ session, token }) => {

session.profile = token.profile;
session.secrets = token.secrets;

return session;
},
jwt({ token, profile, account }) {
if (account) {
token.secrets = {
accessToken: account.access_token,
refreshToken: account.refresh_token,
tokenType: account.token_type,
expires_at: account.expires_at,
account: account
};
}
if (profile) {
token.profile = profile;
}

return token;
}
},
session: {
strategy: 'jwt',
maxAge: 3600 * 24 * 30 // 30 days
}
};
import DiscordProvider from "next-auth/providers/discord";
import { OAuth2Scopes } from "discord-api-types/v10"
const { NEXTAUTH_SECRET, CLIENT_ID, CLIENT_SECRET } = process.env;

export const authOptions = {
secret: NEXTAUTH_SECRET,
providers: [
DiscordProvider({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
})
],
authorization: {
params: {
scope: [
OAuth2Scopes.Identify,
OAuth2Scopes.Email,
OAuth2Scopes.Guilds
]
}
},
callbacks: {
session: async ({ session, token }) => {

session.profile = token.profile;
session.secrets = token.secrets;

return session;
},
jwt({ token, profile, account }) {
if (account) {
token.secrets = {
accessToken: account.access_token,
refreshToken: account.refresh_token,
tokenType: account.token_type,
expires_at: account.expires_at,
account: account
};
}
if (profile) {
token.profile = profile;
}

return token;
}
},
session: {
strategy: 'jwt',
maxAge: 3600 * 24 * 30 // 30 days
}
};
not sure how to tell if its an invalid token. every time i relog its the same
const session = await getServerSession()
const userGuilds = await getUserGuilds(session.secrets.accessToken);
const session = await getServerSession()
const userGuilds = await getUserGuilds(session.secrets.accessToken);
i get the session and then pass the token in like this correct. looks similar to this but its not vOKASD977CHasjJDu7eqweneqeIUAc3
{
application: {
id: '1059957728778407937',
name: 'Nix-Dev',
icon: '5ba879008d7293be7dde70ca6cb01d2f',
description: '',
type: null,
bot: {
id: '1059957728778407937',
username: 'Nix-Dev',
avatar: '5ba879008d7293be7dde70ca6cb01d2f',
discriminator: '6809',
public_flags: 0,
premium_type: 0,
flags: 0,
bot: true,
banner: null,
accent_color: null,
global_name: null,
avatar_decoration_data: null,
banner_color: null
},
summary: '',
bot_public: false,
bot_require_code_grant: false,
verify_key: 'removed_bc_idk_if_bad',
flags: 11051008,
hook: true,
is_monetized: false
},
expires: '2024-01-13T06:50:05.549000+00:00',
scopes: [ 'identify', 'email' ],
user: {
id: '629424261475336193',
username: 'r.v.x',
avatar: 'b6096f05eb51c0aaf064c359aeeff73f',
discriminator: '0',
public_flags: 4194560,
premium_type: 0,
flags: 4194560,
banner: null,
accent_color: 9699384,
global_name: 'RVX',
avatar_decoration_data: null,
banner_color: '#940038'
}
}
{
application: {
id: '1059957728778407937',
name: 'Nix-Dev',
icon: '5ba879008d7293be7dde70ca6cb01d2f',
description: '',
type: null,
bot: {
id: '1059957728778407937',
username: 'Nix-Dev',
avatar: '5ba879008d7293be7dde70ca6cb01d2f',
discriminator: '6809',
public_flags: 0,
premium_type: 0,
flags: 0,
bot: true,
banner: null,
accent_color: null,
global_name: null,
avatar_decoration_data: null,
banner_color: null
},
summary: '',
bot_public: false,
bot_require_code_grant: false,
verify_key: 'removed_bc_idk_if_bad',
flags: 11051008,
hook: true,
is_monetized: false
},
expires: '2024-01-13T06:50:05.549000+00:00',
scopes: [ 'identify', 'email' ],
user: {
id: '629424261475336193',
username: 'r.v.x',
avatar: 'b6096f05eb51c0aaf064c359aeeff73f',
discriminator: '0',
public_flags: 4194560,
premium_type: 0,
flags: 4194560,
banner: null,
accent_color: 9699384,
global_name: 'RVX',
avatar_decoration_data: null,
banner_color: '#940038'
}
}
bruh how lol. its literally defined
${RVX}
${RVX}9mo ago
No description
${RVX}
${RVX}9mo ago
No, i dont see why it wouldnt tho right now its just localhost:3000/dashboard/guilds im using next.js 14. app router. app/dashboard/guilds/page.jsx
import { getBotGuilds, getGuildIcon } from "@/utils/guildFunctions/functions";
import { getUserGuilds } from "@/utils/userFunctions/functions";
import Link from 'next/link';
import Image from "next/image";
import getServerSession from '@/utils/auth/serverSession'
import { redirect } from "next/navigation";

export default async function Guilds() {
const session = await getServerSession()

if (!session) return redirect('/')

const userGuilds = await getUserGuilds(session.secrets.accessToken);
const botGuilds = await getBotGuilds();
const mutualGuilds = userGuilds.filter((guild) => botGuilds.some((botGuild) => botGuild.id === guild.id));

return (
<div>
{mutualGuilds.map((guild) => {
const redirect = `/dashboard/manage/${guild.id}`;
const guildIcon = getGuildIcon(guild);
return (

<Link href={redirect} key={guild.id}>
<Image src={guildIcon} alt="Guild Icon" width={100} height={100} />
<div>{guild.name}</div>
</Link>
);
})}
</div>
);
}
import { getBotGuilds, getGuildIcon } from "@/utils/guildFunctions/functions";
import { getUserGuilds } from "@/utils/userFunctions/functions";
import Link from 'next/link';
import Image from "next/image";
import getServerSession from '@/utils/auth/serverSession'
import { redirect } from "next/navigation";

export default async function Guilds() {
const session = await getServerSession()

if (!session) return redirect('/')

const userGuilds = await getUserGuilds(session.secrets.accessToken);
const botGuilds = await getBotGuilds();
const mutualGuilds = userGuilds.filter((guild) => botGuilds.some((botGuild) => botGuild.id === guild.id));

return (
<div>
{mutualGuilds.map((guild) => {
const redirect = `/dashboard/manage/${guild.id}`;
const guildIcon = getGuildIcon(guild);
return (

<Link href={redirect} key={guild.id}>
<Image src={guildIcon} alt="Guild Icon" width={100} height={100} />
<div>{guild.name}</div>
</Link>
);
})}
</div>
);
}
just simple stuff to test. no styling uh that would just be signing out and back in yeah? i just figured out next auth today so im just using the default /api/auth/signout /api/auth/signin to handle logging in and out session is null when i logout so 🤷‍♂️ honestly just about to give up lmao. this shit is so frustrating
mallusrgreat
mallusrgreat9mo ago
im pretty sure the prefix is supposed to be Basic?
${RVX}
${RVX}9mo ago
no im good on that part
Want results from more Discord servers?
Add your server