-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup passport db connection #247
Comments
You'd need to create an additional database connection, and then publish the passport migrations manually and use |
@heisian thanks, but I can't understand how it will help me. I'll try to explain more detailed. I have User model. It is stored in one database, e.g. In
In Laravel 5.3 related models are used default db connection (which is |
Yes actually I tried following through with using Passport in a secondary database, but was unable to get it to work correctly. So what I did end up doing was having the entire app default to an public function getTokens()
{
$tokens = \Laravel\Passport\Token::where('user_id', $this->id)->orderBy('created_at', 'desc')->get();
return $tokens;
} Or something to that effect. Basically you can't use the stock getters.... unfortunately. |
Might be worth us investigating some pull requests around allowing for multi-db relations, though we'd probably need to get really involved in their Slack channels as it sounds and I don't currently have the time to get that deep :P |
Yes, it works in your code. But, for example, in
so do I :-( |
What you want to do here is take the That way when |
Closing for lack of activity, hope you got the help you needed :) |
@heisian What change I need for multiple db connection in |
You need to override the default |
Is there any related examples . facing the same issue |
Is there any solution for this ? |
Duplicate your HasApiTokens trait and use your new HasApiTokens in User class
Then make a copy of Client and Token class from passport/src and replace the class names in your new HasApiTokens class, now you can define the $connection in Client and Token class like below
|
Instead of overwriting HasApiTokens you can also set your custom models in the boot method of AuthServiceProvider namespace App\Providers;
use Laravel\Passport\Passport;
use App\PassportClient as Client;
use App\PassportToken as Token;
use App\PassportAuthCode as AuthCode;
use App\PassportPersonalAccessClient as PersonalAccessClient;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
//
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::useTokenModel(Token::class);
Passport::useClientModel(Client::class);
Passport::useAuthCodeModel(AuthCode::class);
Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
}
} Custom client model namespace App;
use Laravel\Passport\Client;
class PassportClient extends Client
{
protected $connection = 'my_custom_connection';
protected $table = 'passport_oauth_clients';
} |
Was trying something similar myself, but while this works for clients and tokens. It fails for auth codes when trying to use the authorization code flow. |
I guess I will need to keep passport tables in the default database. Even after doing what @benbjurstrom said. Laravel still makes queries using the default database (see below). It is not using eloquent so, it is using the default DB instead of the one I want to. |
nvm, I wal able to set the right connection in one of my services providers =D
|
you go to passport directory in your project. passpoty->src: you can add "protected $connection = '[database-name]'" example |
@benbjurstrom has the good answer, but you should add |
Could not find the Refresh token model alongside the others from the latest release Please help thanks |
It's in the future 8.x that is there https://github.com/laravel/passport/tree/master/src |
I was able to change the connection by creating the passport.php file in the config folder.
|
@
Only on versions after May 9 2020 ;) |
Lost the past 2 hours with this :D I have a multi-tenant & multi-database laravel application. I used to create my own Passport Models setting the connection variable to the tenant one
For some reason (after upgrade from 7 -> 9), the connection variable is ignored. To fix this I had to publish the config file changing the connection value to 'tenant'. Is this excepted behaviour? |
Additional to https://github.com/laravel/passport/issues/247#issuecomment-424095961 Older version passport, you can override PassportServiceProvider In App\Providers\PassportServiceProvider.php: namespace App\Providers;
use DateInterval;
use Laravel\Passport;
use Laravel\Passport\Bridge;
use League\OAuth2\Server\Grant;
use DB;
class PassportServiceProvider extends Passport\PassportServiceProvider
{
/**
* Build the Auth Code grant instance.
*
* @return \League\OAuth2\Server\Grant\AuthCodeGrant
*/
protected function buildAuthCodeGrant()
{
return new Grant\AuthCodeGrant(
$this->app->make(Bridge\AuthCodeRepository::class),
$this->app->makeWith(Bridge\RefreshTokenRepository::class, ['database' => DB::connection('your-connection')]),
new DateInterval('PT10M')
);
}
/**
* Create and configure a Refresh Token grant instance.
*
* @return \League\OAuth2\Server\Grant\RefreshTokenGrant
*/
protected function makeRefreshTokenGrant()
{
$repository = $this->app->makeWith(Bridge\RefreshTokenRepository::class, ['database' => DB::connection('your-connection')]);
return tap(new Grant\RefreshTokenGrant($repository), function ($grant) {
$grant->setRefreshTokenTTL(Passport\Passport::refreshTokensExpireIn());
});
}
/**
* Create and configure a Password grant instance.
*
* @return \League\OAuth2\Server\Grant\PasswordGrant
*/
protected function makePasswordGrant()
{
$grant = new Grant\PasswordGrant(
$this->app->make(Bridge\UserRepository::class),
$this->app->makeWith(Bridge\RefreshTokenRepository::class, ['database' => DB::connection('your-connection')])
);
$grant->setRefreshTokenTTL(Passport\Passport::refreshTokensExpireIn());
return $grant;
}
} In config/app.php add to providers section /*
* Application Service Providers...
*/
...
App\Providers\PassportServiceProvider::class,
... Finally in composer.json, disable discovering original PassportServiceProvider: "extra": {
"laravel": {
"dont-discover": [
"Laravel\\Passport\\PassportServiceProvider"
]
}
}, And:
|
Hello.
Accordint to this laravel/framework#17511 I need to setup clearly connection name for passport models.
But I can't find any convenient way to do this.
Can somebody suggest any way to do this with minimal code changes ?
The text was updated successfully, but these errors were encountered: