From fcb2000e9495fc23c6bfac0107e567f5b42a228f Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Tue, 18 Apr 2023 16:40:37 +0530 Subject: [PATCH] resetpassword for user and supplier --- src/handlers/userHandler.js | 84 ++++++++++++++++++++++++++++++++++--- src/routes/usersRoute.js | 28 +++++++++++++ 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/handlers/userHandler.js b/src/handlers/userHandler.js index 776649a8..4282213c 100644 --- a/src/handlers/userHandler.js +++ b/src/handlers/userHandler.js @@ -18,6 +18,7 @@ var twilioAuthToken = "7710db3aea89b94027155e6ae774b688"; // Your Auth Token fro const libphonenumberjs = require("libphonenumber-js"); const emailValidator = require("email-validator"); +const { Supplier } = require('../models/supplier'); //function to encrypt password. //used bcrypt module. @@ -588,27 +589,27 @@ exports.resetPassword = async (req, reply) => { user = checkFormEncoding.user; } phone = user.phone; - resetPasswordCode = user.resetPasswordCode; + phoneVerificationCode = user.phoneVerificationCode; newPassword = user.newPassword; hash = await bcryptPassword(newPassword); - console.log(user); + console.log("user===", user); // check if user exists in the system. If user exists , display message that // username is not available userExists = await User.findOne({ phone: phone, - passwordResetCode: resetPasswordCode, + // phoneVerificationCode: phoneVerificationCode, }); - console.log(userExists); + console.log("userExists===", userExists); if (userExists) { // update the phoneVerified flag to true. const filter = { phone: phone, - passwordResetCode: resetPasswordCode, + //phoneVerificationCode: phoneVerificationCode, }; - console.log(filter); + console.log("filter", filter); const update = { $set: { @@ -649,6 +650,77 @@ exports.resetPassword = async (req, reply) => { } }; +exports.resetPasswordSupplier = async (req, reply) => { + try { + console.log(" in reset Password method"); + var user = req.body; + // Handle if the user data is supplied via a url encoded form + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + user = checkFormEncoding.user; + } + phone = user.phone; + phoneVerificationCode = user.phoneVerificationCode; + newPassword = user.newPassword; + hash = await bcryptPassword(newPassword); + + console.log("user===", user); + // check if user exists in the system. If user exists , display message that + // username is not available + + userExists = await Supplier.findOne({ + phone: phone, + // phoneVerificationCode: phoneVerificationCode, + }); + console.log("userExists===", userExists); + if (userExists) { + // update the phoneVerified flag to true. + + const filter = { + phone: phone, + //phoneVerificationCode: phoneVerificationCode, + }; + console.log("filter", filter); + + const update = { + $set: { + "services.password.bcrypt": hash, + oneTimePasswordSetFlag: false, + }, + }; + console.log(update); + const doc = await Supplier.updateOne(filter, update); + // updatedUser = await User.findOne({ phone: phone }); + + if (doc) { + reply.send('{"armintatankdata":{"error":false,"passwordReset": true}}'); + } else { + error = { + armintatankdata: { + error: true, + code: 10007, + message: "10007 - Password Reset code entered cannot be validated.", + }, + }; + req.body.regError = error; + reply.send(error); + } + } else { + error = { + armintatankdata: { + error: true, + code: 10008, + message: "10008 - Either Phone or Temporary Code is Invalid.", + }, + }; + req.body.regError = error; + reply.send(error); + } + } catch (err) { + throw boom.boomify(err); + } +}; + exports.resetPasswordFromAdmin = async (req, reply) => { try { var { userId } = req.body; diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index bcdc6e12..809353aa 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -284,6 +284,34 @@ module.exports = function (fastify, opts, next) { // onResponse: (request,reply) => {validationHandler.resetPassword(request,reply)} }); + fastify.route({ + method: "POST", + url: "/api/resetpasswordsupplier", + schema: { + tags: ["Supplier"], + description: "This is for Supplier Reset Password.", + summary: "This is for Supplier Reset Password.", + body: { + type: "object", + required: ["phone", "resetPasswordCode", "newPassword"], + properties: { + phone: { type: "string" }, + resetPasswordCode: { type: "string" }, + newPassword: { type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: [validationHandler], + handler: validationHandler.resetPasswordSupplier, + // onResponse: (request,reply) => {validationHandler.resetPassword(request,reply)} + }); + + fastify.route({ method: "POST", url: "/api/resetPasswordFromAdmin",