Search code examples
phplaraveloauth-2.0dingo-api

Getting error trying to get authenticated user


Using dingo/api along with lucadegasperi/oauth2-server-laravel. Authenticating a user is fine and I get an access token back but any time I make another request I get the following error:

call_user_func() expects parameter 1 to be a valid callback, no array or string given

I'm using the Service Provider option listed in the dingo/api docs and it's definitely setting the user resolver (I'd var_dump'd the resolver in the setUserResolver method).

My OauthServiceProvider is below.

<?php namespace App\Providers;
use Dingo\Api\Auth\Auth;
use Dingo\Api\Auth\Provider\OAuth2;
use App\User\User;
use Illuminate\Support\ServiceProvider;

class OAuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app[Auth::class]->extend('oauth', function ($app) {
            $provider = new OAuth2($app['oauth2-server.authorizer']->getChecker());

            $provider->setUserResolver(function ($id) {
                return User::first();
                // Logic to return a user by their ID.
            });

            $provider->setClientResolver(function ($id) {
                // Logic to return a client by their ID.
            });

            return $provider;
        });
    }

    public function register()
    {
        //
    }
}

Solution

  • So turns out I was completely off in where I was looking. In config/api.php, in my auth settings I had

            'oauth2' => Dingo\Api\Auth\Provider\OAuth2::class,
    

    Should have just been

            'oauth' => Dingo\Api\Auth\Provider\OAuth2::class,