From cf305d096eebb1d47f8bf88e112f89fb952253e9 Mon Sep 17 00:00:00 2001 From: Redbull Deployer Date: Wed, 4 Mar 2026 21:39:52 -0600 Subject: [PATCH] fix(auth): accept email or identifier login payload --- backend/internal/core/dto/user_auth.go | 9 +++-- backend/internal/core/usecases/auth/login.go | 13 ++++++- .../internal/core/usecases/auth/login_test.go | 39 +++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/backend/internal/core/dto/user_auth.go b/backend/internal/core/dto/user_auth.go index 8bdce0b..6f3b080 100644 --- a/backend/internal/core/dto/user_auth.go +++ b/backend/internal/core/dto/user_auth.go @@ -2,10 +2,11 @@ package dto import "time" -type LoginRequest struct { - Email string `json:"email"` - Password string `json:"password"` -} +type LoginRequest struct { + Email string `json:"email"` + Identifier string `json:"identifier"` + Password string `json:"password"` +} type AuthResponse struct { Token string `json:"token"` diff --git a/backend/internal/core/usecases/auth/login.go b/backend/internal/core/usecases/auth/login.go index 873f2e3..d435538 100644 --- a/backend/internal/core/usecases/auth/login.go +++ b/backend/internal/core/usecases/auth/login.go @@ -24,9 +24,18 @@ func NewLoginUseCase(userRepo ports.UserRepository, authService ports.AuthServic } func (uc *LoginUseCase) Execute(ctx context.Context, input dto.LoginRequest) (*dto.AuthResponse, error) { + loginID := strings.TrimSpace(input.Email) + if loginID == "" { + loginID = strings.TrimSpace(input.Identifier) + } + + if loginID == "" { + return nil, errors.New("invalid credentials") + } + // 1. Find User by Email - fmt.Printf("[LOGIN DEBUG] Searching for user: %s\n", input.Email) - user, err := uc.userRepo.FindByEmail(ctx, input.Email) + fmt.Printf("[LOGIN DEBUG] Searching for user: %s\n", loginID) + user, err := uc.userRepo.FindByEmail(ctx, loginID) if err != nil { fmt.Printf("[LOGIN DEBUG] Error finding user: %v\n", err) return nil, errors.New("invalid credentials") diff --git a/backend/internal/core/usecases/auth/login_test.go b/backend/internal/core/usecases/auth/login_test.go index 4db7555..2d4e5e5 100644 --- a/backend/internal/core/usecases/auth/login_test.go +++ b/backend/internal/core/usecases/auth/login_test.go @@ -99,3 +99,42 @@ func TestLoginUseCase_Execute_StatusCheck(t *testing.T) { }) } } + +func TestLoginUseCase_Execute_AcceptsIdentifierFallback(t *testing.T) { + authSvc := &MockAuthService{ + GenerateTokenFunc: func(userID, tenantID string, roles []string) (string, error) { + return "mock-token", nil + }, + } + + calledWith := "" + userRepo := &MockUserRepo{ + FindByEmailFunc: func(ctx context.Context, email string) (*entity.User, error) { + calledWith = email + return &entity.User{ + ID: "user-123", + Email: "lol@gohorsejobs.com", + PasswordHash: "hashed_pass", + Status: entity.UserStatusActive, + Roles: []entity.Role{}, + }, nil + }, + } + + uc := auth.NewLoginUseCase(userRepo, authSvc) + + resp, err := uc.Execute(context.Background(), dto.LoginRequest{ + Identifier: "lol@gohorsejobs.com", + Password: "password", + }) + + if err != nil { + t.Fatalf("expected success with identifier, got error: %v", err) + } + if resp == nil { + t.Fatal("expected response, got nil") + } + if calledWith != "lol@gohorsejobs.com" { + t.Fatalf("expected repo lookup using identifier value, got %q", calledWith) + } +}