package controllers import ( "clapclap/internal/apperr" "clapclap/internal/env" "clapclap/internal/ginctx" "net/http" "strings" "github.com/gin-gonic/gin" ) // VerifyEmail godoc // @Summary Verify Email // @Description Verifies a user's email using the token sent to their inbox // @Tags auth // @Produce json // @Param token query string true "JWT Verification Token" // @Success 302 {string} string "Redirects to Client URL" // @Failure 401 {object} swaggError401 // @Router /auth/verify-email [get] func VerifyEmail(c *gin.Context) { userID, _ := ginctx.GetUUID(c, "userID") repo, _ := ginctx.GetRepo(c, "repo") user, err := repo.GetUserByID(userID, nil) if err != nil { apperr.AbortWithError(c, apperr.NewDatabaseError(err, "User not found")) return } if user.IsVerified { c.JSON(http.StatusOK, gin.H{"message": "Email is already verified"}) return } user.IsVerified = true if err := repo.UpdateUser(user); err != nil { apperr.AbortWithError(c, apperr.NewDatabaseError(err, "Failed to verify email")) return } clientURL := strings.TrimRight(env.C.CLIENT_URL, "/") c.Redirect(http.StatusFound, clientURL+"/login?verified=true") }