I use the manual authentication in Larave, here is my function of code
public function doLogin(){
// create our user data for the authentication
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata,true)) {
return (Auth::check() ? 'true' : 'false');
}
else {
// validation not successful, send back to form
return (Auth::check() ? 'true' : 'false');
}
}
After logging in, the Auth::check
returned true. But after browsing to protected routes, which have this construct function
public function __construct()
{
$this->middleware('auth');
}
the middleware redirects me to the login page again, even after login.
Auth
middleware has never been modified. Are there any modifications I needed to do?
I also tried my custom middleware:
class LoginCheck
{
public function handle($request, Closure $next)
{
if (!Auth::check()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect('login');
}
}
return $next($request);
}
}
Still not working, means Auth::check() is returning false
.
Cookies are configured to store session, and still not working, too.
This is weird, but...
I created a new Laravel project. Copied all the MVC and routes (only that) but excluding everything about auth
. Then I did php artisan make:auth
, and it worked, and I have literally no idea why.
Seems like I must have messed with something really, bad.
By the way, thanks for all the help!
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.