Files
Puechberty Arthur b7010a1704 feat: add authentication and user management features
- Implemented AuthButton component for Discord sign-in and sign-out functionality.
- Created CopyButton component for copying server IP addresses.
- Developed EventCard and GradeCard components for displaying events and grades.
- Added Footer and Navbar components for site navigation and information.
- Introduced PurchaseButton for handling grade purchases with Stripe integration.
- Created SectionHeader component for consistent section titles.
- Implemented session management with SessionProvider for NextAuth.
- Set up PostgreSQL database with Docker and Prisma for data management.
- Added admin guard functionality to restrict access to certain routes.
- Configured NextAuth with Discord provider for user authentication.
- Defined Prisma schema for user, admin, grade, event, and purchase models.
- Seeded database with initial grades and events data.
- Added SVG hero image for the landing page.
- Extended NextAuth types to include additional user properties.
2026-04-28 21:09:55 +02:00

41 lines
939 B
TypeScript

import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { db } from "@/lib/db";
export type AdminGuardResult =
| {
ok: true;
discordId: string;
}
| {
ok: false;
response: Response;
};
export const requireAdmin = async (): Promise<AdminGuardResult> => {
const session = await getServerSession(authOptions);
const discordId = session?.user?.discordId;
if (!discordId) {
return {
ok: false,
response: Response.json({ error: "Unauthorized" }, { status: 401 }),
};
}
const envAdmin = process.env.ADMIN_DISCORD_ID;
if (envAdmin && envAdmin === discordId) {
return { ok: true, discordId };
}
const admin = await db.admin.findUnique({ where: { discordId } });
if (!admin) {
return {
ok: false,
response: Response.json({ error: "Forbidden" }, { status: 403 }),
};
}
return { ok: true, discordId };
};