Skip to content

Commit

Permalink
Merge pull request #21 from moonpatel/refactor
Browse files Browse the repository at this point in the history
refactor: simplify email service
  • Loading branch information
moonpatel authored Nov 6, 2024
2 parents 7cebffe + 77ef505 commit 1a8b7bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
15 changes: 11 additions & 4 deletions internal/services/auth_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"fmt"
"keizer-auth-api/internal/models"
"keizer-auth-api/internal/repositories"
"keizer-auth-api/internal/utils"
Expand All @@ -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
}
45 changes: 23 additions & 22 deletions internal/services/email_service.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
4 changes: 0 additions & 4 deletions internal/utils/email_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1a8b7bb

Please sign in to comment.