1- import util , { logger } from "#util" ;
1+ import OTPStore from "#models/otpStore" ;
2+ import 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 {
@@ -17,7 +16,7 @@ async function login(req, res) {
1716 userDetails . token = token ;
1817 res . json ( { res : "welcome" , user : userDetails } ) ;
1918 } catch ( error ) {
20- logger . error ( "Error while login" , error )
19+ logger . error ( "Error while login" , error ) ;
2120 if ( error . name === "UserDoesNotExist" ) {
2221 res . status ( 403 ) ;
2322 res . json ( { err : "Incorrect ID password" } ) ;
@@ -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 . update ( { uid } , { otp } ) ;
4039 util . sendOTP ( emailId , otp ) ;
4140 res . json ( { res : "otp sent to emailID" } ) ;
4241 } else {
@@ -46,12 +45,13 @@ async function sendOTP(req, res) {
4645
4746async function resetPassword ( req , res ) {
4847 const { uid, otp, password } = req . body ;
49- if ( otpStore [ uid ] === otp ) {
48+ const storedOtp = await OTPStore . read ( { uid } ) ;
49+ if ( storedOtp [ 0 ] . otp === `${ otp } ` ) {
5050 try {
5151 await updatePassword ( uid , password ) ;
5252 res . json ( { res : "successfully updated password" } ) ;
5353 } catch ( error ) {
54- logger . log ( "Error while updating" , error )
54+ logger . log ( "Error while updating" , error ) ;
5555 res . status ( 500 ) ;
5656 if ( error . name === "UpdateError" ) res . json ( { err : "Something went wrong while updating password" } ) ;
5757 else res . json ( { err : "something went wrong" } ) ;
0 commit comments