mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 23:33:59 +02:00
Remove testroutes; Update register endpoint; Add Redis lib
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user