Symfony firewall: form login and logout
Symfony's firewall, declared in security.yaml, identifies who makes the request; the access_control rules then decide whether it may go through.
security: firewalls: main: lazy: true provider: employes form_login: login_path: app_connexion check_path: app_connexion enable_csrf: true logout: path: app_deconnexion target: app_accueil access_control: - { path: ^/cuisine, roles: ROLE_CUISINIER }
The firewall intercepts the form sent to check_path, finds the user through its provider, checks the hashed password, opens the session and redirects to the requested page: the login controller only displays the form, with getLastUsername() and getLastAuthenticationError() from AuthenticationUtils. Logout is intercepted too, the app_deconnexion route is never executed. Symfony picks a single firewall per request, the first whose pattern matches. The trap: the firewall only intercepts the POST to check_path. A form that posts elsewhere authenticates no one, and the page is displayed again without a word; a login page protected by its own access rule loops through redirects.
The exercises have you close the tavern's kitchen to the public, with an in-memory user whose password is hashed, form login and logout, then fix a sabotaged API where, among other things, the guilds receive an HTML login page instead of their booking.
3 exercises work on this concept, by writing code in the browser.
In the tracks
- Who goes there? Symfony for PHP developers · chapter 4: The keys to the tavern
- 🐉 Boss: the lost pigeon Symfony for PHP developers · chapter 5: Magic scrolls
In Practice
- A route that picks its firewall The firewall option of #[Route] hands a route to a firewall without touching its pattern, and entry_point_path sends anonymous users to the login page, with no class to write.