Reframe AI-tlas as a public research journal with authenticator-protected editorial workspace
Validate / validate (push) Successful in 3m15s
Validate / validate (push) Successful in 3m15s
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { COOKIE, validSession } from './editor-session';
|
||||
|
||||
export class HttpError extends Error {
|
||||
constructor(public status: number, message: string) { super(message); }
|
||||
@@ -10,6 +11,9 @@ type Handler = (req: NextApiRequest) => Promise<unknown>;
|
||||
/** Dispatch by HTTP method; a handler returns the JSON to send, or throws HttpError. */
|
||||
export function route(handlers: Partial<Record<Method, Handler>>) {
|
||||
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
res.setHeader('Cache-Control', 'private, no-store');
|
||||
if (!await validSession(req.cookies[COOKIE])) return res.status(401).json({ error: 'Editor authentication required' });
|
||||
if (req.method !== 'GET' && req.headers.origin !== process.env.EDITOR_ORIGIN) return res.status(403).json({ error: 'Invalid origin' });
|
||||
const h = handlers[req.method as Method];
|
||||
if (!h) {
|
||||
res.setHeader('Allow', Object.keys(handlers).join(', '));
|
||||
|
||||
Reference in New Issue
Block a user