Have you followed this post, similar. https://laracasts.com/discuss/channels/laravel/laravel-5-and-ajax-2
Oct 25, 2015
2
Level 5
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
Please or to participate in this conversation.