From 36e1bc328d7fb9dbc99ea845362b83a73906c75c Mon Sep 17 00:00:00 2001 From: bkawk Date: Sun, 5 Feb 2023 20:24:02 +0000 Subject: [PATCH] send email for forgot password --- api/handlers/forgotPassword.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/api/handlers/forgotPassword.go b/api/handlers/forgotPassword.go index 00e781c..ca6c011 100644 --- a/api/handlers/forgotPassword.go +++ b/api/handlers/forgotPassword.go @@ -1,10 +1,12 @@ package handlers import ( + "bkawk/go-echo/api/emails" "bkawk/go-echo/api/models" "context" "fmt" "net/http" + "os" "time" "github.com/labstack/echo/v4" @@ -42,8 +44,17 @@ func ForgotPasswordPost(c echo.Context) error { return echo.NewHTTPError(http.StatusTooManyRequests, fmt.Sprintf("Try again in %d minutes and %d seconds", int(waitTime.Minutes()), int(waitTime.Seconds())%60)) } - // send email logic - fmt.Println("Sending forgot password email to", u.Email) + // Get the verification URL from the environment + resetEmailUrl := os.Getenv("RESET_EMAIL_URL") + if resetEmailUrl == "" { + return fmt.Errorf("environment variable not set: VERIFY_URL") + } + // Send welcome email + emailError := emails.SendResetPasswordEmail(u.Email, resetEmailUrl+"?verificationCode="+u.VerificationCode) + if emailError != nil { + fmt.Println(emailError) + return c.JSON(http.StatusInternalServerError, echo.Map{"error": emailError}) + } user.ForgotPassword = time.Now().Unix() if _, err := collection.ReplaceOne(ctx, filter, user); err != nil {