Passport: Laravel 5.3 Laravel\Passport\Client uses the wrong database connection when requesting a token

Created on 10 Sep 2016  路  7Comments  路  Source: laravel/passport

So I followed Taylors introduction to Passport on Laracasts and when I attempted to POST to the OAuth server /oauth/token URL with the newly acquired code I get a 'homestead.oauth_clients' doesn't exist' error.

In other words it's using the wrong database connection! I set this up using SQLite and even hard coded 'default' => 'sqlite' in config/database.php. (.env is also set correctly)

In the end I had to add protected $connection = 'sqlite'; to the LaravelPassportClient.php file to get it to work. Obviously this is not really a suitable fix. Just wondering where I might look to see where it determines the connection to use.

Here is the complete errorlog for reference:

[2016-09-10 03:17:10] local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.oauth_clients' doesn't exist' in D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Connection.php:333
Stack trace:
#0 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Connection.php(333): PDO->prepare('select * from `...')
#1 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Connection.php(754): Illuminate\Database\Connection->Illuminate\Database\{closure}(Object(Illuminate\Database\MySqlConnection), 'select * from `...', Array)
#2 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Connection.php(717): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#3 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Connection.php(351): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#4 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1645): Illuminate\Database\Connection->select('select * from `...', Array, true)
#5 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1631): Illuminate\Database\Query\Builder->runSelect()
#6 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(613): Illuminate\Database\Query\Builder->get(Array)
#7 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(318): Illuminate\Database\Eloquent\Builder->getModels(Array)
#8 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(288): Illuminate\Database\Eloquent\Builder->get(Array)
#9 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php(168): Illuminate\Database\Eloquent\Builder->first(Array)
#10 [internal function]: Illuminate\Database\Eloquent\Builder->find('3')
#11 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3515): call_user_func_array(Array, Array)
#12 [internal function]: Illuminate\Database\Eloquent\Model->__call('find', Array)
#13 [internal function]: Laravel\Passport\Client->find('3')
#14 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3529): call_user_func_array(Array, Array)
#15 D:\Dev\Passport\vendor\laravel\passport\src\ClientRepository.php(15): Illuminate\Database\Eloquent\Model::__callStatic('find', Array)
#16 D:\Dev\Passport\vendor\laravel\passport\src\ClientRepository.php(15): Laravel\Passport\Client::find('3')
#17 D:\Dev\Passport\vendor\laravel\passport\src\ClientRepository.php(26): Laravel\Passport\ClientRepository->find('3')
#18 D:\Dev\Passport\vendor\laravel\passport\src\Bridge\ClientRepository.php(37): Laravel\Passport\ClientRepository->findActive('3')
#19 D:\Dev\Passport\vendor\league\oauth2-server\src\Grant\AbstractGrant.php(160): Laravel\Passport\Bridge\ClientRepository->getClientEntity('3', 'authorization_c...', 'yExYq8aAYCbbtB6...', true)
#20 D:\Dev\Passport\vendor\league\oauth2-server\src\Grant\AuthCodeGrant.php(74): League\OAuth2\Server\Grant\AbstractGrant->validateClient(Object(Zend\Diactoros\ServerRequest))
#21 D:\Dev\Passport\vendor\league\oauth2-server\src\AuthorizationServer.php(180): League\OAuth2\Server\Grant\AuthCodeGrant->respondToAccessTokenRequest(Object(Zend\Diactoros\ServerRequest), Object(League\OAuth2\Server\ResponseTypes\BearerTokenResponse), Object(DateInterval))
#22 D:\Dev\Passport\vendor\laravel\passport\src\Http\Controllers\AccessTokenController.php(63): League\OAuth2\Server\AuthorizationServer->respondToAccessTokenRequest(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#23 D:\Dev\Passport\vendor\laravel\passport\src\Http\Controllers\HandlesOAuthErrors.php(25): Laravel\Passport\Http\Controllers\AccessTokenController->Laravel\Passport\Http\Controllers\{closure}()
#24 D:\Dev\Passport\vendor\laravel\passport\src\Http\Controllers\AccessTokenController.php(64): Laravel\Passport\Http\Controllers\AccessTokenController->withErrorHandling(Object(Closure))
#25 [internal function]: Laravel\Passport\Http\Controllers\AccessTokenController->issueToken(Object(Zend\Diactoros\ServerRequest))
#26 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(47): call_user_func_array(Array, Array)
#27 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Route.php(190): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Laravel\Passport\Http\Controllers\AccessTokenController), 'issueToken')
#28 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Route.php(144): Illuminate\Routing\Route->runController()
#29 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Router.php(642): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#30 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#32 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Router.php(644): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#33 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Router.php(618): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#34 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Router.php(596): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#35 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(267): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#36 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#37 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#38 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#41 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#42 D:\Dev\Passport\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#43 D:\Dev\Passport\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#44 {main}

Most helpful comment

After further investigation I found this issue is directly related to DotEnv.

I added some code in the Builder class and could see that the database configuration being used was from the CLIENT and not the SERVER configuration.

I ran artisan config:cache and the problem went away!

Morale of the story is don not rely on DotEnv even in development environment!

All 7 comments

I also have some issues

  1. i want Passport running on different mysql connection, how can i configure this?
  2. i want to use Mongo as the database, can i substitute the Repository to use my own custom Repository?

btw @taylorotwell
https://github.com/laravel/passport/blob/master/src/PassportServiceProvider.php#L209
you should use container when instantiating TokenRepository

 $this->app->make->(TokenRepository::class);

After further investigation I found this issue is directly related to DotEnv.

I added some code in the Builder class and could see that the database configuration being used was from the CLIENT and not the SERVER configuration.

I ran artisan config:cache and the problem went away!

Morale of the story is don not rely on DotEnv even in development environment!

This is gold! I spent six hours trying to solve this issue. Thank you.

issue still persists in version 7 as of mid 2019 and your solution still works
Thanks @MarkLL

@MarkLL u r my lifesaver! been working for this for 2 days and yet cant figure out the problem myself. thanks for your solution

php artisan config:cache fixed it for me in 2020

I have same issue, and i run php artisan config:cache and still have the same error

Note : i use laravel 8

Was this page helpful?
0 / 5 - 0 ratings

Related issues

s4uron picture s4uron  路  3Comments

ghost picture ghost  路  3Comments

brryfrmnn picture brryfrmnn  路  3Comments

SwiTool picture SwiTool  路  3Comments

cookiejarblush picture cookiejarblush  路  4Comments