handle auth correctly and added change password

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-10-28 22:59:33 +02:00
parent e0cc9ef83b
commit aa5c881ec6
7 changed files with 149 additions and 44 deletions

View File

@@ -5,7 +5,7 @@ import {
protectedProcedure,
} from "@/server/api/trpc";
import { db } from "@/server/db";
import { hashPassword } from "@/server/services/hash.service";
import { hashPassword, verifyPassword } from "@/server/services/hash.service";
export const userRouter = createTRPCRouter({
current: protectedProcedure.query(({ ctx }) => {
@@ -47,14 +47,10 @@ export const userRouter = createTRPCRouter({
}
})
if(user.password !== input.oldPassword) {
if(!(await verifyPassword(input.oldPassword, user.password))) {
throw new Error('Old password is incorrect')
}
if(user.password === input.password) {
throw new Error('New password cannot be the same as old password')
}
return db.user.update({
where: {
id: ctx.session.user.id