28 lines
555 B
JavaScript
28 lines
555 B
JavaScript
import auth0 from "./auth0";
|
|
|
|
function withErrorHandling(handler) {
|
|
return async (req, res) => {
|
|
try {
|
|
await handler(req, res);
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(error.status || 500).end(error.message);
|
|
}
|
|
};
|
|
}
|
|
|
|
function withSession(handler) {
|
|
return async (req, res) => {
|
|
const session = await auth0.getSession(req, res);
|
|
|
|
if (!session || !session.user) {
|
|
res.status(401).end();
|
|
return;
|
|
}
|
|
|
|
await handler(req, res, session);
|
|
}
|
|
}
|
|
|
|
export { withErrorHandling, withSession };
|