kanban-dev-webapp/src/lib/auth/session.ts

23 lines
No EOL
655 B
TypeScript

import "server-only";
import { getCurrentUser } from "@/lib/pb/server";
import { AccessError, type Session } from "@/lib/auth/access";
/** The current session, or null when signed out. */
export async function getSession(): Promise<Session | null> {
const user = await getCurrentUser();
if (!user) return null;
return {
userId: user.id,
role: user.role,
clientId: user.client || null,
};
}
/** The current session, or throw a 401. For route handlers. */
export async function requireSession(): Promise<Session> {
const session = await getSession();
if (!session) throw new AccessError("Not signed in", 401);
return session;
}