I'm learning laravel frameworking and I'm having issues with session in laravel and ajax(jquery). I can get authentication when the login form is submited but how to get session from laravel via ajax and access the dashboard panel? Here it's my ajax requisition:
$(document).on("click", '#btn_login', function(e){
e.preventDefault();
$(".msg_login").find('div').remove();
var email = $("#email_login").val();
var senha = $("#senha_login").val();
var _token = $("#form_login input[name=_token]").val();
if(email == '' || senha == '') {
$(".msg_login").append('<div class="alert alert-danger" role="alert"> Dados inválidos!</div>').fadeIn();
$("#email_login").focus();
} else {
$.ajax({
method: 'post',
dataType: 'html',
url: '/login',
data: {
'email': email,
'senha': senha,
'_token': _token
},
beforeSend: function(){
$("#btn_login").text('Acessando ...')
},
success: function(data){
if(data == '1') {
window.location.href = '/dashboard';
} else if(data == 0) {
$(".msg_login").append('<div class="alert alert-danger" role="alert"> Senha inválida</div>').fadeIn();
} else {
$(".msg_login").append('<div class="alert alert-danger" role="alert"> Usuário não encontrado!</div>').fadeIn();
}
},
complete: function(){
$("#btn_login").text('Login')
}
});
}
setTimeout(function(){
$(".msg_login").fadeOut();
}, 2500);
});
and here it's my controller:
public function find(Request $request) {
$user = new academias;
$user = $user::where('email', '=', $request->email)->get();
$msg = '';
if(count($user) > 0) {
$senha = $user[0]->senha;
if(Hash::check($request->senha, $senha)) {
$msg = '1';
} else {
$msg = '0';
}
} else {
$msg = '-1';
}
return $msg;
}
As you see, if the user is found, the controller returns a value 1 and then the javascript redirects the use to the dashboard page but how can I create a session using ajax?