Progress
This commit is contained in:
@@ -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
42
web/lib/jsonapi.js
Normal 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 };
|
||||
Reference in New Issue
Block a user