Skip to content

Commit 062f9a4

Browse files
committed
[Security] Add documentation for programmatic login
1 parent 1aa135c commit 062f9a4

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

security.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,96 @@ many other authenticators:
656656

657657
.. _security-form-login:
658658

659+
Login Programmatically
660+
----------------------
661+
662+
.. versionadded:: 6.2
663+
664+
The :class:`Symfony\\Bundle\\SecurityBundle\\Security\\Security` class
665+
was introduced in Symfony 6.2. In previous Symfony versions this class was
666+
The :method:`Symfony\\Bundle\\SecurityBundle\\Security\\Security::login()` method
667+
was introduced in Symfony 6.2.
668+
669+
Since Symfony 6.2, you can log in an user programmatically using the `login()` method of the
670+
`Symfony\\Bundle\\SecurityBundle\\Security\\Security` helper::
671+
672+
673+
use Symfony\Bundle\SecurityBundle\Security\Security;
674+
675+
class ExampleController
676+
{
677+
public function __construct(private Security $security)
678+
{
679+
}
680+
681+
public function someMethod(): Response
682+
{
683+
$user = ... // Get the user to be authenticated
684+
685+
$this->security->login($user);
686+
687+
// Redirect the user to its account page for instance
688+
// ...
689+
}
690+
}
691+
692+
The previous example only works if you have only one authenticator configured on the current firewall so if you have more than one authenticator, you need to specify which authenticator should be used.
693+
If the firewall is not explicitly passed, the method will
694+
be retrieved from the request. If the current request is not under any firewall an
695+
`Symfony\\Component\\Security\\Core\\Exception\\LogicException` exception will be thrown.
696+
697+
You can also pass the authenticator on which the user will
698+
be authenticated against. For built-in authenticators, you can pass an authenticator name like ``form_login``,
699+
``http_basic``... For custom authenticators, you can pass the service id of the authenticator
700+
(i.e. `App\\Security\\Authenticator\\ExampleAuthenticator`)::
701+
702+
use App\Security\Authenticator\ExampleAuthenticator;
703+
use Symfony\Bundle\SecurityBundle\Security\Security;
704+
705+
class ExampleController
706+
{
707+
public function __construct(private Security $security)
708+
{
709+
}
710+
711+
public function someMethod(): Response
712+
{
713+
$user = ... // Get the user to be authenticated
714+
715+
// You can use the authenticator name for built-in authenticator (like "form_login", "http_basic"...)
716+
$this->security->login($user, 'form_login');
717+
718+
// Or the service id of your custom authenticator
719+
$this->security->login($user, ExampleAuthenticator::class);
720+
721+
// Redirect the user to its account page for instance
722+
// ...
723+
}
724+
}
725+
726+
Previous examples retrieves the firewall name according to the current request. But you can also pass a firewall
727+
name to be authenticated against::
728+
729+
use App\Security\Authenticator\ExampleAuthenticator;
730+
use Symfony\Bundle\SecurityBundle\Security\Security;
731+
732+
class ExampleController
733+
{
734+
public function __construct(private Security $security)
735+
{
736+
}
737+
738+
public function someMethod(): Response
739+
{
740+
$user = ... // Get the user to be authenticated
741+
742+
$this->security->login($user, 'form_login', 'another_firewall');
743+
744+
// Redirect the user to its account page for instance
745+
// ...
746+
}
747+
}
748+
659749
Form Login
660750
~~~~~~~~~~
661751

0 commit comments

Comments
 (0)