Skip to content

Commit

Permalink
fix: some errors
Browse files Browse the repository at this point in the history
- db query, changed uuid key to id
- added env vars for email service in .env.example
- fixed segfault errors
  • Loading branch information
moonpatel committed Nov 12, 2024
1 parent 8d25173 commit b17b0f5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
9 changes: 4 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ REDIS_PORT=6379
REDIS_PASSWORD=password

# STMP
SMTP_HOST=
SMTP_PORT=
EMAIL_FROM=
SMPT_USER=
SMPT_PASS=
SMTP_HOST=localhost
SMTP_PORT=1025
SMTP_USER=keizer
MAIL_FROM=[email protected]
7 changes: 5 additions & 2 deletions internal/app/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Container struct {
DB database.Service
AuthService *services.AuthService
SessionService *services.SessionService
EmailService *services.EmailService
}

var (
Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions internal/controllers/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"})
}
Expand Down
9 changes: 4 additions & 5 deletions internal/repositories/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"keizer-auth/internal/models"

"github.com/google/uuid"
"gorm.io/gorm"
)

Expand All @@ -20,15 +19,15 @@ 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")
}
return nil, fmt.Errorf("error in getting user: %w", result.Error)
}

return &user, nil
return user, nil
}
2 changes: 1 addition & 1 deletion internal/services/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/services/email_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions internal/services/session_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"keizer-auth/internal/utils"
"time"

"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)

Expand All @@ -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)
Expand Down

0 comments on commit b17b0f5

Please sign in to comment.