Laravel Socialite Google登录无法使用个人资料范围

问题描述 投票:0回答:1

我正面临使用Socialite软件包在Laravel项目中执行Social登录的问题。

[尝试使用Google登录时,它会为我提供空错误字符串,并且没有异常消息,但是当我尝试删除配置文件范围时,它登录了我,但没有登录用户的配置文件数据。

我正在搜索结果,但即使在社交名媛Github上也一无所获

这是我控制器中的Socialite软件包中的代码


/**
     * 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);
    }
laravel google-signin laravel-socialite
1个回答
0
投票

经过大量搜索并做了不同的工作,终于找到了解决方案。

我们必须启动Google OAUth2 stateless()

工作代码

    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 */
    }
© www.soinside.com 2019 - 2024. All rights reserved.