This repository has been archived on 2024-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/docs/Security.md

103 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2022-11-17 14:07:14 -08:00
# User Authentication
## Create a user class
Permissions are linked to a user object.
```bash
2023-07-27 11:30:37 -07:00
symfony console make:user # (All default yes)
symfony console make:auth # (1 for login form)
symfony console make:registration-form
symfony console make:reset-password
2022-11-17 14:07:14 -08:00
```
Now you will want to sync the database (using docker)
2022-11-17 14:07:14 -08:00
```bash
sudo docker-compose run symfony php bin/console make:migration
sudo docker-compose run symfony php bin/console doctrine:migrations:migrate
2022-11-17 14:07:14 -08:00
```
Note: 'symfony' is the name of your symfony app in docker-compose (such as app)
2022-11-17 14:07:14 -08:00
## Login Form
2023-07-27 11:30:37 -07:00
The above already created this.
2022-11-17 14:07:14 -08:00
```php bin/console make:controller Login```
You have to add
```yaml
form_login:
login_path: app_login
check_path: app_login
```
to the firewalls section under main of config/packages/security.yaml
2022-11-17 14:07:14 -08:00
### Modify the controller
```php
2022-11-17 20:25:09 -08:00
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class LoginController extends AbstractController
{
#[Route('/login', name: 'app_login')]
public function index(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
2022-11-17 14:07:14 -08:00
$lastUsername = $authenticationUtils->getLastUsername();
2022-11-17 20:25:09 -08:00
2022-11-17 14:07:14 -08:00
return $this->render('login/index.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
2022-11-17 20:25:09 -08:00
}
}
2022-11-17 14:07:14 -08:00
```
### Modify the template
```php
2022-11-17 20:25:09 -08:00
{% extends '@nytwig/master.html.twig' %}
{% block title %}Hello LoginController!{% endblock %}
2022-11-17 14:07:14 -08:00
{% block content %}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('app_login') }}" method="post">
<label for="username">Email:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}"/>
<label for="password">Password:</label>
<input type="password" id="password" name="_password"/>
{# If you want to control the URL the user is redirected to on success
<input type="hidden" name="_target_path" value="/account"/> #}
<button type="submit">login</button>
</form>
{% endblock %}
```
## Loggging Out
https://symfony.com/doc/current/security.html#logging-out
## Access Control (Authorization)
https://symfony.com/doc/current/security.html#access-control-authorization
## References
* https://symfony.com/doc/current/security.html
2023-04-03 20:26:25 -07:00
* https://dev.to/nabbisen/symfony-6-user-authentication-4ek