16 lines
561 B
TypeScript
16 lines
561 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import { getSessionCookieName, verifySessionToken } from "@/lib/session";
|
|
|
|
export async function requireApiAuth(): Promise<NextResponse | null> {
|
|
const cookieStore = await cookies();
|
|
const authCookie = cookieStore.get(getSessionCookieName());
|
|
const isAuthenticated = authCookie ? await verifySessionToken(authCookie.value) : false;
|
|
|
|
if (!isAuthenticated) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
return null;
|
|
}
|