Skip to content

Commit

Permalink
Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol-2 authored and github-actions[bot] committed Mar 15, 2024
1 parent 19e11fd commit 0d2a029
Show file tree
Hide file tree
Showing 13 changed files with 688 additions and 702 deletions.
10 changes: 5 additions & 5 deletions backend/src/models/ChangePasswordReq.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface ChangePasswordReq{
old_password: string,
new_password: string,
repeat_password: string
}
export interface ChangePasswordReq {
old_password: string;
new_password: string;
repeat_password: string;
}
73 changes: 41 additions & 32 deletions backend/src/routes/usersRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,49 +306,58 @@ usersRouter.put("/:userId", async (req: Request, res: OkErrorResponse) => {
}
});

usersRouter.post("/:userId/change-password", async (req: Request, res: OkErrorResponse) => {
try {
const userId = req.params.userId;
const passwords: ChangePasswordReq = req.body;

const {old_password, new_password, repeat_password} = passwords
usersRouter.post(
"/:userId/change-password",
async (req: Request, res: OkErrorResponse) => {
try {
const userId = req.params.userId;
const passwords: ChangePasswordReq = req.body;

const session = driver.session();
const user = await userExists(session, { id: userId });
const { old_password, new_password, repeat_password } = passwords;

if (!user) {
return userNotFoundRes(res);
}
const session = driver.session();
const user = await userExists(session, { id: userId });

// check validation of the old password
if (!user) {
return userNotFoundRes(res);
}

const match: boolean = await bcrypt.compare(old_password, user.password);
// check validation of the old password

if(!match) {
return res.status(400).json({ status: "error", errors: {"error":""} });
}
const match: boolean = await bcrypt.compare(old_password, user.password);

// check if there are two same password
if(new_password !== repeat_password){
return res.status(400).json({ status: "error", errors: {"error":"Passwords don't match"} });
if (!match) {
return res
.status(400)
.json({ status: "error", errors: { "error": "" } });
}

const passwordHashed = await bcrypt.hash(new_password, 10);
// check if there are two same password
if (new_password !== repeat_password) {
return res
.status(400)
.json({
status: "error",
errors: { "error": "Passwords don't match" },
});
}

const passwordHashed = await bcrypt.hash(new_password, 10);

const updatedUser = { ...user, password: passwordHashed };
await session.run(`MATCH (u:User {id: $userId}) SET u=$user`, {
userId,
user: updatedUser,
});
await session.close();
const updatedUser = { ...user, password: passwordHashed };
await session.run(`MATCH (u:User {id: $userId}) SET u=$user`, {
userId,
user: updatedUser,
});
await session.close();

return res.json({ status: "ok" });
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
}
});
return res.json({ status: "ok" });
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
}
},
);

usersRouter.delete("/:userId", async (req: Request, res: OkErrorResponse) => {
try {
Expand Down
Loading

0 comments on commit 0d2a029

Please sign in to comment.