2022-12-16 06:27:49 -08:00
|
|
|
import { inDevelopment } from '@/constants';
|
2022-12-07 06:13:36 -08:00
|
|
|
import type { RequestHandler } from 'express';
|
|
|
|
|
|
|
|
export const corsMiddleware: RequestHandler = (req, res, next) => {
|
|
|
|
if (inDevelopment && 'origin' in req.headers) {
|
|
|
|
// Allow access also from frontend when developing
|
|
|
|
res.header('Access-Control-Allow-Origin', req.headers.origin);
|
|
|
|
res.header('Access-Control-Allow-Credentials', 'true');
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
|
|
|
|
res.header(
|
|
|
|
'Access-Control-Allow-Headers',
|
|
|
|
'Origin, X-Requested-With, Content-Type, Accept, sessionid',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|