5
votes

I'm using the FOSUserBundle and I require the ability to login from 2 different routes(or more). These routes will have different templates and also login to different areas. The only thing that differs between the logins is the permission required. The routes will be something along the lines of

site.com/login

site.com/admin/login

and also possible site.com/ajax_login

I've been able to work out how to get different templates to render by ripping out everything but the CSRF token from the FOSUserBundle login.html.twig(that is overriden) then creating routes that render their own login boxes and also the login route(so that just the CSRF token gets rendered). This doesnt work for admin/login as the form posts back to login and if it fails it displays that page instead.

Is there any easy way to achieve this?

3
Are you talking about different firewalls maybe? - tamir
How did you do to use the login form in multiple templates ? - httpete

3 Answers

2
votes

I was stucked with the same question for a while and then i created a solution on my own. I knew there must be an easy solution...

I've submitted pull request which allows you to create new login templates more easily. Check the pull request here: https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1186.

There is also another quite easy way how to achieve this. Extend SecurityController and change renderLogin method with following content

protected function renderLogin(array $data, $template)
{
    return $this->container->get('templating')->renderResponse('YourBundle:Security:login.html.twig');
}

Then create a route to your newly created controller:

admin.login:
    pattern: /admin/login
    defaults: { _controller: YourBundle:Security:login }

After this only you have to do is to alter your security config accordingly. Change your form_login login_path to /admin/login and you are good to go.

1
votes

Here's what I eventually came up with, basically the both use the same firewall and share the same context so when I login via the normal login they also get access to the admin(assuming they are admin)

firewalls:
    admin:
        context:            site
        switch_user:        true
        pattern:            /admin(.*)
        form_login:
            provider:       fos_userbundle
            login_path:     /admin/login
            success_handler: admin_authentication_handler
            use_forward:    false
            check_path:     /admin/login_check
            failure_path:   null
            use_referer:    true
            always_use_default_target_path: true
            default_target_path: /admin/
        logout:
            path:           /admin/logout
            target:         /admin/login
        anonymous:    true
    public:
        pattern:   ^/
        context:            site
        form_login:
            login_path:     /login
            success_handler: authentication_handler
            failure_handler: authentication_handler
            provider: fos_userbundle
        anonymous: true
        logout: true

Of course if you need to make an AJAX login box you need to override the success and failure handlers and check to see if the request is a XmlHttpRequest and return the result of the login.

0
votes

Could you post your template?

Have you edited the path correctly in the template?

<form action="{{ path('form_submit') }}" method="post" {{ form_enctype(form) }}>

You want to send the form to the correct controller.