2020-07-18 19:36:46 +02:00
|
|
|
import cors from 'cors';
|
2020-07-28 08:52:14 +02:00
|
|
|
import session from './session';
|
|
|
|
import auth from './auth';
|
2020-07-18 19:36:46 +02:00
|
|
|
|
|
|
|
export function use(middleware) {
|
|
|
|
return (req, res) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
middleware(req, res, result => {
|
|
|
|
if (result instanceof Error) {
|
|
|
|
return reject(result);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-26 01:31:07 +02:00
|
|
|
export const useCors = use(cors());
|
2020-07-28 08:52:14 +02:00
|
|
|
|
|
|
|
export const useSession = use(async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
req.session = await session(req);
|
|
|
|
} catch {
|
|
|
|
return res.status(400).end();
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
export const useAuth = use(async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
req.auth = await auth(req);
|
|
|
|
} catch {
|
|
|
|
return res.status(401).end();
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|