Skip to content

Commit 5611f88

Browse files
committed
docs: add docs for programmatic logout
1 parent f34e77d commit 5611f88

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

security.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,49 @@ Next, you need to create a route for this URL (but not a controller):
17231723
That's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
17241724
Symfony will un-authenticate the current user and redirect them.
17251725

1726+
Logout programmatically
1727+
-----------------------
1728+
1729+
.. versionadded:: 6.2
1730+
1731+
The :class:`Symfony\Bundle\SecurityBundle\Security\Security <Symfony\\Bundle\\SecurityBundle\\Security\\Security>`
1732+
class was introduced in Symfony 6.2. Prior to 6.2, it was called
1733+
``Symfony\Component\Security\Core\Security``.
1734+
1735+
.. versionadded:: 6.2
1736+
1737+
The :method:`Symfony\\Bundle\\SecurityBundle\\Security\\Security::logout`
1738+
method was introduced in Symfony 6.2.
1739+
1740+
You can logout user programmatically using the `logout()` method of the
1741+
:class:`Symfony\\Bundle\\SecurityBundle\\Security\\Security` helper. The user will be logout from the current firewall
1742+
in the request. If the current request is not behind a firewall a ``\LogicException`` will be thrown. ::
1743+
1744+
// src/Controller/SecurityController.php
1745+
namespace App\Controller\SecurityController;
1746+
1747+
use App\Security\Authenticator\ExampleAuthenticator;
1748+
use Symfony\Bundle\SecurityBundle\Security\Security;
1749+
1750+
class SecurityController
1751+
{
1752+
public function someAction(Security $security): Response
1753+
{
1754+
// logout the user in on the current firewall
1755+
$response = $this->security->logout();
1756+
1757+
// You can also disable the csrf logout
1758+
$response = $this->security->logout(false);
1759+
1760+
if ($response !== null) {
1761+
return $response;
1762+
}
1763+
1764+
// Redirect to the homepage for instance
1765+
// ...
1766+
}
1767+
}
1768+
17261769
Customizing Logout
17271770
~~~~~~~~~~~~~~~~~~
17281771

0 commit comments

Comments
 (0)