|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/gin-gonic/gin" |
| 7 | + |
| 8 | + "github.com/sdslabs/nymeria/internal/smtp" |
| 9 | + "github.com/sdslabs/nymeria/internal/utils" |
| 10 | +) |
| 11 | + |
| 12 | +func HandleGetVerificationFlow(c *gin.Context) { |
| 13 | + csrfToken, err := utils.GenerateCSRFToken(c.GetHeader("X-User-ID")) // TODO: Get user ID from session |
| 14 | + if err != nil { |
| 15 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 16 | + "status": "error", |
| 17 | + "message": "Failed to generate CSRF token", |
| 18 | + }) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + c.JSON(http.StatusOK, gin.H{ |
| 23 | + "status": "success", |
| 24 | + "message": "Email verification flow initiated", |
| 25 | + "csrf_token": csrfToken, |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +func HandlePostVerificationCodeFlow(c *gin.Context) { |
| 30 | + var req VerifyEmailRequest |
| 31 | + err := c.ShouldBindJSON(&req) |
| 32 | + if err != nil { |
| 33 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 34 | + "status": "error", |
| 35 | + "message": "Invalid request body", |
| 36 | + }) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if req.Email == "" { |
| 41 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 42 | + "status": "error", |
| 43 | + "message": "Email is required", |
| 44 | + }) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + otp, err := smtp.SendOTPHandler(req.Email, req.IsIITRCheck) |
| 49 | + if err != nil { |
| 50 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 51 | + "status": "error", |
| 52 | + "message": err.Error(), |
| 53 | + }) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + c.JSON(http.StatusOK, gin.H{ |
| 58 | + "status": "success", |
| 59 | + "message": "Email verification code sent", |
| 60 | + "data": gin.H{ |
| 61 | + "otp": otp, |
| 62 | + }, |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func HandlePostVerifyEmailFlow(c *gin.Context) { |
| 67 | + var req VerifyEmailCodeRequest |
| 68 | + err := c.ShouldBindJSON(&req) |
| 69 | + if err != nil { |
| 70 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 71 | + "status": "error", |
| 72 | + "message": "Invalid request body", |
| 73 | + }) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if req.Email == "" { |
| 78 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 79 | + "status": "error", |
| 80 | + "message": "Email is required", |
| 81 | + }) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + if req.Code == "" { |
| 86 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 87 | + "status": "error", |
| 88 | + "message": "Code is required", |
| 89 | + }) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + err = smtp.VerifyOTPHandler(req.Email, req.Code) |
| 94 | + if err != nil { |
| 95 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 96 | + "status": "error", |
| 97 | + "message": err.Error(), |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + c.JSON(http.StatusOK, gin.H{ |
| 102 | + "status": "success", |
| 103 | + "message": "Email verified", |
| 104 | + }) |
| 105 | +} |
0 commit comments