mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Extract code for error messages for better reusability
+ Create function for generating permission errors
This commit is contained in:
@@ -1,31 +1,37 @@
|
|||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import { AdminLevel } from "@prisma/client";
|
||||||
|
|
||||||
|
const AUTH_ERROR = {
|
||||||
|
type: "failure",
|
||||||
|
payload: {
|
||||||
|
message: "The server was not able to validate your credentials; Please try again later",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const createInsufficientPermissionsError = (required: AdminLevel = "ELEVATED") => ({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: "You do not have sufficient permissions to use this feature",
|
||||||
|
required_level: required,
|
||||||
|
},
|
||||||
|
_links: [
|
||||||
|
{
|
||||||
|
rel: "authentication",
|
||||||
|
href: "/api/authentication",
|
||||||
|
type: "POST",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
// requires: auth(elevated)
|
// requires: auth(elevated)
|
||||||
export const getAllAdmins = async (req: Request, res: Response) => {
|
export const getAllAdmins = async (req: Request, res: Response) => {
|
||||||
if (!req.auth?.isAuthenticated) {
|
if (!req.auth?.isAuthenticated) {
|
||||||
return res.status(500).json({
|
return res.status(500).json(AUTH_ERROR);
|
||||||
type: "failure",
|
|
||||||
payload: {
|
|
||||||
message: "The server was not able to validate your credentials; Please try again later",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.auth.permission_level !== "ELEVATED") {
|
if (req.auth.permission_level !== "ELEVATED") {
|
||||||
return res.status(403).json({
|
return res.status(403).json(createInsufficientPermissionsError());
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: "You do not have sufficient permissions to use this feature",
|
|
||||||
},
|
|
||||||
_links: [
|
|
||||||
{
|
|
||||||
rel: "authentication",
|
|
||||||
href: "/api/authentication",
|
|
||||||
type: "POST",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add exception handling
|
// TODO: Add exception handling
|
||||||
|
|||||||
Reference in New Issue
Block a user