32

I need to disable redirection after login check, because I need to get only that the login was success or not. After submission /login_check url give me the right data, but keep redirecting to /login (on failure).
/login is blank after that.
I am trying to set up login form using extjs 4 so I need to validate trough an ajax post request. login_check should authenticate, create user session and return whether it was success or failure, but no forwarding anywhere.

my login.html.twig looks like:

{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
    { success:true }
{% else %}
    { success: false }
{% endif %}

and in security.yml:

firewalls:
    main:
        form_login:
            provider: fos_userbundle
            failure_path:  null
            failure_forward: false
j0k
  • 22,600
  • 28
  • 79
  • 90
user1063963
  • 1,355
  • 6
  • 22
  • 34
  • 1
    Can you clarify your use case, eg just a bit of background on how this fits in your application? – richsage Nov 29 '11 at 11:15
  • I am trying to set up login using extjs 4. I get the form to send to symfony for check by ajax post, but after check a GET request appair for the login page, and it would might ok if wouldn't be blank, so js is just keep waiting to get data that can be processed. – user1063963 Nov 29 '11 at 12:26

2 Answers2

64

Create an authentication handler:

namespace YourVendor\UserBundle\Handler;

// "use" statements here

class AuthenticationHandler
implements AuthenticationSuccessHandlerInterface,
           AuthenticationFailureHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if ($request->isXmlHttpRequest()) {
            $result = array('success' => true);
            return new Response(json_encode($result));
        } else {
            // Handle non XmlHttp request here
        }
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        if ($request->isXmlHttpRequest()) {
            $result = array('success' => false);
            return new Response(json_encode($result));
        } else {
            // Handle non XmlHttp request here
        }
    }
}

Register the handler as a service:

services:
    authentication_handler:
        class: YourVendor\UserBundle\Handler\AuthenticationHandler

Register the service in the firewall:

firewalls:
    main:
        form_login:
            success_handler: authentication_handler
            failure_handler: authentication_handler

This is a rough example to give you the general idea — you'll need to figure out the details by yourself. If you're stuck and need further clarifications, put your questions in the comments and I'll try to elaborate the example.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • success_handler and failure_handler options could be arrays? what if I need to install some bundle and as part of its config I need register success_handler and failure_handler provided by bundle. but they are already registered with my own handlers. how to register more than one handler? – Oleg Abrazhaev Apr 23 '17 at 06:33
1

The normal symfony flow is to redirect you to a login page if you are not logged in, which works fine for humans. But you seems to be looking for a programmatic solution.

Have you tried setting _target_path in your form, to declare what the "next page" should be? Symfony is always going to forward you somewhere, but you can set that somewhere to wherever you want.

I found these two pages useful for describing the inner workings of the login form:

Neil Katin
  • 246
  • 2
  • 2