这是我的路线
Route::group(['middleware' => ['web']], function(){
Route::get('/', 'IndexController@index');
Route::get('/play/{game_id}', 'PlayController@play');
});
这是我的kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
这是我的播放方法,我正在检查身份验证,如果还没有登录,它将从数据库创建或获取用户并调用Auth :: login()
public function play($game_id)
{
$this->game_id_ = $game_id;
if(!Auth::check())
{
echo "Not login yet.";
$user = "username";
$name = "password";
$id = "1234555";
$email = "email@email.com";
$avatar = "pic.png";
// echo $avatar;
(new SocialAuthController)->facebookLoginAndUpdateData($name, $id, $email, $avatar);
}
}
它在facebookLoginAndUpdateData()中调用Auth :: login($ user,true)
public function facebookLoginAndUpdateData($name, $email, $facebook_id, $avatar)
{
$authUser = $this->findOrCreateUser($name, $email, $facebook_id, $avatar);
Auth::login($authUser, true);
return $authUser;
}
private function findOrCreateUser($name, $email, $facebook_id, $avatar)
{
$authUser = User::where('facebook_id', $facebook_id)->first();
if ($authUser){
return $authUser;
}
return User::create([
'name' => $name,
'email' => $email,
'facebook_id' => $facebook_id,
'avatar' => $avatar
]);
}
问题是, 第一次调用play()它会在数据库中创建新行。
之后,我刷新了这个页面但是Auth :: check()仍然返回false,尽管我第一次调用了Auth :: login($ user,true),
应该记住我已经登录并且Auth :: check()应该返回true, 我已经谷歌了4个小时,但我还是无法修复它。
任何人都有同样的问题,请帮忙。谢谢。
答案 0 :(得分:0)
检查if (Auth::guest()) {}
而不是if(!Auth::check()){}