farisfaikar's avatar

"This page has expired" error when submitting tenant admin login (Laravel 10, Livewire 2, TenancyForLaravel 3)

For context, I'm using Livewire 2. I've read the docs for livewire integration. And I have added this in config/livewire.php:

'middleware_group' => [
    'web',
    'universal',
    InitializeTenancyByDomain::class, // or whatever tenancy middleware you use
],

That didn't work, so I tried pasting the Livewire 3 code too:

public function boot(): void
{
    // ...

    Livewire::setUpdateRoute(function ($handle) {
        return Route::post('/livewire/update', $handle)
            ->middleware(
                'web',
                'universal',
                InitializeTenancyByDomain::class, // or whatever tenancy middleware you use
            );
    });
}

Which obviously didn't work too as I'm using Livewire 2. I've also uncommented this line in tenancy.features config:

Stancl\Tenancy\Features\UniversalRoutes::class,

And added this to app/Http/Kernel.php:

'universal' => [],

Thus universal routes should be enabled (I hope I did that right). The error I get (using debugbar) when I log in suggests that it seems to be a CSRF token mismatch issue. Any help would be greatly appreciated, thank you

0 likes
4 replies
otavio_araujo's avatar

I Found a solution!

File: app/Providers/TenancyServiceProvider.php

Create a function called prepareLivewireForTenancy() with the content:

private function prepareLivewireForTenancy(): void
    {
        \Livewire::setUpdateRoute(function ($handle) {
            return Route::post('/livewire/update', $handle)
                ->middleware(
                    [
                        'web',
                        'universal',
                        Middleware\InitializeTenancyByDomain::class,
                    ])->name('livewire.update');
        });
    }

Then, call it on boot method:

public function boot()
    {
        $this->bootEvents();
        $this->mapRoutes();

        $this->makeTenancyMiddlewareHighestPriority();

        $this->prepareLivewireForTenancy();


    }

It solved the issue.

2 likes
Eazael's avatar

@otavio_araujo thanks for the solution.

I had to implement the solution you proposed together with 2 other solutions to get the routing, redirecting and detection between tenants and central domains right.

I also had to change the code a little bit:

private function prepareLivewireForTenancy(): void { Livewire::setUpdateRoute(function ($handle) { $centralDomains = config('tenancy.central_domains'); foreach ($centralDomains as $domain) { if (parse_url(request()->root())['host'] === $domain) { return Route::post('/livewire/update', $handle) ->middleware('web') ->domain($domain) ->name('livewire.update'); } } return Route::post('/livewire/update', $handle) ->middleware( [ 'web', Middleware\InitializeTenancyByDomain::class, ] )->name('livewire.update'); }); }​

The other 2 solutions I had to implement where following:

1.- I used what is referenced by stancl/tenancy official documentation: https:// github.com/archtechx/tenancy/pull/1180#issuecomment-2006098346

2.- I also implemented the Central Routes section of the quickstarter guide of stancl/tenancy https:// tenancyforlaravel.com/docs/v3/quickstart

Please or to participate in this conversation.