Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masterpowers's avatar

Ajax and Jquery Login

Hi Hope Someone Can help me, Im Able To Test My Code Working. But I dont know how Integrate My laravel Validation and Json Response. I already Tried all sorts of Stuff.

What I, Trying To Do is Make a Login with Jquery and Ajax. Here is My Working Code

Routes File

Route::get('login', ['as' => 'login', 'uses' => 'Auth\AuthController@login']);

AuthController

 public function authenticate(Request $request)
    {
        $loginRequest = new LoginRequest();
        $validator = Validator::make($request->all(), $loginRequest->rules(), $loginRequest->messages());
        if ($validator->fails()) {
            return "fail";
        }
        $email = $request->email;
        $password = $request->password;
        if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => true])) {
            return redirect()
                ->intended('login');
                
                
        }
        return "fail";

    }

My JS

$('#sign_in').on('click', function(e){
            e.preventDefault();
            var login_form = $('#login_form').serializeArray();
            var url = $('#login_form').attr('action');
                loader('on');
            $.post(url, login_form, function(data){
                loader('off');
                //error
                if(data == "fail"){
                    
                    $('#xloader').addClass('red').fadeIn(2000, function(){
                        $(this).hide();
                        $(this).removeClass("red");

                    });
                    Materialize.toast('You Failed to Login', 4000,'',function(){console.log('You Failed to Login')});
                    $.each(login_form, function(i,k){
                        $('#' + k.name).val('');
                    });

                    //success
                }else{
                    
                    $('#xloader').addClass('green').fadeIn(2000, function(){
                        $(this).hide();
                        $(this).removeClass("green");
                    });
                    Materialize.toast('You Have Successfully Login!', 4000,'',function(){console.log('Congratulations! You Have Successfully Login!')});
                    authenticated('profile');
                }
                
            });
        });

Here i Know I can Login Successfully.

But When I Will Bring mY Old Code of my AuthController

 public function authenticate(Request $request)
    {
         $loginRequest = new LoginRequest();
        $validator = Validator::make($request->all(), $loginRequest->rules(), $loginRequest->messages());
        if ($validator->fails()) {
            return redirect()
                ->back()
                ->withErrors($validator)
                ->withInput()
                ->with('login', 'active');
        }
        $email = $request->email;
        $password = $request->password;
        if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => true])) {
            return redirect()
                ->intended('login');
                
                
        }
        return redirect()->back()->withErrors('User failed to login');

    }

I dont Know How to make it Work with MY JS and with the Laravel Validation i Set up.

Hope SOmeone Can Help me Out Im Thanks

0 likes
2 replies
Snapey's avatar

Your route says it uses Auth\Authcontroller@login but your method is called authenticate?

Also, if successfully logged in you are redirecting to intended. This cannot happen with an ajax request because the browser is not looking to be redirected. You need to return a response to your javascript that says the user was logged in or not, and then decide in the browser what to do next.

Please or to participate in this conversation.