55 "errors"
66 "time"
77
8- "github.com/google/uuid"
9-
108 "github.com/CodeChefVIT/devsoc-backend-26/pkg/db"
119 "github.com/CodeChefVIT/devsoc-backend-26/pkg/redis"
1210 "github.com/CodeChefVIT/devsoc-backend-26/pkg/utils"
@@ -33,7 +31,11 @@ func SendOTP(email string) error {
3331 return errors .New ("otp already sent" )
3432 }
3533
36- otp := utils .GenerateOTP ()
34+ otp , err := utils .GenerateOTP ()
35+ if err != nil {
36+ return errors .New ("failed to generate otp" )
37+ }
38+
3739 hashed := utils .HashOTP (otp )
3840
3941 err = redis .Client .Set (
@@ -47,26 +49,36 @@ func SendOTP(email string) error {
4749 return err
4850 }
4951
50- println ("OTP:" , otp )
52+ err = utils .SendMail (email , otp )
53+ if err != nil {
54+ redis .Client .Del (redis .Ctx , key )
55+ return err
56+ }
57+
5158 return nil
5259}
5360
54-
5561func VerifyOTP (email , otp string ) (string , error ) {
5662 key := "otp:" + email
5763
5864 storedHash , err := redis .Client .Get (redis .Ctx , key ).Result ()
5965 if err != nil {
66+ if redis .IsKeyMissing (err ) {
67+ return "" , errors .New ("otp expired or invalid" )
68+ }
6069 return "" , err
6170 }
6271
6372 if utils .HashOTP (otp ) != storedHash {
64- return "" , err
73+ return "" , errors . New ( "invalid otp" )
6574 }
6675
6776 redis .Client .Del (redis .Ctx , key )
6877
69- userID := uuid .New ().String ()
78+ userID , err := db .Queries .GetUserIDByEmail (context .Background (), email )
79+ if err != nil {
80+ return "" , err
81+ }
7082
71- return utils .CreateJWT (userID , email )
83+ return utils .CreateJWT (userID . String () , email )
7284}
0 commit comments