mirror of
https://github.com/UiO-Gaming/name-100-uio-gamers.git
synced 2026-11-05 13:56:53 +01:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { verifyUserRole } from "@/lib/discord";
|
|
import type { NextAuthOptions } from "next-auth";
|
|
import DiscordProvider, { type DiscordProfile } from "next-auth/providers/discord";
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
providers: [
|
|
DiscordProvider({
|
|
clientId: process.env.DISCORD_CLIENT_ID as string,
|
|
clientSecret: process.env.DISCORD_CLIENT_SECRET as string,
|
|
profile(profile: DiscordProfile) {
|
|
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.global_name ?? profile.username,
|
|
email: profile.email,
|
|
image: profile.image_url,
|
|
};
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
async signIn({ user }) {
|
|
const hasRequiredRole = await verifyUserRole(user.id);
|
|
|
|
if (!hasRequiredRole) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
},
|
|
},
|
|
pages: {
|
|
error: "/auth/error",
|
|
},
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
...(process.env.NEXTAUTH_URL && {
|
|
url: process.env.NEXTAUTH_URL,
|
|
}),
|
|
};
|