I'm trying to display the username of the logged in user in my twig template.
Setting token, which works perfect with firewalls and logs the user in:
$token = new UsernamePasswordToken($user->getUsername(), $user->getPassword(), "secured_area", $user->getRoles());
$this->get("security.context")->setToken($token);
$session->set('_security_secured_area', serialize($token));
I want to put something like:
<div class='meta'>Logged in as <b>username_here</b>
at the top of my template.
Under normal circumstances you should be able to display the username by writting this line:
{{ app.user.username }}
Just in case I didn't get the question right or didn't understand what you have there is also an alternative. Note what was also mentioned in the comments by @Tsounable. This alternative is deprecated since version 2.6 and shouldn't be used anymore!
{{ app.security.getToken().getUser().getUsername() }}
Not sure what works out for you. The former is the kind of "standard" way to achieve what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With