2

Just wondering if there is an easy solution to this in Symfony 4. Normally users would logout and be sent back to the home page. But there is one page where it is checked that the user is currently authenticated on another site, if this is incorrect I have a link that logs the user out of my site and redirects to the external site. I managed this on an old silex based version of the site using the following routing of controllers in app.php

$app->get('/logout', $app->factory(function() use($app) {
$pid = $app['request_stack']->getCurrentRequest()->query->get('pid');
$message = $app['request_stack']->getCurrentRequest()->query->get('message');
$redirect = $app['request_stack']->getCurrentRequest()->query->get('redirect');
return $app->redirect(FRAMEWORK_URL."/logout?pid=$pid&message=$message&redirect=$redirect");
})

);

Thanks

Martyn

3 Answers 3

2

Set value of logout.target for a firewall in security.yaml to an external URL:

    firewalls:
        main:
                ...
            logout:
                ...
                target: 'EXTERNAL URL'

Value of logout.target could be an URL or app route name. App route and related controller could be used to create dynamic redirect targets.

2
  • Hi Andreas, would you mind giving me a little more help please? I can't seem to make it work (see below). If I link to 'redirectafterlogout' with a url attached then it does not log me out?
    – MartynW
    Feb 5, 2019 at 20:51
  • target can only be set to same domain, so this will not work. Look at Symfony\Component\Security\Http\HttpUtils::createRedirectResponse. There is a check !preg_match(sprintf($this->domainRegexp, preg_quote($request->getHttpHost())), $host[0]) which makes sure that domain to be redirected is same as domain of the request. Nov 5, 2020 at 18:16
2

Unfortunately logout.target will not work as there is special check (in Symfony\Component\Security\Http\HttpUtils::createRedirectResponse) to match the request domain to the redirection domain. If they're not the same it will set the target to /.

Proper way of redirecting to external URL is to create class which implements Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface, for example like this:

<?php

// src/Security/CustomLogoutSuccessHandler.php

class CustomLogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
    private $target;

    public function __construct(string $target)
    {
        $this->target = $target;
    }

    public function onLogoutSuccess(Request $request)
    {
        return new RedirectResponse($this->target);
    }
}

Then create service in services.yaml:

services:
  App\Security\CustomLogoutSuccessHandler:
    arguments: ['%env(resolve:LOGOUT_TARGET_URL)%'] # resolve from .env (or you can get from anywhere)

and finally tell security.yaml to use your handler instead of default one:

security:
  firewalls:
    main:
      logout:
        success_handler: 'App\Security\CustomLogoutSuccessHandler'
0

Ok so tried this

I made a new route in a controller:

    /**
 * @Route("/redirectafterlogout/{url?/}", name="redirectafterlogout")
 */
public function redirectafterlogout($url)
{
    if($url == "/"){
        // redirects to the "homepage" route
        return $this->redirectToRoute('homepage');
    } else {
        return $this->redirect('http://'.$url);
    }
}

I added this to security.yaml

logout:
            path:   logout
            target: 'redirectafterlogout'

it works fine and logs me out and puts me on the homepage if i use the normal logout link. However, if i try a link like:

it redirects me but does not seem to log me out? I can go back with me browser and I'm still logged in?

Not sure what I am doing wrong?

Thanks

1
  • Maybe your browser just shows you a cached page (in a logged in state). Are you able to access any restricted routes after the redirect?
    – Andreas
    Feb 7, 2019 at 6:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.