KharNam
1 month ago
272
2
Laravel

Login with ajax

Posted 1 month ago by KharNam

I am performing the login function with ajax, but it seems my validator does not work, check it for me

  $data = $request->only('email', 'password');
            $validator = Validator::make($data, [
                'email' => 'unique:users,email'
            ]);
            if(!$validator->fails()){
                if(Auth::attempt($data)){
                    return \response()->json(route('success'), 200);
                }
                else{
                    return \response()->json(0, 500);
                }
            }
            else{
                return \response()->json(1, 500);
            }

client

 $.ajax({
           type : "POST",
           url : action,
           data: formData,
           success: function(data, textStatus ,jqXHR) {
               window.location.replace(data);
           },
           error: function (jqXHR, textStatus, errorThrown) {
               if(jqXHR.status === 500){
                   console.log();
                   if(parseInt(jqXHR.responseText) === 1){
                       $("#error-response").addClass("active");
                       $("#error-response").text("User not found");
                   }
                   else {
                       $("#error-response").addClass("active");
                       $("#error-response").text("Wrong password");
                   }
               }
           }
       });

Please sign in or create an account to participate in this conversation.