0

I have the following problem where I've added the FosJSRoutingBundle to my project, and added the links to my base template

<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>

I can generate a couple of routes which I use for AJAX calls. But the problem occurs after a succesful login, which instead of matching to the last accessed page, is matching the route "fos_js_routing_js", and then shows a white page with the following content:

/**/fos.Router.setData({"base_url":"","routes":{"..."}});

My firewall has a pretty basic setup

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        lazy: true
        provider: users
        remember_me:
            secret: '%kernel.secret%'
            name: remember_me
            always_remember_me: true
        guard:
            authenticators:
                - App\Security\LoginAuthenticator

And I've tried several solutions like adding/editing new pattern to the firewall but with no success.

The LoginAuthenticator is the same one as the one generated from the make:auth command. The part concerning the redirecting is the method onAuthenticationSuccess:

/**
 * @param Request        $request
 * @param TokenInterface $token
 * @param string         $providerKey
 *
 * @return RedirectResponse
 * @throws Exception
 */
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): RedirectResponse
{
    if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey))
    {
        return new RedirectResponse($targetPath);
    }

    // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
    throw new Exception('TODO: provide a valid redirect inside ' . __FILE__);
}

When I dump the $request->getSession() part on login, I see the it's redirecting to js/routing:

[
  "_security.main.target_path" => "https://mydomain.local/js/routing?callback=fos.Router.setData"
  "_csrf/https-ajax" => "..."
  "_csrf/https-authenticate" => "..."
  "_security.last_username" => "YTZ"
]

Isn't the pattern in dev supposed to stop this from happening in the first place or do I misunderstand its usage?

EDIT

I've mentioned the link to the "last accessed page" for the RequestSubscriber earlier, and I've added '/js/routing' === $request->getPathInfo() to the if statement in the onKernelRequest method:

if (
     !$event->isMasterRequest()
     || $request->isXmlHttpRequest()
     || 'app_login' === $request->attributes->get('_route')
     || '/js/routing' === $request->getPathInfo()
)
{
   return;
}

The above works, but it seems more like a workaround than an actual solution.

YTZ
  • 876
  • 11
  • 26
  • Your firewall config looks good. Can you show the LoginAuthenticator? – dbrumann Mar 31 '21 at 07:16
  • @dbrumann Sorry for the late response. I've added the information to the question. It's the same authentication as in the documentation, created by the `make:auth` command. I've also dumped the response to show that it goes to `js/routing` instead of the real last accessed pages. – YTZ Mar 31 '21 at 18:24

0 Answers0