mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Refactor the endpoints to share common code
This commit is contained in:
@@ -87,19 +87,13 @@ export const createAdmin = async (req: Request<{}, {}, CreateAdminBody>, res: Re
|
|||||||
if (
|
if (
|
||||||
!(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level) && groupsIsValid)
|
!(typeof name === "string" && typeof password == "string" && isPermissionLevel(permission_level) && groupsIsValid)
|
||||||
) {
|
) {
|
||||||
return res.status(400).json({
|
return res.status(400).json(
|
||||||
type: "error",
|
generateInvalidBodyError({
|
||||||
payload: {
|
name: DataType.STRING,
|
||||||
message: "The body of your request did not conform to the requirements",
|
password: DataType.STRING,
|
||||||
schema: {
|
permission_level: DataType.PERMISSION_LEVEL,
|
||||||
body: {
|
})
|
||||||
name: "string",
|
);
|
||||||
password_level: "string",
|
|
||||||
permission_level: "'STANDARD' | 'ELEVATED'",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const password_hash = await argon2.hash(password, { type: argon2.argon2id });
|
const password_hash = await argon2.hash(password, { type: argon2.argon2id });
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { authClient } from "../lib/redis";
|
|||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import argon2 from "argon2";
|
import argon2 from "argon2";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
|
import { DataType, generateInvalidBodyError } from "./common";
|
||||||
|
|
||||||
const JWT_SECRET = process.env.JWT_SECRET || "secret";
|
const JWT_SECRET = process.env.JWT_SECRET || "secret";
|
||||||
const TOKEN_EXPIRY = "4 days";
|
const TOKEN_EXPIRY = "4 days";
|
||||||
@@ -39,12 +40,7 @@ export const authenticateUser = async (req: Request<{}, {}, AuthenticateUserBody
|
|||||||
const { name, password } = req.body || {};
|
const { name, password } = req.body || {};
|
||||||
|
|
||||||
if (name == null || password == null) {
|
if (name == null || password == null) {
|
||||||
return res.status(400).json({
|
return res.status(400).json(generateInvalidBodyError({ name: DataType.STRING, password: DataType.STRING }));
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: "The body must contain 'name' and 'password' attributes of type string",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await prisma.admin.findFirst({ where: { name }, include: { groups: true } });
|
const user = await prisma.admin.findFirst({ where: { name }, include: { groups: true } });
|
||||||
|
|||||||
Reference in New Issue
Block a user