fix: bypass Nova error page for missing tenant

This commit is contained in:
MeghdadFadaee committed 2026-09-09 21:28:48 +03:30
1 parent eec91ac8e5
commit ef8c73531d
4 files changed
+31 -5

No files matched your search

+8
View File
@@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [1.1.3] - 2026-09-09
### Fixed
- Use Inertia's location response for tenant-less SPA visits so Nova navigates to the selector instead of displaying its unrecoverable-error page.
- Return tenant-selection responses directly from middleware so Nova's exception handler cannot replace them with its generic error page.
## [1.1.2] - 2026-09-09 ## [1.1.2] - 2026-09-09
### Changed ### Changed
@@ -53,6 +60,7 @@ All notable changes to this project will be documented in this file.
- Optional tenant-aware Nova resource base and reusable context helpers. - Optional tenant-aware Nova resource base and reusable context helpers.
- English and Persian translations. - English and Persian translations.
[1.1.3]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.3
[1.1.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.2 [1.1.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.2
[1.1.0]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.0 [1.1.0]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.1.0
[1.0.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.0.2 [1.0.2]: https://github.com/MeghdadFadaee/nova-tenancy/releases/tag/v1.0.2
+1 -1
View File
@@ -20,7 +20,7 @@ class TenantNotSelected extends RuntimeException implements Responsable
$destination = $this->selectorUrl(); $destination = $this->selectorUrl();
if ($request instanceof Request && $request->header('X-Inertia') === 'true') { if ($request instanceof Request && $request->header('X-Inertia') === 'true') {
return redirect()->to($destination); return response('', 409, ['X-Inertia-Location' => url($destination)]);
} }
if ($request instanceof Request && $request->expectsJson()) { if ($request instanceof Request && $request->expectsJson()) {
+4 -1
View File
@@ -5,6 +5,7 @@ namespace MeghdadFadaee\NovaTenancy\Http\Middleware;
use Closure; use Closure;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext; use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class RequireTenant class RequireTenant
@@ -14,7 +15,9 @@ class RequireTenant
/** @param Closure(Request): Response $next */ /** @param Closure(Request): Response $next */
public function handle(Request $request, Closure $next): Response public function handle(Request $request, Closure $next): Response
{ {
$this->currentTenant->requireModel(); if (! $this->currentTenant->hasTenant()) {
return (new TenantNotSelected)->toResponse($request);
}
return $next($request); return $next($request);
} }
+18 -3
View File
@@ -4,6 +4,7 @@ use Illuminate\Http\Request;
use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext; use MeghdadFadaee\NovaTenancy\Contracts\CurrentTenantContext;
use MeghdadFadaee\NovaTenancy\CurrentTenant; use MeghdadFadaee\NovaTenancy\CurrentTenant;
use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected; use MeghdadFadaee\NovaTenancy\Exceptions\TenantNotSelected;
use MeghdadFadaee\NovaTenancy\Http\Middleware\RequireTenant;
use MeghdadFadaee\NovaTenancy\NovaTenancy; use MeghdadFadaee\NovaTenancy\NovaTenancy;
use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider; use MeghdadFadaee\NovaTenancy\Providers\UserTenantProvider;
use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant; use MeghdadFadaee\NovaTenancy\Tests\Fixtures\Tenant;
@@ -46,7 +47,7 @@ it('provides scoped string bindings and strict accessors', function (): void {
->toThrow(TenantNotSelected::class); ->toThrow(TenantNotSelected::class);
}); });
it('redirects an inertia page request to tenant selection', function (): void { it('sends an inertia location response to tenant selection', function (): void {
$request = Request::create('/nova/dashboards/home', server: [ $request = Request::create('/nova/dashboards/home', server: [
'HTTP_ACCEPT' => 'text/html, application/xhtml+xml', 'HTTP_ACCEPT' => 'text/html, application/xhtml+xml',
'HTTP_X_INERTIA' => 'true', 'HTTP_X_INERTIA' => 'true',
@@ -55,8 +56,22 @@ it('redirects an inertia page request to tenant selection', function (): void {
$response = (new TenantNotSelected)->toResponse($request); $response = (new TenantNotSelected)->toResponse($request);
expect($response->getStatusCode())->toBe(302) expect($response->getStatusCode())->toBe(409)
->and($response->headers->get('Location'))->toEndWith('/nova/nova-tenancy'); ->and($response->headers->get('X-Inertia-Location'))->toEndWith('/nova/nova-tenancy');
});
it('returns the selection response directly from tenant middleware', function (): void {
$request = Request::create('/nova/dashboards/home', server: [
'HTTP_ACCEPT' => 'text/html, application/xhtml+xml',
'HTTP_X_INERTIA' => 'true',
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
]);
$response = (new RequireTenant(app(CurrentTenantContext::class)))
->handle($request, fn () => response()->noContent());
expect($response->getStatusCode())->toBe(409)
->and($response->headers->get('X-Inertia-Location'))->toEndWith('/nova/nova-tenancy');
}); });
it('returns a conflict response for a json api request', function (): void { it('returns a conflict response for a json api request', function (): void {