laravel - Laravel Socialite Google Signin 不适用于个人资料范围
问题描述
我在使用 Socialite 包的 Laravel 项目中面临社交登录实现的问题。
当尝试使用 Google 登录时,它给了我空错误字符串并且没有异常消息,但是当我尝试删除配置文件范围时,它让我登录但没有登录用户的配置文件数据。
我搜索结果,但即使在社交名流 Github 上也一无所获
这是我的控制器中社交名流包中的代码
/**
* Obtain the user information from Google.
*
* @return \Illuminate\Http\Response
*/
public function googleLogin()
{
try {
// Not able to get the user data
$user = Socialite::driver('google')->user();
} catch (\Exception $e) {
return redirect('/login')->with('error','Try after some time');
}
$existingUser = User::where('social_id', $user->id)->first();
if (!empty($existingUser)) {
$getType = UserType::where('user_id',$existingUser['id'])->get()->toArray();
$count = count($getType);
if ($count == 2) {
return view('Front.types')->with('getTypes',$getType);
}elseif($count == 1){
if ($getType[0]['user_type'] == "consumer") {
\Auth::loginUsingId($existingUser['id']);
return redirect()->intended('/');
}
if ($getType[0]['user_type'] == "bizzowner") {
\Auth::loginUsingId($existingUser['id']);
return redirect('business/dashboard');
}
}
// if(!session()->has('url.intended'))
// {
// session(['url.intended' => url()->previous()]);
// }
}
我总是在用户显示屏上收到此错误消息
过一段时间试试
收到此异常消息
Laravel\Socialite\Two\InvalidStateException {#616 ▼
#message: ""
#code: 0
#file: "/home/greenalleymart/public_html/vendor/laravel/socialite/src/Two/AbstractProvider.php"
#line: 210
trace: {▼
/home/greenalleymart/public_html/vendor/laravel/socialite/src/Two/AbstractProvider.php:210 {▶}
/home/greenalleymart/public_html/app/Http/Controllers/Front/UserController.php:367 {▼
App\Http\Controllers\Front\UserController->googleLogin() …
› try {
› $user = Socialite::driver('google')->user();
› dd($user);
}
解决方案
经过大量搜索和做不同的事情,终于找到了解决方案。
我们必须启动 Google OAUth2stateless()
工作代码
public function redirectToGoogle(){
return Socialite::driver('google')
->stateless()
->redirect();
}
/**
* Obtain the user information from Google.
*
* @return \Illuminate\Http\Response
*/
public function googleLogin()
{
try {
$user = Socialite::driver('google')->stateless()->user();
// dd($user);
} catch (\Exception $e) {
return redirect('/login')->with('error','Try after some time');
}
$existingUser = User::where('social_id', $user->id)->first();
/** Your code */
}
推荐阅读
- php - 我应该始终将 ignore_user_abort 设置为 true 吗?
- java - Android Java 应用程序无法通过蓝牙发送选定的文件/数据
- c# - 如何使用 Activator.CreateInstance 创建列表
哪里 T 在运行时是未知的? - python - 在 Python OOP 中实现 datetime 模块
- python - 如何将一列字符串转换为列表
- javascript - 如何在没有 JS 的情况下使用嵌套 UL 获取大型菜单?
- javascript - 在 html 和纯 javascript 中使用 Angular 6 库
- python - Python对groupby求和两次
- graphql-java - 更改 graphql-java 的默认数据获取器行为
- r - 'ggarrange' 的组合图中缺少颜色