user auth bearer updates to multiple apis and scripts
This commit is contained in:
parent
eb1a97541e
commit
b54120d88e
4 changed files with 182 additions and 160 deletions
18
app/api/_lib/auth.ts
Normal file
18
app/api/_lib/auth.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// app/api/_lib/auth.ts
|
||||
import { getUserBearerFromRequest } from "@/lib/directus";
|
||||
|
||||
export function requireBearer(req: Request): string {
|
||||
const bearer = getUserBearerFromRequest(req);
|
||||
if (!bearer) {
|
||||
const err: any = new Error("UNAUTHENTICATED");
|
||||
err.status = 401;
|
||||
throw err;
|
||||
}
|
||||
return bearer;
|
||||
}
|
||||
|
||||
export function jsonError(e: any, fallback = 500) {
|
||||
const status = e?.status ?? fallback;
|
||||
const body = { error: e?.message || "ERROR", detail: e?.detail ?? String(e) };
|
||||
return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue