Fix auth system

This commit is contained in:
Stefan-5422
2022-05-04 11:46:36 +02:00
parent 6099f58771
commit 7f52f8c30e
5 changed files with 14 additions and 11 deletions
+1 -1
View File
@@ -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
+9 -3
View File
@@ -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) => {
+1 -1
View File
@@ -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,
},
+1 -5
View File
@@ -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");
});
+2 -1
View File
@@ -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;