Add try/catch in change password mutation (#3337)
* Revert "Update change password w/ try/catch"
This reverts commit 186532ef2d.
* Update changePassword.ts
* Update packages/generator/templates/app/app/auth/mutations/changePassword.ts
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
* Update changePassword.ts
* Apply suggestions from code review
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { NotFoundError, SecurePassword, resolver } from "blitz"
|
||||
import { NotFoundError, SecurePassword, resolver, AuthenticationError } from "blitz"
|
||||
import db from "db"
|
||||
import { authenticateUser } from "./login"
|
||||
import { ChangePassword } from "../validations"
|
||||
@@ -10,7 +10,15 @@ export default resolver.pipe(
|
||||
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } })
|
||||
if (!user) throw new NotFoundError()
|
||||
|
||||
await authenticateUser(user.email, currentPassword)
|
||||
try {
|
||||
await authenticateUser(user.email, currentPassword)
|
||||
} catch (error: any) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
throw new Error("Invalid Password")
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
|
||||
const hashedPassword = await SecurePassword.hash(newPassword.trim())
|
||||
await db.user.update({
|
||||
|
||||
Reference in New Issue
Block a user