Laravel 5.1 间歇性状态异常
Laravel 5.1 Intermittent State Exception
SO 上有几篇关于此问题的帖子,并且在尝试了我找到的所有解决方案后,我确信我的代码确实存在问题。这是我的 Facebook 社交登录代码的当前版本。
需要说明的是,此代码在大约 90% 的时间内都有效。对于我网站上大约 10% 的用户,他们触发了无效状态异常错误并且无法登录。
我计划下个月在 Laravel 5.3 重建整个网站,所以我只需要这个修复持续 30 天。
这是我的代码:
public function create()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$socialUser = Socialite::driver('facebook')->user();
$user = User::firstOrNew(['socialite_provider' => 'facebook', 'socialite_id' => $socialUser->getId()]);
$user->socialite_provider = 'facebook';
$user->socialite_id = $socialUser->getId();
$user->socialite_nickname = $socialUser->getNickname();
$user->socialite_name = $socialUser->getName();
$user->socialite_avatar = $socialUser->getAvatar();
$user->socialite_email = $socialUser->getEmail();
$user->save();
/*
* Hack to fix invalid state error, I think this helped a little but it's still not working for all users.
*/
$state = $request->get('state');
$request->session()->put('state', $state);
if (Auth::check() == false) {
session()->regenerate();
}
// Update orders so that we don't lose our guest cart
$oldSessionId = Session::getId();
Auth::login($user, true);
$newSessionId = Session::getId();
Order::updateSession($oldSessionId, $newSessionId);
return redirect()->intended('/');
}
此问题已在 Laravel 5.3 中通过 ->stateless() 得到解决。
https://laravel.com/docs/5.3/authentication#stateless-http-basic-authentication
SO 上有几篇关于此问题的帖子,并且在尝试了我找到的所有解决方案后,我确信我的代码确实存在问题。这是我的 Facebook 社交登录代码的当前版本。
需要说明的是,此代码在大约 90% 的时间内都有效。对于我网站上大约 10% 的用户,他们触发了无效状态异常错误并且无法登录。
我计划下个月在 Laravel 5.3 重建整个网站,所以我只需要这个修复持续 30 天。
这是我的代码:
public function create()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$socialUser = Socialite::driver('facebook')->user();
$user = User::firstOrNew(['socialite_provider' => 'facebook', 'socialite_id' => $socialUser->getId()]);
$user->socialite_provider = 'facebook';
$user->socialite_id = $socialUser->getId();
$user->socialite_nickname = $socialUser->getNickname();
$user->socialite_name = $socialUser->getName();
$user->socialite_avatar = $socialUser->getAvatar();
$user->socialite_email = $socialUser->getEmail();
$user->save();
/*
* Hack to fix invalid state error, I think this helped a little but it's still not working for all users.
*/
$state = $request->get('state');
$request->session()->put('state', $state);
if (Auth::check() == false) {
session()->regenerate();
}
// Update orders so that we don't lose our guest cart
$oldSessionId = Session::getId();
Auth::login($user, true);
$newSessionId = Session::getId();
Order::updateSession($oldSessionId, $newSessionId);
return redirect()->intended('/');
}
此问题已在 Laravel 5.3 中通过 ->stateless() 得到解决。
https://laravel.com/docs/5.3/authentication#stateless-http-basic-authentication