Search code examples
phpsymfonyfosuserbundlesymfony-2.6

login_check performs a wrong redirect in login form using FosUserBundle


I use a UserBundle that overrides FosUserBundle, in there I have set the users prefix for all my user routes:

// UserBundle/Resources/config/routing/all.xml
...
<import
    resource="@FooUserBundle/Resources/config/routing/security.xml" 
    prefix="/users/" />
<import
    resource="@FooUserBundle/Resources/config/routing/profile.xml"
    prefix="/users/profile" />
<import
    resource="@FooUserBundle/Resources/config/routing/registration.xml"
    prefix="/users/register" />
...

If I check the routes with php app/console route:debug, they are shown with the expected prefix:

fos_user_security_login               ANY      ANY    ANY  /users/login    
fos_user_security_check               POST     ANY    ANY  /users/login_check
fos_user_security_logout              ANY      ANY    ANY  /users/logout
fos_user_registration_register        ANY      ANY    ANY  /users/register/

I use the following url to show the login form:

http://www.foo.local/app_dev.php/users/login

In order to manage the submit, I use this path in my twig template:

<form action="{{ path("fos_user_security_check") }}" method="post">

Which generates the desired action url once the template is processed:

<form action="/app_dev.php/users/login_check" method="post">

My security.yml config is this:

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    main:
        pattern: ^/
        form_login:
            check_path: fos_user_security_check
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
        logout:       true
        anonymous:    true 

However If I log in a user using this form, it redirects me to http://www.foo.local/app_dev.php/login, instead of http://www.foo.local/app_dev.php/users/login, giving a "no route found error"

How can I fix this?


Solution

  • FOSUserBundle does not handle the login. It is done by the Security component. So FOSUserBundle does not have a way to change it as it is not responsible for the default behavior.

    The form login allows configuring the redirection after the login: http://symfony.com/doc/current/cookbook/security/form_login.html#redirecting-after-success

    I see 2 ways to do it:

    use a custom success handler (the cleaner way, but more complex and not documented yet in the official doc) set the redirection after the login to a fixed path (as described in the doc) with a controller redirecting again.