Laravel 5.2登录会话不持久 [英] Laravel 5.2 login session not persisting
问题描述
我一直在使用 Sentinel 在Laravel 5.2上实现一个简单的身份验证系统.
I have been implementing a simple authentication system on Laravel 5.2 using Sentinel.
// Route : /login
$success = Sentinel::authenticate(array(
'email' => $email,
'password' => $password,
));
echo $success ? 'Login success' : 'Login failed';
因此,以上代码在身份验证代码之后输出Login success
.但是,登录状态不会持续存在于其他请求中.即:如果我从其他请求中检查身份验证状态,则表示我尚未登录!
So, the above code outputs Login success
after the authentication code. But, the login status is not getting persisted to other requests. ie: if I check the authentication status from other requests, it is saying that I am not logged in!
// Route : test-login
echo \Sentinel::check() ? 'User is logged in' : 'User is not logged in';
我什至尝试使用\Auth::attempt
实现默认的laravel身份验证.但是,这也带来了同样的事情.
I have even tried to implement a defaut laravel authencation using \Auth::attempt
. But, that also giving the same thing.
对此表示任何帮助.
推荐答案
在Laravel 5.2中,您需要将web
组middlewere应用于所有您不想使会话正常工作的路由.这是Laravel 5.1的主要更改.
In Laravel 5.2 you need to apply web
group middlewere to all your routes you wan't to make sessions work. This is the major change from Laravel 5.1.
请查看 https://laravel.com/docs/5.2/routing#basic-routing
当前默认的route.php文件如下所示:
The default routes.php file looks like this at the moment:
Route::group(['middleware' => ['web']], function () {
// here you should put your routes
});
编辑
您也可以直接查看 https://github.com/laravel/laravel /blob/master/app/Http/Kernel.php 进入middlewareGroups
属性,以了解针对web
组中间件触发了哪些中间件
You can look also directly at https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php into middlewareGroups
property to know which middlewares are fired for web
group middleware
这篇关于Laravel 5.2登录会话不持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!