mirror of
https://github.com/GithubOrgProjectPiza/server.git
synced 2026-09-04 08:36:10 +02:00
Fix auth system
This commit is contained in:
@@ -31,7 +31,7 @@ model Restaurant {
|
|||||||
|
|
||||||
model Pizza {
|
model Pizza {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
name String @unique
|
name String
|
||||||
description String?
|
description String?
|
||||||
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
|
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
|
||||||
restaurantId Int
|
restaurantId Int
|
||||||
|
|||||||
@@ -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 } }))) {
|
if (!(await prisma.organization.findUnique({ where: { id: organization } }))) {
|
||||||
return res.status(404).json(createDefaultError(`Could not find organization with 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 } });
|
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", {}));
|
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", {}));
|
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);
|
req.headers.authorization = jwt.sign(userjwt, JWT_SECRET);
|
||||||
|
|
||||||
|
res.status(200).json(createDefaultSucces("Logged in succesfully",{}))
|
||||||
};
|
};
|
||||||
|
|
||||||
export const verifyEmail = async (req: Request, res: Response) => {
|
export const verifyEmail = async (req: Request, res: Response) => {
|
||||||
|
|||||||
@@ -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: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
},
|
},
|
||||||
|
|||||||
+1
-5
@@ -2,7 +2,6 @@ import express, { NextFunction, Request, Response } from "express";
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
|
|
||||||
import adminRoutes from "./routes/auth.route";
|
import adminRoutes from "./routes/auth.route";
|
||||||
const testRoutes = require("./routes/testrouter.route");
|
|
||||||
const ordersRoutes = require("./routes/orders.route");
|
const ordersRoutes = require("./routes/orders.route");
|
||||||
const organizationsRoutes = require("./routes/organisations.route");
|
const organizationsRoutes = require("./routes/organisations.route");
|
||||||
const pizzasRoutes = require("./routes/pizzas.route");
|
const pizzasRoutes = require("./routes/pizzas.route");
|
||||||
@@ -27,17 +26,14 @@ app.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
|
||||||
app.use("/api/auth", adminRoutes);
|
app.use("/api/auth", adminRoutes);
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use("/tests", testRoutes);
|
|
||||||
app.use("/order", ordersRoutes);
|
app.use("/order", ordersRoutes);
|
||||||
app.use("/organization", organizationsRoutes);
|
app.use("/organization", organizationsRoutes);
|
||||||
app.use("/pizza", pizzasRoutes);
|
app.use("/pizza", pizzasRoutes);
|
||||||
app.use("/restaurant", restaurantsRoutes);
|
app.use("/restaurant", restaurantsRoutes);
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.listen(3000, () => {
|
||||||
console.log("Server is running on port 3000");
|
console.log("Server is running on port 3000");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import { register } from "../controller/auth.controller";
|
import { authenthicate, register } from "../controller/auth.controller";
|
||||||
import { getAuthenthication } from "../middleware/auth.middle";
|
import { getAuthenthication } from "../middleware/auth.middle";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.post("/register", getAuthenthication, register);
|
router.post("/register", getAuthenthication, register);
|
||||||
|
router.post("/authenthicate",authenthicate);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user