Authentication
The authentication system follows Clean Architecture and CQRS principles, providing clear separation of concerns and testability.
Authentication Flow
Login
- Controller dispatches
AuthenticateUserCommand - Handler verifies credentials via
PasswordHasherInterface - Handler dispatches
UserAuthenticatedEvent - Event listener sets user ID in session
- Redirect to protected area
Logout
- Controller dispatches
LogoutUserCommand - Handler dispatches
UserLogoutEvent - Event listener clears session
- Redirect to login page
Session Service
The SessionServiceInterface centralizes session management:
$this->sessionService->setCurrentUserId($user->id());
$this->sessionService->addFlashMessage('success', 'Welcome back!');
User Context Middleware
The UserContextMiddleware adds user information to request attributes:
$isAuthenticated = $request->getAttribute('is_authenticated', false);
$userId = $request->getAttribute('user_id');
Security Best Practices
Password Handling
The Password Hasher plugin handles secure hashing:
$hashedPassword = $passwordHasher->hash($plainPassword);
$isValid = $passwordHasher->verify($plainPassword, $hashedPassword);
Session Protection
- Regenerate session ID after authentication
- Use HttpOnly cookies (prevents XSS access)
- Use SameSite=Lax (basic CSRF protection)
Error Messages
Generic messages prevent user enumeration:
See Also
- Commands & Handlers - AuthenticateUserCommand flow
- Domain Events - UserAuthenticatedEvent
- Password Hasher - Credential verification
- User Module - Authentication implementation