php - Laravel Socialite 不能在谷歌浏览器上运行
问题描述
我的 Laravel Socialite 登录有一个问题,在我的 Chrome 中正常工作,但在其他人的浏览器中不起作用(在其他浏览器中工作)。在服务器中的 php 从 7.1 更新到 7.3.18 并从 5.8 更新到 Laravel 6 之前,一切正常。我尝试清除所有缓存,将会话模式更改为 cookie(之前的文件),清除浏览器中的会话和 cookie,但没有解决问题。
这是我的代码:
public function loginSocial(Request $request){
$this->validate($request, [
'social_type' => 'required|in:google,facebook'
]);
$socialType = $request->get('social_type');
return Socialite::driver($socialType)->stateless()->redirect();
}
public function loginCallback(Request $request){
$socialType = $request->session()->get('social_type');
//Aparently, this get give to $socialType null in ppl browser. I dont understand why this get doesn't works.
$userSocial = Socialite::driver($socialType)->stateless()->user();
//If use 'google' instead $socialType, works fine.
$user = User::where('email',$userSocial->email)->first();
\Auth::login($user);
return redirect()->intended($this->redirectPath());
}
解决方案
我了解您正在尝试做的事情,但有时越来越少……回电是由提供商而不是用户进行的。无论如何,每个社交登录都有不同的方法
// Google login
public function googleSocialLogin(Request $request){
Socialite::driver('google')->stateless()->redirect();
}
// Google callback
public function googleSocialLoginCallback(){
$userSocial = Socialite::driver('google')->stateless()->user();
$user = User::where('email',$userSocial->email)->first();
\Auth::login($user);
return redirect()->intended($this->redirectPath());
}
// Facebook login
public function facebookSocialLogin(Request $request){
Socialite::driver('facebook')->stateless()->redirect();
}
// Facebook callback
public function facebookSocialLoginCallback(){
$userSocial = Socialite::driver('facebook')->stateless()->user();
$user = User::where('email',$userSocial->email)->first();
\Auth::login($user);
return redirect()->intended($this->redirectPath());
}
将您的方法分开后,您将有不同的路线用于不同的社交登录,IMO 会更好,因为它们的返回参数略有不同,您可能希望将来为特定的社交登录执行附加功能。
推荐阅读
- java - 为什么这个边界不再起作用了?
- html - Until how many pixels should i do responsive the web page?
- azure-web-app-service - 如何输出通过 ARM 模板部署的应用服务的 IP 地址
- javascript - 如何获取json,离线存储和在线同步?
- c# - 在文件夹 C# 中搜索特定文件
- php - laravel 主域路由优先于动态子域
- reactjs - 当最大长度充满第一个输入时,将焦点从一个输入移动到反应 js 中的另一个输入
- python - 仅在 K-folds 交叉验证中增加训练集
- c# - 为什么要加密的数据长度无效?
- node.js - node.js 我可以为同一个项目使用多个 ssl 证书和密钥吗?如何?