implemented change password functionality

This commit is contained in:
Daniel
2023-05-22 23:59:24 +03:30
parent 59e4dc471f
commit e774f41d37
4 changed files with 149 additions and 14 deletions
+26 -2
View File
@@ -7,10 +7,9 @@ import { prisma } from "@/lib/api/db";
import { AccountSettings } from "@/types/global";
import fs from "fs";
import path from "path";
import bcrypt from "bcrypt";
export default async function (user: AccountSettings, userId: number) {
console.log(console.log(user.profilePic));
const profilePic = user.profilePic;
if (profilePic && profilePic !== "DELETE") {
@@ -50,6 +49,31 @@ export default async function (user: AccountSettings, userId: number) {
},
});
if (user.newPassword && user.oldPassword) {
const saltRounds = 10;
const requestOldHashedPassword = bcrypt.hashSync(
user.oldPassword,
saltRounds
);
console.log(requestOldHashedPassword);
if (bcrypt.compareSync(user.oldPassword, updatedUser.password)) {
const newHashedPassword = bcrypt.hashSync(user.newPassword, saltRounds);
await prisma.user.update({
where: {
id: userId,
},
data: {
password: newHashedPassword,
},
});
} else {
return { response: "Passwords do not match.", status: 403 };
}
}
const { password, ...unsensitiveInfo } = updatedUser;
return { response: unsensitiveInfo, status: 200 };