Add register endpoint & auth checker

This commit is contained in:
Stefan-5422
2022-03-09 13:18:40 +01:00
parent c6319edc31
commit 5944bb6f75
6 changed files with 5689 additions and 4 deletions
+5575 -4
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -21,7 +21,9 @@
"dependencies": {
"@prisma/client": "^3.10.0",
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
"prisma": "^3.10.0",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
+47
View File
@@ -0,0 +1,47 @@
import express from "express";
import { Request, Response } from "express";
import prisma from "../lib/prisma.lib";
import { Role } from "@prisma/client";
import { createDefaultError } from "../lib/standardError";
interface registerBody {
name: string;
password: string;
role: string;
organization?: number;
}
const ROLES: readonly Role[] = [Role.ADMIN, Role.USER];
export const register = async (req: Request<{}, {}, registerBody>, res: Response) => {
const { name, password, role, organization } = req.body || {};
if (!(typeof name === "string" && typeof password === "string" && ROLES.includes(role as any))) {
return res.status(400).json(
createDefaultError("Request body does not match required parameters", {
name: "string",
password: "string",
role: "role = user",
})
);
}
//TODO: Generate hash
if (!(await prisma.organization.findUnique({ where: { id: organization } }))) {
return res.status(404).json(createDefaultError(`Could not find organization with id ${organization}`, {}));
}
const user = await prisma.user.create({
data: {
email: name,
passwordSalt: "",
passwordHash: password,
role: role as Role,
organization: {connect: {id: organization} || undefined},
},
select: {
id:true,email:true,role:true
}
});
};
+16
View File
@@ -0,0 +1,16 @@
export const createDefaultError = (message: string, context: any) => ({
"type": "error",
"payload": {
"message": message,
"context": context,
}
})
export const createDefaultSucces = (message: string, context: any) => ({
"type": "success",
"payload": {
"message": message,
"context": context,
}
})
+41
View File
@@ -0,0 +1,41 @@
import { NextFunction, Request, Response } from "express";
import { createDefaultError } from "../lib/standardResponse";
import jwt, { JwtPayload } from "jsonwebtoken"
import { authJwt } from "../controller/auth.controller";
import prisma from "../lib/prisma.lib";
const verifyAuthorizationFormat = (authorization: string) => /^Bearer .+$/.test(authorization);
const getBearerToken = (authorization: string) => authorization.slice(7);
const JWT_SECRET = process.env.jwtsecret || "secret";
export const requireAuthentication = async (req: Request, res: Response, next: NextFunction) => {
const {authorization} = req.headers;
if(!authorization) {
return res.status(403).json(createDefaultError("The request did not contain a authorization header",null))
}
if(!verifyAuthorizationFormat(authorization)) {
return res.status(400).json(createDefaultError("Malformed Bearer Token", {"format": "Bearer <token>"}))
}
let payload_: string | JwtPayload;
try {
payload_ = jwt.verify(getBearerToken(authorization),JWT_SECRET);
}catch(e) {
return res.status(403).json(createDefaultError("The Token could not be verified, it might be expiered",null))
}
const payload = payload_ as authJwt; //TODO: write a payload in the controller which is responsible for typechecking the payload
const {id} = payload;
const user = await prisma.user.findUnique({where: {id}});
//TODO: set req.auth to something more sensible
next();
}
+8
View File
@@ -0,0 +1,8 @@
import { Router } from "express";
const router = Router();
router.post("/auth");
router.post("/register");