This commit is contained in:
2022-08-27 08:14:10 -05:00
parent f65ae56f74
commit 696ca6a393
9 changed files with 101 additions and 77 deletions

View File

@@ -1,27 +0,0 @@
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 };

42
web/lib/jsonapi.js Normal file
View File

@@ -0,0 +1,42 @@
import auth0 from "./auth0";
async function jsonApi(handler) {
var unauthenticationHandler = async (req, res) => {
try {
await handler(req, res);
} catch (error) {
handleError(res, error);
}
res.end();
};
return unauthenticationHandler;
}
function authenticatedJsonApi(handler) {
var authenticatedHandler = async (req, res) => {
try {
const session = await auth0.getSession(req, res);
if (!session || !session.user) {
res.status(401);
} else {
await handler(req, res, session);
}
} catch (error) {
handleError(res, error);
}
res.end();
};
return authenticatedHandler;
}
function handleError(res, error) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
export { jsonApi, authenticatedJsonApi };