mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 10:14:03 +00:00
1.8 KiB
1.8 KiB
| title | extends | section |
|---|---|---|
| Laravel Sanctum integration | _layouts.documentation | content |
Laravel Sanctum
Note: The
sanctumauth guard can't be used with [user impersonation]({{ $page->link('features/user-impersonation') }}) because user impersonation supports stateful guards only.
Laravel Sanctum works with Tenancy out of the box, with the exception of the sanctum.csrf-cookie route.
Making the csrf-cookie route work in the tenant app
To make the sanctum.csrf-cookie route work in the tenant app, do the following:
- Add
'routes' => falseto thesanctum.phpconfig - Publish the Sanctum migrations and move them to
migrations/tenant - Make Sanctum not use its migrations in the central app by adding
Sanctum::ignoreMigrations()to theregister()method in yourAuthServiceProvider - Add the following code to
routes/tenant.phpto override the originalsanctum.csrf-cookieroute:
Route::group(['prefix' => config('sanctum.prefix', 'sanctum')], static function () {
Route::get('/csrf-cookie', [CsrfCookieController::class, 'show'])
->middleware([
'web',
InitializeTenancyByDomain::class // Use tenancy initialization middleware of your choice
])->name('sanctum.csrf-cookie');
});
Making the csrf-cookie route work both in the central and the tenant app
To use the sanctum.csrf-cookie route in both the central and the tenant apps:
- Follow the steps in the previous section ("Sanctum's csrf-cookie route in the tenant app")
- Set up [universal routes]({{ $page->link('features/universal-routes') }})
- Remove
Sanctum::ignoreMigrations()from yourAuthServiceProvider'sregister()method - Remove
'routes' => falsefrom thesanctum.phpconfig - Add the
'universal'middleware to thesanctum.csrf-cookieroute in yourroutes/tenant.php