Remove testroutes; Update register endpoint; Add Redis lib

This commit is contained in:
Stefan-5422
2022-03-30 12:21:02 +02:00
parent 71e3a3c941
commit 8f9a8ebdea
9 changed files with 366 additions and 6 deletions
+46 -4
View File
@@ -1,9 +1,13 @@
import express from "express";
import { Request, Response } from "express";
import prisma from "../lib/prisma.lib";
import { Role } from "@prisma/client";
import { Role, Status } from "@prisma/client";
import { createDefaultError, createDefaultSucces } from "../lib/standardResponse";
import argon2 from "argon2";
import jwt from "jsonwebtoken";
import { emailClient } from "../lib/redis.lib";
const JWT_SECRET = process.env.jwtsecret || "secret";
interface registerBody {
name: string;
@@ -22,8 +26,6 @@ const ROLES: readonly string[] = [Role.ADMIN, Role.USER];
export const register = async (req: Request, res: Response) => {
const { name, password, role, organization } = req.body || {};
console.log(name);
if (
!(
typeof name === "string" &&
@@ -36,7 +38,6 @@ export const register = async (req: Request, res: Response) => {
createDefaultError("Request body does not match required parameters", {
name: "string",
password: "string",
role: "USER | ADMIN",
organization: "int",
})
);
@@ -48,12 +49,19 @@ export const register = async (req: Request, res: Response) => {
return res.status(404).json(createDefaultError(`Could not find organization with id ${organization}`, {}));
}
if (role ? ("USER" as Role) === Role.ADMIN : Role) {
if (req.auth?.role !== Role.ADMIN) {
return res.status(401).json(createDefaultError(`Unauthorized`, {}));
}
}
const user = await prisma.user.create({
data: {
email: name,
passwordSalt: "salty",
passwordHash: hash,
role: role as Role,
status: role === Role.ADMIN ? Status.UNVERIFIED : Status.ENABLED,
organization: { connect: { id: organization } || undefined },
},
select: {
@@ -63,5 +71,39 @@ export const register = async (req: Request, res: Response) => {
},
});
(await emailClient).set(name, "salty");
res.status(201).json(createDefaultSucces("User created succesfully", user));
};
export const authenthicate = async (req: Request, res: Response) => {
const { name, password } = req.body || {};
if (!(typeof name === "string" && typeof password === "string")) {
return res.status(400).json(
createDefaultError("Request body does not match required parameters", {
name: "string",
password: "string",
})
);
}
const hash = await argon2.hash("salty" + password + "salty");
const user = await prisma.user.findUnique({ where: { email: name } });
if (!user) {
return res.status(403).json(createDefaultError("Username or Password wrong", {}));
}
if (user.passwordHash !== hash) {
return res.status(403).json(createDefaultError("Username or Password wrong", {}));
}
const userjwt: authJwt = {
id: user.id,
role: user.role,
};
req.headers.authorization = jwt.sign(userjwt, JWT_SECRET);
};