Skip to content

Commit 112bdf7

Browse files
committed
refactored and restructured the code
1 parent 88da452 commit 112bdf7

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

controller/auth.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import OTPStore from "/models/OTPStore";
1+
import OTPStore from "#models/otpStore";
22
import util, {logger} from "#util";
33
import { authenticateUser, userExists, updatePassword } from "#services/user";
44

@@ -35,7 +35,7 @@ async function sendOTP(req, res) {
3535
const { uid, emailId } = req.body;
3636
if (await userExists(uid, emailId)) {
3737
const otp = Math.floor(1000 + Math.random() * 9000);
38-
await OTPStore.findOneAndUpdate({ uid }, { otp: otp }, { upsert: true });
38+
await OTPStore.update({uid: uid}, {otp: otp});
3939
util.sendOTP(emailId, otp);
4040
res.json({ res: "otp sent to emailID" });
4141
} else {
@@ -45,20 +45,19 @@ async function sendOTP(req, res) {
4545

4646
async function resetPassword(req, res) {
4747
const { uid, otp, password } = req.body;
48-
try{
49-
const otpData=await OTPStore.find({uid});
50-
if(otpData.otp ===otp ){
51-
await updatePassword(uid,password)
52-
res.json({res:"successfully updated password"})
53-
}
54-
else {
55-
res.json({ err: "Incorrect OTP" });
48+
const storedOtp = await OTPStore.read({uid: uid});
49+
if (storedOtp[0].otp === `${otp}`) {
50+
try {
51+
await updatePassword(uid, password);
52+
res.json({ res: "successfully updated password" });
53+
} catch (error) {
54+
logger.log("Error while updating", error)
55+
res.status(500);
56+
if (error.name === "UpdateError") res.json({ err: "Something went wrong while updating password" });
57+
else res.json({ err: "something went wrong" });
5658
}
57-
58-
}
59-
catch(error){
60-
console.log(error)
61-
res.json({res:"Something is wrong"})
59+
} else {
60+
res.json({ err: "incorrect otp" });
6261
}
6362
}
6463

models/otpStore.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
import connector from "#models/databaseUtil";
2-
const {Schema}=connector;
32

4-
const otpStoreSchema=new Schema({
5-
uid:{type:String,unique:true,required:true},
6-
otp:{type:String,required:true,unique:true}
7-
})
3+
const otpStoreSchema = {
4+
uid: { type: String, unique: true, required: true },
5+
otp: { type: String, unique: true, required: true }
6+
}
87

9-
const OTPStore=connector.model("OTPStore",otpStoreSchema)
8+
const OTPStore = connector.model("OTPStore", otpStoreSchema)
9+
10+
async function remove(filter) {
11+
const res = await OTPStore.findOneAndDelete(filter);
12+
return res;
13+
}
14+
15+
async function create(uid, otp) {
16+
const otpStore = new OTPStore({
17+
uid,
18+
otp
19+
});
20+
const otpDoc = await otpStore.save();
21+
return otpDoc;
22+
}
23+
24+
async function read(filter, limit = 1) {
25+
const otpData = await OTPStore.find(filter).limit(limit);
26+
return otpData;
27+
}
28+
29+
async function update(filter, updateObject) {
30+
const otpDoc = await OTPStore.findOneAndUpdate(filter, updateObject, { upsert: true, new: true });
31+
return otpDoc;
32+
}
33+
34+
35+
export default {
36+
create, read, update, remove,
37+
};
1038

0 commit comments

Comments
 (0)