Files
name-100-uio-gamers/src/app/lib/auth.ts
T
leander 7715db4029 add nextauth_url env var
make sure the oauth callback knows where to go
2025-06-22 02:49:10 +02:00

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,
}),
};