sonata user new firewall keeps redirecting to wrong route

722 Views Asked by At

basicaly what i'm trying to accomplish is to create a new login form based on sonata user which is a fos user implementation used in sonata ecommerce. The new login form is supposed to do the same as the original login form but have different layout.

What i did:
- Created new twig file
- added routing:

m2m_partner:
resource: "@ApplicationSonataUserBundle/Resources/config/routing/partnerlogin.xml"
prefix: /partner

contents:

<route id="m2m_partner_login" pattern="/login">
<default key="_controller">ApplicationSonataUserBundle:Partner:login</default>
</route>

-Added new firewall in security.yml (pretty similar to the one in 'admin')

    partner:
        pattern:      /partner(.*)
        context:        user
        form_login:
            provider:       fos_userbundle
            login_path:     /partner/login
            use_forward:    false
            check_path:     /partner/login_check
            failure_path:   null
        logout:
            path: /partner/logout
            invalidate_session: false
            handlers: ['sonata.page.cms_manager_selector']
        anonymous:    true
        switch_user: true

After that when i fill in good credentials it logs me in fine but when i put bad credentials it redirects me to /login instead of /partner/login with 'bad credentials' message. How can i fix it? am i missing something?

edit 1:
i checked how admin login is going on and found out form action goes to a different controller than normal login which looks just the same as fos user 'check' action:

    public function checkAction()
{
    throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}

i copied it to my controller and after this all i get after submitting form with bad credentials is this error:

 You must configure the check path to be handled by the firewall using form_login in your security firewall configuration. 
2

There are 2 best solutions below

0
On BEST ANSWER

Check order of your firewals elements. Partner should be above main section like

 firewalls:
      partner:
         pattern:      /partner(.*)
         ......



      main:
       .....
1
On

You should change the failure_path to your new route:

  form_login:
        failure_path:   /partner/login

Or

  form_login:
        failure_path:   m2m_partner_login

The second way is cleaner because it uses route name instead of the url. So if you change the url in your xml it wont break your firewall.

http://symfony.com/doc/current/cookbook/security/form_login.html#redirecting-on-login-failure

I'm guessing if you dont provide the failure_path, it fallbacks to the default failure path (/login).

edit1:

You need to add in your route xml:

<route id="m2m_partner_login_check" pattern="/login_check">
    <default key="_controller">ApplicationSonataUserBundle:Partner:check</default>
    <requirement key="_method">POST</requirement>
</route>

<route id="m2m_partner_logout" pattern="/logout">
    <default key="_controller">ApplicationSonataUserBundle:Partner:logout</default>
</route>

Maybe this will fix the exception.