Files
petvity/lib/auth/guards.ts
T
GandClaude Opus 5 c65ac4b7d7 chore: add a formatter (#71)
* chore: add a formatter, matching the style this repo already writes

singleQuote=false was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (252 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 10:05:01 +00:00

34 lines
1.0 KiB
TypeScript

import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
import type { Session } from "next-auth";
import type { UserRole } from "./types";
type GuardSuccess = { session: Session; error: null };
type GuardFailure = { session: null; error: NextResponse };
export async function requireSession(): Promise<GuardSuccess | GuardFailure> {
const session = await auth();
if (!session) {
return {
session: null,
error: NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 }),
};
}
return { session, error: null };
}
export async function requireRole(role: UserRole): Promise<GuardSuccess | GuardFailure> {
const session = await auth();
if (!session || session.user.role !== role) {
return {
session: null,
error: NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 }),
};
}
return { session, error: null };
}
export async function requireAdmin(): Promise<GuardSuccess | GuardFailure> {
return requireRole("admin");
}