From 3138d6b52ca1d703a5ea4a94d331a6cbe12e505e Mon Sep 17 00:00:00 2001 From: Fred Shema <51389166+fredshema@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:25:05 +0200 Subject: [PATCH] ref: account password reset (#363) --- src/resolvers/userResolver.ts | 25 +++++++++++++++++++++++++ src/schema/index.ts | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/src/resolvers/userResolver.ts b/src/resolvers/userResolver.ts index c3bfeb67..c7292977 100644 --- a/src/resolvers/userResolver.ts +++ b/src/resolvers/userResolver.ts @@ -1175,6 +1175,31 @@ const resolvers: any = { throw new Error('Oopps! something went wrong') } }, + async changeUserPassword( + _: any, + { currentPassword, newPassword, confirmPassword, token }: any, + context: any + ) { + const { userId } = verify(token, SECRET) as JwtPayload + if (newPassword === confirmPassword) { + const user: any = await User.findById(userId) + if (!user) { + throw new Error("User doesn't exist! ") + } + + if (bcrypt.compareSync(currentPassword, user.password)) { + user.password = newPassword + await user.save() + return 'Your password was reset successfully! ' + } else { + throw new Error('Current Password is incorrect') + } + } else if (newPassword !== confirmPassword) { + throw new Error('New password mismatch!') + } else { + throw new Error('Oopps! something went wrong') + } + }, }, User: { diff --git a/src/schema/index.ts b/src/schema/index.ts index 15b72a26..37d77761 100644 --- a/src/schema/index.ts +++ b/src/schema/index.ts @@ -419,6 +419,12 @@ const Schema = gql` confirmPassword: String! token: String! ): String! + changeUserPassword( + currentPassword: String! + newPassword: String! + confirmPassword: String! + token: String! + ): String! addActiveRepostoOrganization(name: String!, repoUrl: String!): Organization!