-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Sanctum package not switching the database connection #51
Comments
Hi @redonosmanollaj, are you using the SwitchTenantDatabaseTask class? I have tried now and it seems ok: |
Yes tokens generated good, but when trying to call routes that have sanctum middleware, landlord works well, but tenantToken doesnt work because sanctum is trying to find token in landlord database, and getting Unauthenticated error message. |
Or vice versa, if I set tenant as default connection, tenant routes works well, but when I try to call landlord routes I get this error: Illuminate\Database\QueryException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: select * from #0 C:\laragon\www\strike-backend\vendor\laravel\framework\src\Illuminate\Database\Connection.php(631): Illuminate\Database\Connection->runQueryCallback('select * from |
Try to extend "Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask::class" with: config([
'database.default' => 'YOUR-TENANT-CONNECTION',
]); |
Yes it works. Thank you very much. |
Sorry about open this one again but not sure that setting default DB connection to "tenant" one is the best choice. Consider this post as another way of getting things working with Sanctum. Still on a early stage on the project using this and not tested entirely... See: Sanctum gives you the opportunity to overwrite it's default model like so: <?php
namespace App\Models\Tenant;
use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;
class PersonalAccessToken extends \Laravel\Sanctum\PersonalAccessToken
{
use UsesTenantConnection;
} Then you'll need a task that should set the new model into Sanctum <?php
namespace App\Multitenancy\Tasks;
use Laravel\Sanctum\Sanctum;
use Spatie\Multitenancy\Models\Tenant;
use App\Models\Tenant\PersonalAccessToken;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
use Laravel\Sanctum\PersonalAccessToken as BasePersonalAccessToken;
class SetSanctumPersonAccessTokenModelTask implements SwitchTenantTask
{
public function makeCurrent(Tenant $tenant): void
{
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
}
public function forgetCurrent(): void
{
Sanctum::usePersonalAccessTokenModel(BasePersonalAccessToken::class);
}
} Do not forget to add it to package config: /*
* These tasks will be performed when switching tenants.
*
* A valid task is any class that implements Spatie\Multitenancy\Tasks\SwitchTenantTask
*/
'switch_tenant_tasks' => [
// Some other tasks in here...
App\Multitenancy\Tasks\SetSanctumPersonAccessTokenModelTask::class
], In this way all the database stays the same and just the models that are supposed to work with multi tenancy will use the proper DB connection. Regards Nasko |
Hi, Thank you |
I figured out the issue |
Thank you, its work for me |
This is the best solution and worked for me also in 2023 |
My approach was a bit different with #51 (comment) and getting help from AI :) https://www.phind.com/search?cache=d0a2e8b6-5615-49db-8376-9f05a28e4bff in Laravel v10 version. Basically, I need to override the default sanctum model and that's it! use App\Models\Sanctum\PersonalAccessToken;
use Laravel\Sanctum\Sanctum;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
} |
I am using sanctum package for authorizing users in both tenant database connection and landlord database connection. In my case I'm using example.test domain for authorizing landlord users, and subdomains ex. subdomain.example.test for authorizing tenant users. I am using landlord connection as default and works good when trying to login as landlord user, but when trying to login in subdomain.example.test it looks like sanctum cannot change database connection to 'tenant'.
The text was updated successfully, but these errors were encountered: