fix(auth): accept email or identifier login payload
This commit is contained in:
parent
1a449b7824
commit
cf305d096e
3 changed files with 55 additions and 6 deletions
|
|
@ -4,6 +4,7 @@ import "time"
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
|
Identifier string `json:"identifier"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
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
|
// 1. Find User by Email
|
||||||
fmt.Printf("[LOGIN DEBUG] Searching for user: %s\n", input.Email)
|
fmt.Printf("[LOGIN DEBUG] Searching for user: %s\n", loginID)
|
||||||
user, err := uc.userRepo.FindByEmail(ctx, input.Email)
|
user, err := uc.userRepo.FindByEmail(ctx, loginID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[LOGIN DEBUG] Error finding user: %v\n", err)
|
fmt.Printf("[LOGIN DEBUG] Error finding user: %v\n", err)
|
||||||
return nil, errors.New("invalid credentials")
|
return nil, errors.New("invalid credentials")
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue