Skip to content

Commit d6e5067

Browse files
committed
feature #17284 [Security] Add documentation for programmatic login (johnkrovitch)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Security] Add documentation for programmatic login Add documentation for programmatic login Fixes #16940 Commits ------- b63ccf7 [Security] Add documentation for programmatic login
2 parents 70d88d7 + b63ccf7 commit d6e5067

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

security.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,52 @@ and set the ``limiter`` option to its service ID:
15991599
;
16001600
};
16011601
1602+
Login Programmatically
1603+
----------------------
1604+
1605+
.. versionadded:: 6.2
1606+
1607+
The :class:`Symfony\Bundle\SecurityBundle\Security\Security <Symfony\\Bundle\\SecurityBundle\\Security\\Security>`
1608+
class was introduced in Symfony 6.2. Prior to 6.2, it was called
1609+
``Symfony\Component\Security\Core\Security``.
1610+
1611+
.. versionadded:: 6.2
1612+
1613+
The :method:`Symfony\\Bundle\\SecurityBundle\\Security\\Security::login`
1614+
method was introduced in Symfony 6.2.
1615+
1616+
You can log in a user programmatically using the `login()` method of the
1617+
:class:`Symfony\\Bundle\\SecurityBundle\\Security\\Security` helper::
1618+
1619+
// src/Controller/SecurityController.php
1620+
namespace App\Controller\SecurityController;
1621+
1622+
use App\Security\Authenticator\ExampleAuthenticator;
1623+
use Symfony\Bundle\SecurityBundle\Security\Security;
1624+
1625+
class SecurityController
1626+
{
1627+
public function someAction(Security $security): Response
1628+
{
1629+
// get the user to be authenticated
1630+
$user = ...;
1631+
1632+
// log the user in on the current firewall
1633+
$this->security->login($user);
1634+
1635+
// if the firewall has more than one authenticator, you must pass it explicitly
1636+
// by using the name of built-in authenticators...
1637+
$this->security->login($user, 'form_login');
1638+
// ...or the service id of custom authenticators
1639+
$this->security->login($user, ExampleAuthenticator::class);
1640+
1641+
// you can also log in on a different firewall
1642+
$this->security->login($user, 'form_login', 'other_firewall');
1643+
1644+
// ... redirect the user to its account page for instance
1645+
}
1646+
}
1647+
16021648
.. _security-logging-out:
16031649

16041650
Logging Out

0 commit comments

Comments
 (0)