Laravel Socialite不工作?

时间:2017-08-10 09:03:24

标签: laravel laravel-5 laravel-socialite

我在使用Laravel社交网站时遇到问题,这里是代码:

class SocialAuthController extends Controller
{
    public function redirect($provider) {
        return Socialite::driver($provider)->redirect();    
    } 
    public function callback(SocialAccountService $service, $provider) {
        $user = $service->createOrGetUser(Socialite::driver($provider));
        auth()->login($user, true);
        return redirect()->to('/');
    }
}

SocialAccountService:

public function createOrGetUser(Provider $provider)
{
    $providerUser = $provider->user();
    $providerName = class_basename($provider); 
    $account = SocialAccount::whereProvider($providerName)
        ->whereProviderUserId($providerUser->getId())
        ->first();
    if ($account) {
        return $account->user;
    }else{
        $account = new SocialAccount([
            'provider_user_id' => $providerUser->getId(),
            'provider' => $providerName
        ]);
        $user = User::whereEmail($providerUser->getEmail())->first();
        if (!$user) {
            $user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'avatar' => $providerUser->getAvatar(),
            ]);
        }
        $account->user()->associate($user);
        $account->save();
        return $user;
    }
}

现在我遇到的问题是:

  

FatalThrowableError类型错误:传递给的参数1   Illuminate \ Auth \ SessionGuard :: login()必须实现接口   Illuminate \ Contracts \ Auth \ Authenticatable,null给定,调用   /var/www/liveandnow/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php   在第294行

有人可以向我解释我做错了什么吗?因为我的代码没有看到任何问题

错误

(1/1) InvalidStateException 

in AbstractProvider.php (line 209)
at AbstractProvider->user()
in SocialAccountService.php (line 11)
at SocialAccountService->createOrGetUser(object(GoogleProvider))
in SocialAuthController.php (line 18)
at SocialAuthController->callback(object(SocialAccountService), 'google')
at call_user_func_array(array(object(SocialAuthController), 'callback'), array(object(SocialAccountService), 'provider' => 'google'))
in Controller.php (line 55)
at Controller->callAction('callback', array(object(SocialAccountService), 'provider' => 'google'))
in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(SocialAuthController), 'callback')
in Route.php (line 203)
at Route->runController()
in Route.php (line 160)
at Route->run()
in Router.php (line 574)
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php (line 41)
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in VerifyCsrfToken.php (line 65)
at VerifyCsrfToken->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ShareErrorsFromSession.php (line 49)
at ShareErrorsFromSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in StartSession.php (line 64)
at StartSession->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in AddQueuedCookiesToResponse.php (line 37)
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in EncryptCookies.php (line 59)
at EncryptCookies->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Router.php (line 576)
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php (line 535)
at Router->dispatchToRoute(object(Request))
in Router.php (line 513)
at Router->dispatch(object(Request))
in Kernel.php (line 176)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)

1 个答案:

答案 0 :(得分:0)

App\User中,您必须提供null模型的实例,但不是这样,您传递public function redirectToProvider($provider) { return Socialite::driver($provider)->redirect(); } public function handleProviderCallback($provider) { $socialMediaUser = Socialite::driver($provider)->user(); $user = $this->findOrCreateUser($socialMediaUser); auth()->login($user); return redirect()->route('home'); } public function findOrCreateUser($socialMediaUser) { $user = User::where('social_provider_id', $socialMediaUser->getId())->first(); if(is_null($user)) { $user = User::create([ 'social_provider_id' => $socialMediaUser->getId(), 'name' => $socialMediaUser->getName(), 'email' => $socialMediaUser->getEmail(), 'avatar' => $socialMediaUser->getAvatar() ]); } return $user; } 值。

以下是您正在寻找(测试)的代码:

$user = User::whereEmail($providerUser->getEmail())->first();

完整源代码:

https://github.com/xparthxvorax/Laravel-Socialite

注意:

Twitter没有提供用户的电子邮件,因此这对Twitter不起作用:

static Map<>