coderello / laravel-passport-social-grant

🔒 API authentication via social networks for your Laravel application
https://packagist.org/packages/coderello/laravel-passport-social-grant
MIT License
174 stars 20 forks source link

Declaration of Illuminate\Contracts\Auth\Authenticable is not compatible #14

Closed kresnasatya closed 5 years ago

kresnasatya commented 5 years ago

Hi @hivokas , I just try your tutorial based on your blog post: https://hivokas.com/api-authentication-via-social-networks-for-your-laravel-application. I got an exception that says:

Declaration of App\Services\SocialUserResolver::resolveUserByProviderCredentials(string $provider, string $accessToken): ?Illuminate\Contracts\Auth\Authenticable must be compatible with Coderello\SocialGrant\Resolvers\SocialUserResolverInterface::resolveUserByProviderCredentials(string $provider, string $accessToken): ?Illuminate\Contracts\Auth\Authenticatable

My SocialUserResolver.php class:

<?php

namespace App\Services;

use Exception;
use Coderello\SocialGrant\Resolvers\SocialUserResolverInterface;
use Illuminate\Contracts\Auth\Authenticable;
use Laravel\Socialite\Facades\Socialite;

class SocialUserResolver implements SocialUserResolverInterface
{
    /**
     * Resolve user by provider credentials.
     *
     * @param string $provider
     * @param string $accessToken
     *
     * @return Authenticable|null
     */
    public function resolveUserByProviderCredentials(string $provider, string $accessToken): ?Authenticable
    {
        // Return the user that corresponds to provided credentials.
        // If the credentials are invalid, then return NULL.

        $providerUser = null;

        try {
            $providerUser = Socialite::driver($provider)->userFromToken($accessToken);
        } catch (Exception $exception) {

        }

        if ($providerUser) {
            return (new SocialAccountsService())->findOrCreate($providerUser, $provider);
        }

        return null;
    }
}

I'm using Laravel version 5.8.32.

samtoya commented 5 years ago

Hi @satyakresna

Try this....

if ( $providerUser ) { return User::whereHas('identities', function (Builder $builder) use ($providerUser, $provider) { $builder->where('provider_id', $providerUser->getId()) ->where('provider_name', $provider); })->first(); }