From 77ef5052d63e590bc938868a4c698d40f1eea35a Mon Sep 17 00:00:00 2001 From: Moon Patel Date: Wed, 6 Nov 2024 14:58:22 +0530 Subject: [PATCH] refactor: simplify email service - simplify email service by using smtp values from env vars - add more context to errors for better debugging --- internal/services/auth_service.go | 15 +++++++--- internal/services/email_service.go | 45 +++++++++++++++--------------- internal/utils/email_helpers.go | 4 --- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/internal/services/auth_service.go b/internal/services/auth_service.go index 88bced9..9993a90 100644 --- a/internal/services/auth_service.go +++ b/internal/services/auth_service.go @@ -1,6 +1,7 @@ package services import ( + "fmt" "keizer-auth-api/internal/models" "keizer-auth-api/internal/repositories" "keizer-auth-api/internal/utils" @@ -18,23 +19,29 @@ func NewAuthService(userRepo *repositories.UserRepository) *AuthService { func (as *AuthService) RegisterUser(userRegister *validators.UserRegister) error { passwordHash, err := utils.HashPassword(userRegister.Password) if err != nil { - return err + return fmt.Errorf("failed to hash password: %w", err) } otp, err := utils.GenerateOTP() if err != nil { - return err + return fmt.Errorf("failed to generate OTP: %w", err) } err = SendOTPEmail(userRegister.Email, otp) if err != nil { - return err + return fmt.Errorf("failed to send OTP email: %w", err) } - return as.userRepo.CreateUser(&models.User{ + err = as.userRepo.CreateUser(&models.User{ Email: userRegister.Email, FirstName: userRegister.FirstName, LastName: userRegister.LastName, PasswordHash: passwordHash, Otp: otp, }) + + if err != nil { + return fmt.Errorf("failed to create user: %w", err) + } + + return nil } diff --git a/internal/services/email_service.go b/internal/services/email_service.go index 6fb00c1..37b8b03 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -1,51 +1,52 @@ package services import ( - "errors" - "keizer-auth-api/internal/utils" + "fmt" "net/smtp" "os" ) var ( - from = os.Getenv("EMAIL_FROM") + smtpHost = os.Getenv("SMTP_HOST") + smtpPort = os.Getenv("SMTP_PORT") + smtpUser = os.Getenv("SMTP_USER") + smtpPassword = os.Getenv("SMTP_PASSWORD") + from = os.Getenv("EMAIL_FROM") ) type EmailService struct { + host string + port string + user string + pass string + from string } func NewEmailService() *EmailService { - return &EmailService{} + return &EmailService{host: smtpHost, port: smtpPort, user: smtpUser, pass: smtpPassword, from: from} } -func (es *EmailService) SendEmail(to string, message string) error { - appEnv := os.Getenv("APP_ENV") +func (es *EmailService) SendEmail(to string, subject string, body string) error { + message := []byte("To: " + to + "\r\n" + + "Subject: " + subject + "\r\n" + + "\r\n" + + body + "\r\n") - if appEnv == "local" { - // Send email using mailhog - sendEmailUsingMailHog(to, message) - } else if appEnv == "production" { - // Send email using SendGrid - } else { - return errors.New("invalid APP_ENV") + var auth smtp.Auth + if es.pass != "" { + auth = smtp.PlainAuth("", es.user, es.pass, es.host) } - return nil -} + err := smtp.SendMail(es.host+":"+es.port, auth, es.from, []string{to}, message) -func sendEmailUsingMailHog(to string, message string) error { - smtpHost := "localhost" - smtpPort := "1025" - err := smtp.SendMail(smtpHost+":"+smtpPort, nil, from, []string{to}, []byte(message)) if err != nil { - return err + return fmt.Errorf("failed to send email: %w", err) } return nil } func SendOTPEmail(to string, otp string) error { emailService := NewEmailService() - message := utils.ConstructOTPMail("OTP Verification", "Your OTP is "+otp) - err := emailService.SendEmail(to, message) + err := emailService.SendEmail(to, "OTP Verification", "Your OTP is "+otp) if err != nil { return err } diff --git a/internal/utils/email_helpers.go b/internal/utils/email_helpers.go index 322b81f..45d2aab 100644 --- a/internal/utils/email_helpers.go +++ b/internal/utils/email_helpers.go @@ -20,7 +20,3 @@ func GenerateOTP() (string, error) { } return otp, nil } - -func ConstructOTPMail(subject string, body string) string { - return "Subject: " + subject + "\r\n\r\n" + body -}