Laravel pulse does not work with specific guard different than the default guard

147 views Asked by At

I tried to report the issue on github, but my issue was closed in like 2 hours and the response was: "Try another sites for answers". Did I do something wrong? Here is my problem:

I have a different guard for my admins. My guard is not the default guard.

`'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ],

/* |--------------------------------------------------------------------------

Authentication Guards Next, you may define every authentication guard for your application. Of course, a great default configuration has been defined for you here which uses session storage and the Eloquent user provider. All authentication drivers have a user provider. This defines how the users are actually retrieved out of your database or other storage mechanisms used by this application to persist your user's data. Supported: "session" */`

My guard is using a custom model named AdminUser instead of User. If I set up in AuthServiceProvider: Gate::define('viewPulse', function () { return true; });

receive 403 no matter what because I have a different guard for my admins and laravel pulse does not see that I'm logged in.

Also I use laravel nova for my admins.

I think the solution is to make the guard used for laravel pulse configurable in config/pulse.php

Steps To Reproduce:

Create a new guard: 'admins' => [ 'driver' => 'session', 'provider' => 'admins', ],

Create a new provider for your guard: 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\AdminUser::class, ],

Install laravel nova and change the default guard.

Log in into nova as admin

Add in AuthServiceProvider: Gate::define('viewPulse', function () { return true; });

Try to access laravel pulse. You will receive 403

I tried to configure laravel pulse for another guard than the default one and I got 403.

1

There are 1 answers

0
GRK On

Eventually I gave up on Pulse for now.


The sample code on Laravel Pulse doc is incorrect(?). The $user must be nullable.

(?User $user = null)
Gate::define('viewPulse', function (?User $user = null) {
    if ($user !== null) {
        return $user->isAdmin();
        //return in_array($user->email, [
        //   '[email protected]'
        //]);
    }
    
    return false;
});

How I found out about this is, that I checked PulseServiceProvider.php.

protected function registerAuthorization(): void
{
    $this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
        $gate->define('viewPulse', fn ($user = null) => $app->environment('local'));
    });
}