I want to log users in with ajax request and after login in controller, redirect users to dashboard with javascript.
The Problem is when I use Auth:login($user)
in controller, it returns false and my return
line in controller is not reached. when I refresh the page manually I see that user is logged in !!
Backend (pseudo code):
$r = $request->all();
$user = User::where('email', $r['email'])->first();
Auth::login($user); // this returns false
return [ // this line is not reached
'status' > 'success'
];
Front End :
$.ajax({
type: 'Post',
url: '/login/js-login',
data: user_data,
success: function (res) {
if (res.data.status == 'success') {
toastr.success(" LoginSuccessfull!")
window.location.replace("https://www.example.com");
} else {
toastr.error('Error Login!')
}
});
I doubt that you are using Auth::login
in api.php
which is stateless.
Either you need to use Token base authentication or use Auth::login
in web.php
which is statefull
If you are using laravel version < 5.3
then middleware should be web
not api
Auth:logn
returns void
so you can use
Auth::login($user);
return [
'status' => Auth:check()
];
check()
Determine if the current user is authenticated.
you can use loginUsingId
:
$r = $request->all();
$user = User::where('email', $r['email'])->first();
if(isset($user)){
Auth::loginUsingId($user->id);
return ['status' => 'success'];
}
return ['status' => 'fail'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.