package tfa import ( "fmt" "strings" "time" "clapclap/internal/auth/jwt" "clapclap/internal/email" "clapclap/internal/env" "clapclap/internal/uuid" ) // DispatchVerificationEmail generates a token and sends the verification email. // Notice it returns an error instead of logging it directly. func DispatchVerificationEmail(userID uuid.UUID, userEmail string) error { secret := env.C.JWT_SECRET clientURL := strings.TrimRight(env.C.API_URL, "/") tokenString, err := jwt.GenerateActionToken(secret, userID, "email_verification", 24*time.Hour) if err != nil { return fmt.Errorf("failed to generate verification token: %w", err) } verifyLink := fmt.Sprintf("%s/auth/verify-email?token=%s", clientURL, tokenString) htmlContent := fmt.Sprintf(`

Welcome to clapclap! 🚀

Please click the button below to verify your email address. This link will expire in 24 hours.

Verify Email

If you didn't request this, you can safely ignore this email.

`, verifyLink) emailObj := email.Email{ To: userEmail, Subject: "Verify your Account - clapclap", Content: htmlContent, } if err := email.Send(emailObj); err != nil { return fmt.Errorf("smtp error sending verification email: %w", err) } return nil } func DispatchPasswordResetEmail(userID uuid.UUID, userEmail string) error { secret := env.C.JWT_SECRET clientURL := strings.TrimRight(env.C.CLIENT_URL, "/") tokenString, err := jwt.GenerateActionToken(secret, userID, "password_reset", 15*time.Minute) if err != nil { return fmt.Errorf("failed to generate password reset token: %w", err) } resetLink := fmt.Sprintf("%s/change-password?token=%s", clientURL, tokenString) htmlContent := fmt.Sprintf(`

Password Reset Request

We received a request to reset your password. Click the button below to choose a new one. This link will expire in 15 minutes.

Reset Password

If you didn't request a password reset, you can safely ignore this email.

`, resetLink) emailObj := email.Email{ To: userEmail, Subject: "Password Reset Request", Content: htmlContent, } if err := email.Send(emailObj); err != nil { return fmt.Errorf("smtp error sending password reset email: %w", err) } return nil }