在facebook登录后重新加载我的页面时,AbstractProvider.php中的Laravel Socialite InvalidStateException

时间:2017-07-29 08:08:09

标签: php facebook laravel facebook-login laravel-socialite

我的预期结果是在Facebook登录后将用户重定向到我的主页。我使用的是Socialite,Laravel 5.4和Xampp。在能够通过facebook登录之后,我的url现在处于回调状态,其中回调调用重定向到主页的logincontroller。问题是重定向到主页后,我的网址现在有一些哈希值。

localhost / sampleProject / public / login / facebook / callback?code = AQBZpjdW ...

当我重新加载页面时,它在abstractprovider.php中显示错误无效状态异常。我错过了我的功能吗?

在我的登录控制器中

    public function redirectToProvider()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Obtain the user information from facebook.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('facebook')->user();

        return view('user-profile', compact('user',$user));
    }

在我的services.php文件中

'facebook' => [
    'client_id' => 'insert_app_id_here',
    'client_secret' => 'enter_app_secret_here',
    'redirect' => 'http://localhost/sampleProject/public/login/facebook/callback',
],

在我的routes / web.php里面

Route::get('login/facebook', 'Auth\LoginController@redirectToProvider');
Route::get('login/facebook/callback', 'Auth\LoginController@handleProviderCallback');

我也将这些包含在我的config / app.php

Laravel\Socialite\SocialiteServiceProvider::class,

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

2 个答案:

答案 0 :(得分:1)

你可以尝试这件事)

public function handleProviderCallback(Request $request)
{
    session()->put('state', $request->input('state'));
    $user = Socialite::driver('facebook')->user();

    return view('user-profile', compact('user',$user));
}

答案 1 :(得分:-2)

首先,运行以下命令:

composer require laravel/socialite

之后在 app / config.php 中,在提供商中添加以下行。

Laravel\Socialite\SocialiteServiceProvider::class,

之后在 app / config.php 中,在别名中添加以下行

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

config / services.php 中添加:

//Socialite
    'facebook' => [
        'client_id'     => '1234567890444',
        'client_secret' => '1aa2af333336fffvvvffffvff',
        'redirect'      => 'http://laravel.dev/login/callback/facebook',
    ],

现在创建两条路线,我的就是这样:

//Social Login
Route::get('/login/{provider?}',[
    'uses' => 'AuthController@getSocialAuth',
    'as'   => 'auth.getSocialAuth'
]);

Route::get('/login/callback/{provider?}',[
    'uses' => 'AuthController@getSocialAuthCallback',
    'as'   => 'auth.getSocialAuthCallback'
]);

您还需要为上面的路线创建控制器,如下所示:

<?php namespace App\Http\Controllers;

 use Laravel\Socialite\Contracts\Factory as Socialite;

 class AuthController extends Controller
 {

       public function __construct(Socialite $socialite){
           $this->socialite = $socialite;
       }


       public function getSocialAuth($provider=null)
       {
           if(!config("services.$provider")) abort('404'); //just to handle providers that doesn't exist

           return $this->socialite->with($provider)->redirect();
       }


       public function getSocialAuthCallback($provider=null)
       {
          if($user = $this->socialite->with($provider)->user()){
             dd($user);
          }else{
             return 'something went wrong';
          }
       }

 }

最后,将网站网址添加到您的Facebook应用中。我按照上面提到的解决方案的这篇文章。 https://www.cloudways.com/blog/social-login-in-laravel-using-socialite/