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 . update ( { uid : uid } , { otp : otp } ) ;
4039 util . sendOTP ( emailId , otp ) ;
4140 res . json ( { res : "otp sent to emailID" } ) ;
4241 } else {
@@ -46,7 +45,8 @@ 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 : uid } ) ;
49+ if ( storedOtp [ 0 ] . otp === `${ otp } ` ) {
5050 try {
5151 await updatePassword ( uid , password ) ;
5252 res . json ( { res : "successfully updated password" } ) ;
@@ -60,6 +60,7 @@ async function resetPassword(req, res) {
6060 res . json ( { err : "incorrect otp" } ) ;
6161 }
6262}
63+
6364
6465export default {
6566 validateUser, sendOTP, resetPassword, login,
0 commit comments