1+ import OTPStore from "/models/OTPStore" ;
12import util , { logger } from "#util" ;
23import { authenticateUser , userExists , updatePassword } from "#services/user" ;
34
4- const otpStore = { } ;
5-
65async function login ( req , res ) {
76 const { id, password } = req . body ;
87 try {
@@ -36,7 +35,7 @@ async function sendOTP(req, res) {
3635 const { uid, emailId } = req . body ;
3736 if ( await userExists ( uid , emailId ) ) {
3837 const otp = Math . floor ( 1000 + Math . random ( ) * 9000 ) ;
39- otpStore [ uid ] = otp ;
38+ await OTPStore . findOneAndUpdate ( { uid } , { otp : otp } , { upsert : true } ) ;
4039 util . sendOTP ( emailId , otp ) ;
4140 res . json ( { res : "otp sent to emailID" } ) ;
4241 } else {
@@ -46,20 +45,23 @@ async function sendOTP(req, res) {
4645
4746async function resetPassword ( req , res ) {
4847 const { uid, otp, password } = req . body ;
49- if ( otpStore [ uid ] === 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" } ) ;
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" } )
5853 }
59- } else {
60- res . json ( { err : "incorrect otp" } ) ;
54+ else {
55+ res . json ( { err : "Incorrect OTP" } ) ;
56+ }
57+
58+ }
59+ catch ( error ) {
60+ console . log ( error )
61+ res . json ( { res :"Something is wrong" } )
6162 }
6263}
64+
6365
6466export default {
6567 validateUser, sendOTP, resetPassword, login,
0 commit comments