From 7f52f8c30eceaa787c10505a71eb67e0f75402f0 Mon Sep 17 00:00:00 2001 From: Stefan-5422 <32109571+Stefan-5422@users.noreply.github.com> Date: Wed, 4 May 2022 11:46:36 +0200 Subject: [PATCH] Fix auth system --- prisma/schema.prisma | 2 +- src/controller/auth.controller.ts | 12 +++++++++--- src/controller/organisations.controller.ts | 2 +- src/index.ts | 6 +----- src/routes/auth.route.ts | 3 ++- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 885dbaf..63ccf26 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -31,7 +31,7 @@ model Restaurant { model Pizza { id Int @id @default(autoincrement()) - name String @unique + name String description String? restaurant Restaurant @relation(fields: [restaurantId], references: [id]) restaurantId Int diff --git a/src/controller/auth.controller.ts b/src/controller/auth.controller.ts index 3c1f9fb..e80f278 100644 --- a/src/controller/auth.controller.ts +++ b/src/controller/auth.controller.ts @@ -45,7 +45,9 @@ export const register = async (req: Request, res: Response) => { ); } - const hash = await argon2.hash("salty" + password + "salty"); + const hash = await argon2.hash("salty" + password + "salty",{ + type: argon2.argon2id + }); if (!(await prisma.organization.findUnique({ where: { id: organization } }))) { return res.status(404).json(createDefaultError(`Could not find organization with id ${organization}`, {})); @@ -94,7 +96,9 @@ export const authenthicate = async (req: Request, res: Response) => { ); } - const hash = await argon2.hash("salty" + password + "salty"); + const hash = await argon2.hash("salty" + password + "salty",{ + type: argon2.argon2id + }); const user = await prisma.user.findUnique({ where: { email: name } }); @@ -102,7 +106,7 @@ export const authenthicate = async (req: Request, res: Response) => { return res.status(403).json(createDefaultError("Username or Password wrong", {})); } - if (user.passwordHash !== hash) { + if (await argon2.verify(user.passwordHash,hash)) { return res.status(403).json(createDefaultError("Username or Password wrong", {})); } @@ -112,6 +116,8 @@ export const authenthicate = async (req: Request, res: Response) => { }; req.headers.authorization = jwt.sign(userjwt, JWT_SECRET); + + res.status(200).json(createDefaultSucces("Logged in succesfully",{})) }; export const verifyEmail = async (req: Request, res: Response) => { diff --git a/src/controller/organisations.controller.ts b/src/controller/organisations.controller.ts index 4af0f52..6f850cd 100644 --- a/src/controller/organisations.controller.ts +++ b/src/controller/organisations.controller.ts @@ -77,7 +77,7 @@ export const getOrganization = async (req: Request, res: Response) => { }); } - const organization = await prisma.restaurant.findUnique({ + const organization = await prisma.organization.findUnique({ where: { id: id, }, diff --git a/src/index.ts b/src/index.ts index e9819c6..0e282ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,6 @@ import express, { NextFunction, Request, Response } from "express"; import dotenv from "dotenv"; import adminRoutes from "./routes/auth.route"; -const testRoutes = require("./routes/testrouter.route"); const ordersRoutes = require("./routes/orders.route"); const organizationsRoutes = require("./routes/organisations.route"); const pizzasRoutes = require("./routes/pizzas.route"); @@ -27,17 +26,14 @@ app.use((err: any, req: Request, res: Response, next: NextFunction) => { } }); - async function main() { - app.use("/api/auth", adminRoutes); app.use(express.json()); - app.use("/tests", testRoutes); app.use("/order", ordersRoutes); app.use("/organization", organizationsRoutes); app.use("/pizza", pizzasRoutes); app.use("/restaurant", restaurantsRoutes); - + app.listen(3000, () => { console.log("Server is running on port 3000"); }); diff --git a/src/routes/auth.route.ts b/src/routes/auth.route.ts index efa7ad2..a1636fe 100644 --- a/src/routes/auth.route.ts +++ b/src/routes/auth.route.ts @@ -1,9 +1,10 @@ import { Router } from "express"; -import { register } from "../controller/auth.controller"; +import { authenthicate, register } from "../controller/auth.controller"; import { getAuthenthication } from "../middleware/auth.middle"; const router = Router(); router.post("/register", getAuthenthication, register); +router.post("/authenthicate",authenthicate); export default router;