Laravel \ Socialite \ Two \ InvalidStateException No message Facebook Socialite错误

时间:2019-02-07 03:51:39

标签: php laravel facebook laravel-socialite

我正在使用facebook作为我的应用程序的登录名。用户成功登录后,他们将被重定向到我的用户主页。但是,当我尝试在登录后刷新页面时,会引发错误:

  

Laravel \ Socialite \ Two \ InvalidStateException   没有消息

这是显示的代码:

public function user()
{
    if ($this->hasInvalidState()) {
        throw new InvalidStateException; // this line is highlighted
    }

    $response = $this->getAccessTokenResponse($this->getCode());

    $user = $this->mapUserToObject($this->getUserByToken(
        $token = Arr::get($response, 'access_token')
    ));

    return $user->setToken($token)
                ->setRefreshToken(Arr::get($response, 'refresh_token'))
                ->setExpiresIn(Arr::get($response, 'expires_in'));
}

这是我的控制器代码:

<?php
 namespace App\Http\Controllers\Auth;

 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use Socialite;

 class SocialAccountController extends Controller
{
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}
/**
 * Redirect the user to the SNS authentication page.
 *
 * @return Response
 */
public function redirectToProvider($provider)
{
    if ($provider !== 'facebook') {
        return abort(404);
    }

    return Socialite::with($provider)->redirect();
}

public function handleProviderCallback(\App\Models\User $accountService, $provider)
 {
    try {
        $user = Socialite::with($provider)->user();
        $create['name'] = $user->getName();
        $create['email'] = $user->getEmail();
        $create['facebook_id'] = $user->getId();

        $user = $accountService->addNew($create);

        return view ('user.home')->withDetails($user)->withService($provider);
    } catch(Exception $e){
        return redirect('/login');
    }
}
}

我找到了一些答案,但对我而言并不有效。我现在坚持了两天。我该如何解决?

2 个答案:

答案 0 :(得分:0)

当您从OAuth提供程序(在本例中为Facebook)获得回调时,将所需的数据存储到数据库中,然后将用户重定向到帐户页面或另一页上,以确认链接成功。

该回调URL专门用于OAuth提供程序提供的凭据。首次命中后,这些凭据将过期,刷新页面会导致错误。通过强制将用户重定向到另一个页面,他们将无法刷新并导致相同的问题。

答案 1 :(得分:0)

尝试刷新client_secret并运行php artisan cache:clear

composer dump-autoload。对我来说很好。