1- import OTPStore from "/ models/OTPStore " ;
1+ import OTPStore from "# models/otpStore " ;
22import util , { logger } from "#util" ;
33import { 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
4646async 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
0 commit comments