Skip to content

Commit b7e917d

Browse files
committed
[Security] Add documentation for programmatic login
1 parent d0a40c2 commit b7e917d

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

security.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,98 @@ 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+
defined in ``Symfony\Component\Security\Core\Security``.
667+
668+
Since Symfony 6.2, you can log in an user programmatically using the `login()` method of the
669+
`Symfony\\Bundle\\SecurityBundle\\Security\\Security` helper.
670+
671+
.. code-block:: php
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 one authenticator in the firewall. If the firewall is not passed, it will
693+
retrieved from the request. If the current request is not under any firewall an exception will be thrown.
694+
695+
You can also pass the authenticator on which the user will
696+
be authenticated against. For built-in authenticators, you can pass an authenticator name like "form_login",
697+
"http_basic"... For custom authenticators, you can pass the service id of the authenticator
698+
(ie `App\\Security\\Authenticator\\ExampleAuthenticator`)
699+
700+
.. code-block:: php
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+
.. code-block:: php
730+
731+
use App\Security\Authenticator\ExampleAuthenticator;
732+
use Symfony\Bundle\SecurityBundle\Security\Security;
733+
734+
class ExampleController
735+
{
736+
public function __construct(private Security $security)
737+
{
738+
}
739+
740+
public function someMethod(): Response
741+
{
742+
$user = ... // Get the user to be authenticated
743+
744+
$this->security->login($user, 'form_login', 'another_firewall');
745+
746+
// Redirect the user to its account page for instance
747+
// ...
748+
}
749+
}
750+
659751
Form Login
660752
~~~~~~~~~~
661753

0 commit comments

Comments
 (0)