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 };
|
||||||
@@ -1,6 +1,23 @@
|
|||||||
/** @type {import('next').NextConfig} */
|
/** @type {import('next').NextConfig} */
|
||||||
const nextConfig = {
|
const nextConfig = {
|
||||||
reactStrictMode: true,
|
reactStrictMode: true,
|
||||||
|
async headers() {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
source: "/api/:path*",
|
||||||
|
headers: [
|
||||||
|
{
|
||||||
|
key: "Content-Type",
|
||||||
|
value: "application/vnd.api+json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "x-testing",
|
||||||
|
value: "true"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = nextConfig
|
module.exports = nextConfig
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
import db from "../../../../lib/db";
|
import db from "../../../../lib/db";
|
||||||
import { withErrorHandling, withSession } from "../../../../lib/apiHelpers";
|
import { authenticatedJsonApi } from "../../../../lib/jsonapi";
|
||||||
import models from "../../../../lib/models";
|
import models from "../../../../lib/models";
|
||||||
|
|
||||||
const handler = withErrorHandling(
|
const handler = authenticatedJsonApi(async (req, res, session) => {
|
||||||
withSession(async (req, res, session) => {
|
const { method } = req;
|
||||||
const { method } = req;
|
const { user } = session;
|
||||||
const { user } = session;
|
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
await getEntity(req, res, user);
|
await getEntity(req, res, user);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
res.setHeader("Allow", ["GET"]);
|
res.setHeader("Allow", ["GET"]);
|
||||||
res.status(405).end(`Method ${method} Not Allowed`);
|
res.status(405).send(`Method ${method} Not Allowed`);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const getEntity = async (req, res, user) => {
|
const getEntity = async (req, res, user) => {
|
||||||
const { entityName, entityId } = req.query;
|
const { entityName, entityId } = req.query;
|
||||||
|
|
||||||
if (!models[entityName]) {
|
if (!models[entityName]) {
|
||||||
res.status(404).end();
|
res.status(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +28,7 @@ const getEntity = async (req, res, user) => {
|
|||||||
|
|
||||||
if (!model.validateId(entityId)) {
|
if (!model.validateId(entityId)) {
|
||||||
console.warn(model.validateId.errors);
|
console.warn(model.validateId.errors);
|
||||||
res.status(404).end();
|
res.status(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +46,7 @@ const getEntity = async (req, res, user) => {
|
|||||||
if (entities.Items.length == 1) {
|
if (entities.Items.length == 1) {
|
||||||
res.status(200).json(entities.Items[0]);
|
res.status(200).json(entities.Items[0]);
|
||||||
} else {
|
} else {
|
||||||
res.status(404).end();
|
res.status(404);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,29 @@
|
|||||||
import db from "../../../../lib/db";
|
import db from "../../../../lib/db";
|
||||||
import { withErrorHandling, withSession } from "../../../../lib/apiHelpers";
|
import { authenticatedJsonApi } from "../../../../lib/jsonapi";
|
||||||
import models from "../../../../lib/models";
|
import models from "../../../../lib/models";
|
||||||
|
|
||||||
const handler = withErrorHandling(
|
const handler = authenticatedJsonApi(async (req, res, session) => {
|
||||||
withSession(async (req, res, session) => {
|
const { method } = req;
|
||||||
const { method } = req;
|
const { user } = session;
|
||||||
const { user } = session;
|
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
await getEntities(req, res, user);
|
await getEntities(req, res, user);
|
||||||
break;
|
break;
|
||||||
case "POST":
|
case "POST":
|
||||||
await createEntity(req, res, user);
|
await createEntity(req, res, user);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
res.setHeader("Allow", ["GET", "POST"]);
|
res.setHeader("Allow", ["GET", "POST"]);
|
||||||
res.status(405).end(`Method ${method} Not Allowed`);
|
res.status(405).json(`Method ${method} Not Allowed`);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const getEntities = async (req, res, user) => {
|
const getEntities = async (req, res, user) => {
|
||||||
const { entityName } = req.query;
|
const { entityName } = req.query;
|
||||||
|
|
||||||
if (!models[entityName]) {
|
if (!models[entityName]) {
|
||||||
res.status(404).end();
|
res.status(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +44,7 @@ const createEntity = async (req, res, user) => {
|
|||||||
const { entityName } = req.query;
|
const { entityName } = req.query;
|
||||||
|
|
||||||
if (!models[entityName]) {
|
if (!models[entityName]) {
|
||||||
res.status(404).end();
|
res.status(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import auth0 from "../../../lib/auth0";
|
import auth0 from "../../../lib/auth0";
|
||||||
import { withErrorHandling } from "../../../lib/apiHelpers";
|
|
||||||
|
|
||||||
const callback = withErrorHandling(async (req, res) => {
|
const callback = async (req, res) => {
|
||||||
await auth0.handleCallback(req, res);
|
await auth0.handleCallback(req, res);
|
||||||
});
|
};
|
||||||
|
|
||||||
export default callback;
|
export default callback;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import db from "../../../lib/db";
|
import db from "../../../lib/db";
|
||||||
import { withErrorHandling } from "../../../lib/apiHelpers";
|
|
||||||
|
|
||||||
const handler = withErrorHandling(async (req, res) => {
|
const handler = async (req, res) => {
|
||||||
const { method } = req;
|
const { method } = req;
|
||||||
|
|
||||||
switch (method) {
|
switch (method) {
|
||||||
@@ -11,7 +10,7 @@ const handler = withErrorHandling(async (req, res) => {
|
|||||||
res.setHeader("Allow", ["GET"]);
|
res.setHeader("Allow", ["GET"]);
|
||||||
res.status(405).end(`Method ${method} Not Allowed`);
|
res.status(405).end(`Method ${method} Not Allowed`);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
const get = async (req, res) => {
|
const get = async (req, res) => {
|
||||||
const params = {
|
const params = {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import auth0 from "../../../lib/auth0";
|
import auth0 from "../../../lib/auth0";
|
||||||
import { withErrorHandling } from "../../../lib/apiHelpers";
|
|
||||||
|
|
||||||
const login = withErrorHandling(async (req, res) => {
|
const login = async (req, res) => {
|
||||||
await auth0.handleLogin(req, res);
|
await auth0.handleLogin(req, res);
|
||||||
});
|
};
|
||||||
|
|
||||||
export default login;
|
export default login;
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import auth0 from "../../../lib/auth0";
|
import auth0 from "../../../lib/auth0";
|
||||||
import { withErrorHandling } from "../../../lib/apiHelpers";
|
|
||||||
|
|
||||||
const me = withErrorHandling(async (req, res) => {
|
const me = async (req, res) => {
|
||||||
await auth0.handleProfile(req, res);
|
await auth0.handleProfile(req, res);
|
||||||
});
|
};
|
||||||
|
|
||||||
export default me;
|
export default me;
|
||||||
|
|||||||
Reference in New Issue
Block a user