diff --git a/.env.example b/.env.example index 13d3228f..8b85875d 100644 --- a/.env.example +++ b/.env.example @@ -20,8 +20,7 @@ REDIS_PORT=6379 REDIS_PASSWORD=password # STMP -SMTP_HOST= -SMTP_PORT= -EMAIL_FROM= -SMPT_USER= -SMPT_PASS= \ No newline at end of file +SMTP_HOST=localhost +SMTP_PORT=1025 +SMTP_USER=keizer +MAIL_FROM=auth@keizer.com \ No newline at end of file diff --git a/internal/app/container.go b/internal/app/container.go index c22145f8..54120306 100644 --- a/internal/app/container.go +++ b/internal/app/container.go @@ -12,6 +12,7 @@ type Container struct { DB database.Service AuthService *services.AuthService SessionService *services.SessionService + EmailService *services.EmailService } var ( @@ -28,10 +29,12 @@ func GetContainer() *Container { userRepo := repositories.NewUserRepository(gormDB) redisRepo := repositories.NewRedisRepository(rds) authService := services.NewAuthService(userRepo, redisRepo) + sessionService := services.NewSessionService(redisRepo, userRepo) container = &Container{ - DB: db, - AuthService: authService, + DB: db, + AuthService: authService, + SessionService: sessionService, } }) return container diff --git a/internal/controllers/auth_controller.go b/internal/controllers/auth_controller.go index ba7c1e81..57280325 100644 --- a/internal/controllers/auth_controller.go +++ b/internal/controllers/auth_controller.go @@ -2,14 +2,12 @@ package controllers import ( "errors" - "fmt" "keizer-auth/internal/services" "keizer-auth/internal/utils" "keizer-auth/internal/validators" "github.com/gofiber/fiber/v2" - "github.com/google/uuid" "gorm.io/gorm" ) @@ -76,11 +74,7 @@ func (ac *AuthController) VerifyOTP(c *fiber.Ctx) error { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "OTP not valid"}) } - parsedUuid, err := uuid.Parse(verifyOtpBody.Id) - if err != nil { - return fmt.Errorf("error parsing uuid %w", err) - } - sessionId, err := ac.sessionService.CreateSession(parsedUuid) + sessionId, err := ac.sessionService.CreateSession(verifyOtpBody.Id) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to create session"}) } diff --git a/internal/repositories/user_repository.go b/internal/repositories/user_repository.go index 00c6f3b1..11783741 100644 --- a/internal/repositories/user_repository.go +++ b/internal/repositories/user_repository.go @@ -4,7 +4,6 @@ import ( "fmt" "keizer-auth/internal/models" - "github.com/google/uuid" "gorm.io/gorm" ) @@ -20,9 +19,9 @@ func (r *UserRepository) CreateUser(user *models.User) error { return r.db.Create(user).Error } -func (r *UserRepository) GetUser(uuid uuid.UUID) (*models.User, error) { - var user models.User - result := r.db.First(&user, uuid.String()) +func (r *UserRepository) GetUser(uuid string) (*models.User, error) { + user := new(models.User) + result := r.db.First(&user, "id = ?", uuid) if result.Error != nil { if result.Error == gorm.ErrRecordNotFound { return nil, fmt.Errorf("user not found") @@ -30,5 +29,5 @@ func (r *UserRepository) GetUser(uuid uuid.UUID) (*models.User, error) { return nil, fmt.Errorf("error in getting user: %w", result.Error) } - return &user, nil + return user, nil } diff --git a/internal/services/auth_service.go b/internal/services/auth_service.go index ae5ad746..678d3ca6 100644 --- a/internal/services/auth_service.go +++ b/internal/services/auth_service.go @@ -55,7 +55,7 @@ func (as *AuthService) RegisterUser(userRegister *validators.SignUpUser) error { } func (as *AuthService) VerifyOTP(verifyOtpBody *validators.VerifyOTP) (bool, error) { - val, err := as.redisRepo.Get(verifyOtpBody.Email) + val, err := as.redisRepo.Get("registration-verification-otp-" + verifyOtpBody.Email) if err != nil { if err == redis.Nil { return false, fmt.Errorf("otp expired") diff --git a/internal/services/email_service.go b/internal/services/email_service.go index a94dece7..e920f40d 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -11,7 +11,7 @@ var ( smtpPort = os.Getenv("SMTP_PORT") smtpUser = os.Getenv("SMTP_USER") smtpPassword = os.Getenv("SMTP_PASSWORD") - from = os.Getenv("EMAIL_FROM") + from = os.Getenv("MAIL_FROM") ) type EmailService struct { diff --git a/internal/services/session_service.go b/internal/services/session_service.go index 1d3b5d61..b77c1e16 100644 --- a/internal/services/session_service.go +++ b/internal/services/session_service.go @@ -8,7 +8,6 @@ import ( "keizer-auth/internal/utils" "time" - "github.com/google/uuid" "github.com/redis/go-redis/v9" ) @@ -17,11 +16,11 @@ type SessionService struct { userRepo *repositories.UserRepository } -func NewSessionService(redisRepo *repositories.RedisRepository) *SessionService { - return &SessionService{redisRepo: redisRepo} +func NewSessionService(redisRepo *repositories.RedisRepository, userRepo *repositories.UserRepository) *SessionService { + return &SessionService{redisRepo: redisRepo, userRepo: userRepo} } -func (ss *SessionService) CreateSession(uuid uuid.UUID) (string, error) { +func (ss *SessionService) CreateSession(uuid string) (string, error) { sessionId, err := utils.GenerateSessionID() if err != nil { return "", fmt.Errorf("error in generating session %w", err)