1

I try to use an app_flash on my symfony app, it work everywhere but when I try to display a flash on my app_default it doesn't work. I tried to dump but it's empty like nothing is send.

I have a simple login controller to connect my users with this code:

 #[Route(path: '/login', name: 'app_login')]
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        $error = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();

        if($lastUsername){
            $this->addFlash('success', 'You are logged in.');
            return $this->redirectToRoute('app_default');

        }
        return $this->render('pages/security/login.html.twig', [
            'last_username' => $lastUsername,
            'error' => $error,
        ]);
    }

and my vue index.html.twig contains :

{% block body %}
    <div class="container w-75">
        {% for message in app.flashes('success') %}
            <div class="alert alert-success">
                {{ message }}
            </div>
        {% endfor %}
    </div>
{% endblock %}
4
  • What are you trying to achieve here? Are you trying to set the flash message only after a user successfully logs in, or are you wanting to redirect already logged-in users back to the app_default page if they accidentally load the login page and then show the flash message to notify them that they're already logged in? Commented Jul 27 at 10:28
  • My user is redirected on my homepage when he is successfully logs in, so I want to display my flash on my homepage when it's successful. Commented Jul 27 at 14:04
  • is your page loaded twice because flash messages cannot be repeated... ;) :)) Commented Jul 27 at 14:47
  • You have my code, I don't think it's loaded twice, but maybe I don't understand what you mean :( Commented Jul 27 at 15:49

1 Answer 1

1

$lastUsername will only contain a value if a user tried logging in with the login form but there was an error (e.g. they submitted the wrong password). (You can then echo $lastUsername back in the form so that they can try logging in again with the same username without having to retype it.)

So if a user logs in successfully on their first try, $lastUsername will remain empty and the flash message will not be set.

However, the login function is only used to render the login form, you cannot use it to determine if the login was successful. So if you only want to show a flash message when a user has successfully logged in, you should not set the flash message there.

If you're already using a custom LoginFormAuthenticator (one may have been generated for you if you used make:auth), you can set the flash message in its onAuthenticationSuccess method, right before the redirect:

class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{

    // ...

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        $request->getSession()->getFlashBag()->add('success', 'You are logged in.');

        return new RedirectResponse($this->urlGenerator->generate('app_default'));
    }
}

If not, you can set it in an event subscriber for the InteractiveLoginEvent:

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;

class LoginSuccessSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            InteractiveLoginEvent::class => 'onLoginSuccess',
        ];
    }

    public function onLoginSuccess(InteractiveLoginEvent $event)
    {
        $request = $event->getRequest();

        $request->getSession()->getFlashBag()->add('success', 'You are logged in.');
    }
}

This then sets the flash message and redirects to the default_target_path you have set in security.yaml, or to / if none was set:

security:
    firewalls:
        main:
            form_login:
                # ...
                default_target_path: app_default

But you could also use the LoginSuccessEvent, see the events here: https://symfony.com/doc/current/security.html#authentication-events

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.