refactor: cleans up authentication duplication between page and api routes
This commit is contained in:
parent
384ca71f89
commit
5cc122bf39
5 changed files with 33 additions and 54 deletions
|
|
@ -1,5 +1,3 @@
|
|||
import { isAdmin } from './auth';
|
||||
|
||||
export function jsonResponse(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
|
|
@ -10,10 +8,3 @@ export function jsonResponse(data: unknown, status = 200): Response {
|
|||
export function errorResponse(message: string, status: number): Response {
|
||||
return jsonResponse({ error: message }, status);
|
||||
}
|
||||
|
||||
export function requireAdmin(session: { user?: { id?: string } } | null): Response | null {
|
||||
if (!session?.user?.id || !isAdmin(session.user.id)) {
|
||||
return errorResponse('Unauthorized', 403);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue