Skip to content

Commit 0fb878b

Browse files
committed
[Security] Add documentation for programmatic login
1 parent f34e77d commit 0fb878b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

security.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,95 @@ 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.
666+
The :method:`Symfony\\Bundle\\SecurityBundle\\Security\\Security::login` method
667+
was introduced in Symfony 6.2.
668+
669+
You can log in an user programmatically using the `login()` method of the
670+
:class:`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+
public function someMethod(): Response
681+
{
682+
$user = ... // Get the user to be authenticated
683+
684+
$this->security->login($user);
685+
686+
// Redirect the user to its account page for instance
687+
// ...
688+
}
689+
}
690+
691+
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.
692+
If the firewall is not explicitly passed, the method will
693+
be retrieved from the request. If the current request is not under any firewall an
694+
`Symfony\\Component\\Security\\Core\\Exception\\LogicException` exception will be thrown.
695+
696+
You can also pass the authenticator on which the user will
697+
be authenticated against. For built-in authenticators, you can pass an authenticator name like ``form_login``,
698+
``http_basic``... For custom authenticators, you can pass the service id of the authenticator
699+
(i.e. `App\\Security\\Authenticator\\ExampleAuthenticator`)::
700+
701+
use App\Security\Authenticator\ExampleAuthenticator;
702+
use Symfony\Bundle\SecurityBundle\Security\Security;
703+
704+
class ExampleController
705+
{
706+
public function __construct(private Security $security)
707+
{
708+
}
709+
710+
public function someMethod(): Response
711+
{
712+
$user = ... // Get the user to be authenticated
713+
714+
// You can use the authenticator name for built-in authenticator (like "form_login", "http_basic"...)
715+
$this->security->login($user, 'form_login');
716+
717+
// Or the service id of your custom authenticator
718+
$this->security->login($user, ExampleAuthenticator::class);
719+
720+
// Redirect the user to its account page for instance
721+
// ...
722+
}
723+
}
724+
725+
Previous examples retrieves the firewall name according to the current request. But you can also pass a firewall
726+
name to be authenticated against::
727+
728+
use App\Security\Authenticator\ExampleAuthenticator;
729+
use Symfony\Bundle\SecurityBundle\Security\Security;
730+
731+
class ExampleController
732+
{
733+
public function __construct(private Security $security)
734+
{
735+
}
736+
737+
public function someMethod(): Response
738+
{
739+
$user = ... // Get the user to be authenticated
740+
741+
$this->security->login($user, 'form_login', 'another_firewall');
742+
743+
// Redirect the user to its account page for instance
744+
// ...
745+
}
746+
}
747+
659748
Form Login
660749
~~~~~~~~~~
661750

0 commit comments

Comments
 (0)