Symfony2 - Redirection after successful login

18,190

Solution 1

// if you're using Symfony 2.0
$key = '_security.target_path';
// if you're using Symfony 2.1 or greater
// where "main" is the name of your firewall in security.yml
$key = '_security.main.target_path';

// try to redirect to the last page, or fallback to the homepage
if ($this->container->get('session')->has($key)) {
  $url = $this->container->get('session')->get($key);
  $this->container->get('session')->remove($key);
} else {
  $url = $this->container->get('router')->generate('homepage');
}

return new RedirectResponse($url);

Solution 2

You need 2 listeners.

  • One to set in session last page

  • Second to redirect after succesfull login

That link will solve your problem: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

Share:
18,190
Dan
Author by

Dan

Updated on June 08, 2022

Comments

  • Dan
    Dan almost 2 years

    I've just started working through the Symfony 2 tutorials. I have created a bundle with a user class and have tried to follow the instructions to set up a login process. I think I am nearly there, however I'm currently falling at the last hurdle.

    I have set up a bundle: Dan\AuthBundle, which contains my user class and another bundle: Dan\HelloBundle which I want to allow only logged in users to access.

    My security.yml file is as follows:

    security:
        encoders:
            Dan\AuthBundle\Entity\User: sha512
    
        providers:
            main:
                entity: { class: Dan\AuthBundle\Entity\User, property: username }
            administrators:
                entity: { class: DanAuthBundle:User }
    
        firewalls:
            secured_area:
                pattern:    ^/*
                form_login:
                    check_path: /login_check
                    login_path: /login
                    always_use_default_target_path: false
                    default_target_path: /hello
    
        access_control:
            - { path: ^/hello/.* }
    

    The main routing.yml file looks like this:

    DanAuthBundle:
        resource: "@DanAuthBundle/Resources/config/routing.yml"
        prefix:   /auth/
    
    DanHelloBundle_homepage:
    pattern:  /hello/
    defaults: { _controller: DanHelloBundle:Default:index }
    
    login:
        pattern: /login
        defaults: {_controller: DanAuthBundle:Default:login }
    
    login_check:
        pattern: /login_check
    

    I have created several instances of my user class manually.

    If I try to access the url /hello, I correctly get redirected to the login page. If I enter incorrect details, I get the correct message(s) delivered in the template, however, when I log in with the correct details, I receive a 324 (empty response) error (at this time, the url displayed in the browser is login_check).

    From reading the documentation, I thought I should be redirected to the page I was originally trying to access?

    http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

    By default, if the submitted credentials are correct, the user will be redirected to the original page that was requested (e.g. /admin/foo). If the user originally went straight to the login page, he'll be redirected to the homepage. This can be highly customized, allowing you to, for example, redirect the user to a specific URL.

    Also, if I try to access the page after entering the correct details, I once again get redirected to the login page.

    Can anyone see if I've missed anything obvious?

    This is from my log file:

    [2012-06-18 18:33:47] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.salt AS salt3, t0.hashed_password AS hashed_password4 FROM User t0 WHERE t0.username = ? (["hello"]) [] [] [2012-06-18 18:33:47] security.INFO: User "hello" has been authenticated successfully [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] [] [2012-06-18 18:33:47] security.DEBUG: Write SecurityContext in the session [] []

    Any advice appreciated.

    Thanks.

  • Dan
    Dan almost 12 years
    Hi, is this something that has changed recently? I've added the quote from the documentation that made me believe it would redirect automatically. Is the documentation no longer correct?
  • Max Małecki
    Max Małecki almost 12 years
    Hi the _target_path is easy solution, but when you need to redirect in other controllers (ex. when user go another role it's useful to have those listeners).
  • Aerendir
    Aerendir about 9 years
    "Never check for the User object to see if they're logged in" from the Symfony documentation: symfony.com/doc/current/book/…