fix(auth): accept email or identifier login payload

This commit is contained in:
Redbull Deployer 2026-03-04 21:39:52 -06:00
parent 1a449b7824
commit cf305d096e
3 changed files with 55 additions and 6 deletions

View file

@ -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"`

View file

@ -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")

View file

@ -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)
}
}