mirror of
https://github.com/bitbaum/vitareba.git
synced 2026-12-06 07:28:34 +01:00
* 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 (304 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> * style: format the two files the reformat pass missed prettier --check flagged app/shared.module.css and lib/domain/clinicians.test.ts on a branch whose whole point was that everything had been formatted. Re-running the same command with the same version fixes both, so the rollout's write step did not finish what it reported. The rollout now runs --check straight after --write and refuses to open a PR unless it comes back clean, so a partial reformat fails at the source instead of in CI. --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
import { auth } from "@/lib/auth";
|
|
import { NextResponse } from "next/server";
|
|
import type { Session } from "next-auth";
|
|
import { eq } from "drizzle-orm";
|
|
import { db } from "@/lib/db";
|
|
import { users } from "@/lib/db/schema";
|
|
import { USER_ROLE } from "@/lib/config/auth";
|
|
|
|
export async function requireSession(): Promise<
|
|
{ session: Session; error: null } | { session: null; error: NextResponse }
|
|
> {
|
|
const session = await auth();
|
|
if (!session) {
|
|
return {
|
|
session: null,
|
|
error: NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 }),
|
|
};
|
|
}
|
|
return { session, error: null };
|
|
}
|
|
|
|
/**
|
|
* Verifies the request carries a valid CRON_SECRET bearer token.
|
|
* Returns a 401 NextResponse if the check fails, null if authorized.
|
|
* Usage: const cronError = requireCron(req); if (cronError) return cronError;
|
|
*/
|
|
export function requireCron(req: Request): NextResponse | null {
|
|
const secret = process.env.CRON_SECRET;
|
|
const authHeader = req.headers.get("authorization");
|
|
// Reject if secret is not configured or header doesn't match
|
|
if (!secret || authHeader !== `Bearer ${secret}`) {
|
|
return NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export async function requireAdmin(): Promise<
|
|
{ session: Session; error: null } | { session: null; error: NextResponse }
|
|
> {
|
|
const session = await auth();
|
|
if (!session || session.user.role !== USER_ROLE.admin) {
|
|
return {
|
|
session: null,
|
|
error: NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 }),
|
|
};
|
|
}
|
|
return { session, error: null };
|
|
}
|
|
|
|
/**
|
|
* Second occurrence of this exact check (first: api/clinician/accepting-patients)
|
|
* — moved here instead of copied again, per the project's "notice on the 2nd,
|
|
* extract by the 3rd" rule, since more clinician-only routes were already
|
|
* planned. `isClinician` isn't on the session/JWT, so this is a DB read.
|
|
*/
|
|
export async function requireClinician(): Promise<
|
|
| { session: Session; clinicianId: string; error: null }
|
|
| { session: null; clinicianId: null; error: NextResponse }
|
|
> {
|
|
const session = await auth();
|
|
if (!session) {
|
|
return {
|
|
session: null,
|
|
clinicianId: null,
|
|
error: NextResponse.json({ success: false, error: "Unauthorized" }, { status: 401 }),
|
|
};
|
|
}
|
|
|
|
const row = await db.query.users.findFirst({
|
|
where: eq(users.id, session.user.id),
|
|
columns: { isClinician: true },
|
|
});
|
|
if (!row?.isClinician) {
|
|
// Not "forbidden" — a plain patient account has no reason to learn that
|
|
// this route exists at all.
|
|
return {
|
|
session: null,
|
|
clinicianId: null,
|
|
error: NextResponse.json({ success: false, error: "Not found" }, { status: 404 }),
|
|
};
|
|
}
|
|
return { session, clinicianId: session.user.id, error: null };
|
|
}
|