All files / lib session.ts

0% Statements 0/27
0% Branches 0/1
0% Functions 0/1
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                                                       
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { SESSION_COOKIE, verifySession, type SessionPayload } from './auth';

export async function getSession(): Promise<SessionPayload | null> {
  const token = (await cookies()).get(SESSION_COOKIE)?.value;
  if (!token) return null;
  return verifySession(token);
}

export async function requireSession(): Promise<SessionPayload> {
  const session = await getSession();
  if (!session) redirect('/login');
  return session;
}

/**
 * Higher-level guard: gate a route behind one of the permission helpers
 * in src/lib/permissions.ts.  Redirects to /dashboard if the check fails.
 */
export async function requirePermission(
  check: (role: 'MASTER' | 'STAFF') => boolean,
): Promise<SessionPayload> {
  const session = await requireSession();
  if (!check(session.role)) redirect('/dashboard');
  return session;
}