I am trying authenticate user using ajax and redirect user to dashboard. Here is my code.
LOGIN FORM:
<div class="input-with-icon-left" title="Should be at least 8 characters long" data-tippy-placement="bottom">
<i class="icon-material-outline-lock"></i>
<input type="password" class="input-text with-border" name="password-register" id="password-register" placeholder="Password" required/>
</div>
<div class="input-with-icon-left">
<i class="icon-material-outline-lock"></i>
<input type="password" class="input-text with-border" name="password-repeat-register" id="password-repeat-register" placeholder="Repeat Password" required/>
</div>
AJAX:
$('#login-form').submit(function (event){
event.preventDefault();
var results = '';
jQuery('#login_error').html('');
$.ajax({
type: 'POST',
url: '{{ route ('user.login.post') }}',
data: {email: $("#emailaddress").val(), password:$("#password").val()},
dataType: "text",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function (response) {
var response = JSON.parse(response);
if(response.success == false){
jQuery.each(response.errors, function(key, value){
jQuery('#login_error').show();
jQuery('#login_error').append('<p>'+value+'</p>');
});
} else {
window.location.href = response.redirectto;
}
}, error: function (xhr, status, error) {
jQuery('#login_error').show();
jQuery('#login_error').append('<p>'+error+'</p>');
}
});
});
MY WEB ROUTES:
Route::group(['as' => 'user.'], function() {
Route::post('login_manual', 'UserController@login_manual')->name('login.post');
Route::post('register', 'UserController@register')->name('register.post');
Route::get('logout', 'UserController@logout')->name('logout');
Route::get('dashboard', 'UserController@dashboard')->name('dashboard');
});
USER CONTROLLER:
public function login_manual(Request $request) {
$validator = \Validator::make($request->all() , [
'email' => 'required|email|max:255',
'password' => 'required|min:6',
]);
if($validator->fails()) {
return response()->json(['success' => false, 'errors' => $validator->errors()->all()]);
} else {
// create our user data for the authentication
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
return response()->json(['success' => true, 'redirectto' => 'dashboard']);
} else {
return response()->json(['success' => false, 'error' => ['Login Failed! Username and password is incorrect.']]);
}
}
}
If I trying to login with ajax it was giving me error "route[login]' not found. So I override the unauthenticated method of \App\Exceptions\Helper.php file to this:
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
public function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
// : response()->json(['success' => false, 'error' => [$exception->getMessage()], 401]);
: response()->json(['success' => true, 'redirectto' => 'dashboard']);
}
But now I am getting this JSON response on '/dashboard'
{"success":true,"redirectto":"dashboard"}
I didn't understand where I am doing wrong.