Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: some errors #33

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=auth@keizer.com
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
Loading