From 7764d98f14e672b8528195a68948fca8cdb06662 Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Sun, 22 Jun 2025 02:37:21 +0200 Subject: [PATCH] move auth handler and fix type issues --- src/app/api/auth/[...nextauth]/route.ts | 43 +------------------------ src/app/api/guess/route.ts | 2 +- src/app/auth/error/page.tsx | 2 +- src/app/lib/auth.ts | 42 ++++++++++++++++++++++++ src/app/page.tsx | 2 +- 5 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 src/app/lib/auth.ts diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 447a9b8..8931ab0 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,46 +1,5 @@ -import { verifyUserRole } from "@/lib/discord"; -import type { NextAuthOptions } from "next-auth"; import NextAuth 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, -}; +import { authOptions } from "../../../lib/auth"; const handler = NextAuth(authOptions); diff --git a/src/app/api/guess/route.ts b/src/app/api/guess/route.ts index 4c85982..21896aa 100644 --- a/src/app/api/guess/route.ts +++ b/src/app/api/guess/route.ts @@ -1,4 +1,4 @@ -import { authOptions } from "@/api/auth/[...nextauth]/route"; +import { authOptions } from "@/lib/auth"; import type { Match } from "@/types"; import { getServerSession } from "next-auth/next"; import { NextResponse } from "next/server"; diff --git a/src/app/auth/error/page.tsx b/src/app/auth/error/page.tsx index 74bcf01..096de2a 100644 --- a/src/app/auth/error/page.tsx +++ b/src/app/auth/error/page.tsx @@ -1,5 +1,5 @@ -import { authOptions } from "@/api/auth/[...nextauth]/route"; import { LanguageProvider } from "@/LanguageContext"; +import { authOptions } from "@/lib/auth"; import { getServerSession } from "next-auth"; import { redirect } from "next/navigation"; import AuthErrorClient from "./AuthErrorClient"; diff --git a/src/app/lib/auth.ts b/src/app/lib/auth.ts new file mode 100644 index 0000000..91259a2 --- /dev/null +++ b/src/app/lib/auth.ts @@ -0,0 +1,42 @@ +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, +}; diff --git a/src/app/page.tsx b/src/app/page.tsx index 7e123f6..d632778 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,5 +1,5 @@ -import { authOptions } from "@/api/auth/[...nextauth]/route"; import { LanguageProvider } from "@/LanguageContext"; +import { authOptions } from "@/lib/auth"; import { getServerSession } from "next-auth"; import HomeClient from "./components/HomeClient"; import LoginClient from "./components/LoginClient";