Initial commit

This commit is contained in:
twotalesanimation
2026-06-11 10:46:09 +02:00
commit 81ad7e4ea9
223 changed files with 39530 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
// lib/auth-check.ts
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { redirect } from 'next/navigation';
/**
* Require a logged-in user. If not logged in, redirect to /login.
* Returns the session object when present.
*
* Use this helper from server components / app-route handlers.
*/
export async function requireUser(redirectTo = '/login') {
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
// server-side redirect (preferred for pages)
redirect(redirectTo);
}
return session;
}
/**
* Use this variant for API routes where you want JSON 401/403 instead of redirect.
*/
export async function requireUserApi() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) {
const err: any = new Error('Unauthorized');
err.status = 401;
throw err;
}
return session;
}