Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

Redirecting users after login through remember_me to different routes depending on their ROLES in Symfony 4

$
0
0

I am trying to redirect my users to different routes depending on their ROLES when they login. It works fine within my LoginFormAuthenticator (onAuthenticationSuccess function), but not once the used has remember_me activated.

LoginFormAuthenticator.php

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    $roles = $token->getRoleNames();
    if (in_array('ROLE_ADMIN', $roles, true)) {
        return new RedirectResponse($this->urlGenerator->generate('admin'));
    }

    if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
        return new RedirectResponse($targetPath);
    }

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

security.yaml

remember_me:
    secret: '%kernel.secret%'
    lifetime: 604800  # 1 week in seconds

I manage to see that the authenticate function of the RememberMeListener is called at every request, but he first line of the function is checking for the current security token and if the request has one, it just ends the function with a return;. In my case, when I try to access the app through remember_me, there is a token. So I guess this is why I can use the security.interactive_login event that is dispatched a few lines below in this Listener.

RememberMeListener.php

public function authenticate(RequestEvent $event)
{
    if (null !== $this->tokenStorage->getToken()) {
        return;
    }

    (...)

    try {
        (...)

        if (null !== $this->dispatcher) {
            $loginEvent = new InteractiveLoginEvent($request, $token);
            $this->dispatcher->dispatch($loginEvent, SecurityEvents::INTERACTIVE_LOGIN);
        }

        (...)
    }
}

It is hard to find what other event I could use, so if anybody has a clue, I would be glad to try something else!


Viewing all articles
Browse latest Browse all 3924

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>