From b01b0b2d563a1db29f4cc51c133d20475776672c Mon Sep 17 00:00:00 2001 From: Shivam Tiwari Date: Tue, 20 Jun 2023 23:50:31 +0530 Subject: [PATCH 1/3] created an schema for the otp store and made the required changes to store the otp into the db --- controller/auth.js | 30 ++++++++++++++++-------------- models/otpStore.js | 10 ++++++++++ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 models/otpStore.js diff --git a/controller/auth.js b/controller/auth.js index a5281a2..fe16a5e 100644 --- a/controller/auth.js +++ b/controller/auth.js @@ -1,8 +1,7 @@ +import OTPStore from "/models/OTPStore"; import util, {logger} from "#util"; import { authenticateUser, userExists, updatePassword } from "#services/user"; -const otpStore = {}; - async function login(req, res) { const { id, password } = req.body; try { @@ -36,7 +35,7 @@ async function sendOTP(req, res) { const { uid, emailId } = req.body; if (await userExists(uid, emailId)) { const otp = Math.floor(1000 + Math.random() * 9000); - otpStore[uid] = otp; + await OTPStore.findOneAndUpdate({ uid }, { otp: otp }, { upsert: true }); util.sendOTP(emailId, otp); res.json({ res: "otp sent to emailID" }); } else { @@ -46,20 +45,23 @@ async function sendOTP(req, res) { async function resetPassword(req, res) { const { uid, otp, password } = req.body; - if (otpStore[uid] === otp) { - try { - await updatePassword(uid, password); - res.json({ res: "successfully updated password" }); - } catch (error) { - logger.log("Error while updating", error) - res.status(500); - if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" }); - else res.json({ err: "something went wrong" }); + try{ + const otpData=await OTPStore.find({uid}); + if(otpData.otp ===otp ){ + await updatePassword(uid,password) + res.json({res:"successfully updated password"}) } - } else { - res.json({ err: "incorrect otp" }); + else { + res.json({ err: "Incorrect OTP" }); + } + + } + catch(error){ + console.log(error) + res.json({res:"Something is wrong"}) } } + export default { validateUser, sendOTP, resetPassword, login, diff --git a/models/otpStore.js b/models/otpStore.js new file mode 100644 index 0000000..1486aaa --- /dev/null +++ b/models/otpStore.js @@ -0,0 +1,10 @@ +import connector from "#models/databaseUtil"; +const {Schema}=connector; + +const otpStoreSchema=new Schema({ + otpID:{type:String,unique:true,required:true}, + otp:{type:String,required:true,unique:true} +}) + +const OTPStore=connector.model("OTPStore",otpStoreSchema) + From 88da4526785e6780330500c408439efb7616c78f Mon Sep 17 00:00:00 2001 From: Shivam Tiwari Date: Tue, 20 Jun 2023 23:57:33 +0530 Subject: [PATCH 2/3] made Otpid to uid in otpschema --- models/otpStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/otpStore.js b/models/otpStore.js index 1486aaa..a03adbd 100644 --- a/models/otpStore.js +++ b/models/otpStore.js @@ -2,7 +2,7 @@ import connector from "#models/databaseUtil"; const {Schema}=connector; const otpStoreSchema=new Schema({ - otpID:{type:String,unique:true,required:true}, + uid:{type:String,unique:true,required:true}, otp:{type:String,required:true,unique:true} }) From 112bdf7edfb39e60652b08c2d41c347fc943a942 Mon Sep 17 00:00:00 2001 From: Hitansh Doshi Date: Wed, 28 Jun 2023 00:01:42 +0530 Subject: [PATCH 3/3] refactored and restructured the code --- controller/auth.js | 29 ++++++++++++++--------------- models/otpStore.js | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/controller/auth.js b/controller/auth.js index fe16a5e..d38860f 100644 --- a/controller/auth.js +++ b/controller/auth.js @@ -1,4 +1,4 @@ -import OTPStore from "/models/OTPStore"; +import OTPStore from "#models/otpStore"; import util, {logger} from "#util"; import { authenticateUser, userExists, updatePassword } from "#services/user"; @@ -35,7 +35,7 @@ async function sendOTP(req, res) { const { uid, emailId } = req.body; if (await userExists(uid, emailId)) { const otp = Math.floor(1000 + Math.random() * 9000); - await OTPStore.findOneAndUpdate({ uid }, { otp: otp }, { upsert: true }); + await OTPStore.update({uid: uid}, {otp: otp}); util.sendOTP(emailId, otp); res.json({ res: "otp sent to emailID" }); } else { @@ -45,20 +45,19 @@ async function sendOTP(req, res) { async function resetPassword(req, res) { const { uid, otp, password } = req.body; - try{ - const otpData=await OTPStore.find({uid}); - if(otpData.otp ===otp ){ - await updatePassword(uid,password) - res.json({res:"successfully updated password"}) - } - else { - res.json({ err: "Incorrect OTP" }); + const storedOtp = await OTPStore.read({uid: uid}); + if (storedOtp[0].otp === `${otp}`) { + try { + await updatePassword(uid, password); + res.json({ res: "successfully updated password" }); + } catch (error) { + logger.log("Error while updating", error) + res.status(500); + if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" }); + else res.json({ err: "something went wrong" }); } - - } - catch(error){ - console.log(error) - res.json({res:"Something is wrong"}) + } else { + res.json({ err: "incorrect otp" }); } } diff --git a/models/otpStore.js b/models/otpStore.js index a03adbd..45648b8 100644 --- a/models/otpStore.js +++ b/models/otpStore.js @@ -1,10 +1,38 @@ import connector from "#models/databaseUtil"; -const {Schema}=connector; -const otpStoreSchema=new Schema({ - uid:{type:String,unique:true,required:true}, - otp:{type:String,required:true,unique:true} -}) +const otpStoreSchema = { + uid: { type: String, unique: true, required: true }, + otp: { type: String, unique: true, required: true } +} -const OTPStore=connector.model("OTPStore",otpStoreSchema) +const OTPStore = connector.model("OTPStore", otpStoreSchema) + +async function remove(filter) { + const res = await OTPStore.findOneAndDelete(filter); + return res; +} + +async function create(uid, otp) { + const otpStore = new OTPStore({ + uid, + otp + }); + const otpDoc = await otpStore.save(); + return otpDoc; +} + +async function read(filter, limit = 1) { + const otpData = await OTPStore.find(filter).limit(limit); + return otpData; +} + +async function update(filter, updateObject) { + const otpDoc = await OTPStore.findOneAndUpdate(filter, updateObject, { upsert: true, new: true }); + return otpDoc; +} + + +export default { + create, read, update, remove, +};