Skip to content
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

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

Closed
CostiNec opened this issue Feb 9, 2024 · 4 comments

Comments

@CostiNec
Copy link

CostiNec commented Feb 9, 2024

Pulse Version

1.0@beta

Laravel Version

10.0

PHP Version

8.1.20

Livewire Version

3.0.2

Database Driver & Version

mysql Ver 8.0.36-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

Description

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"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admins' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],`

My guard is using a custom model named AdminUser instead of User. If I set up in AuthServiceProvider:

`Gate::define('viewPulse', function () {
        return true;
    });`

I 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

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

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

  3. Install laravel nova and change the default guard.

  4. Log in into nova as admin

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

  6. Try to access laravel pulse. You will receive 403

@driesvints
Copy link
Member

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

@timacdonald
Copy link
Member

@CostiNec, in the docs we reference the ResolvesUsers contract which you may implement if you have multiple guards or a more complex user configuration.

@gcjbr
Copy link

gcjbr commented Feb 12, 2024

That also happens for me.

I'll take some time to try to figure it out.

@sahapranta
Copy link

@gcjbr @CostiNec

<?php

namespace App\Services;

use Closure;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Http\Request;

class AuthorizePulse
{   
    public function handle(Request $request, Closure $next): mixed
    {
        // $this->gate->authorize('viewPulse');

        abort_unless(auth('admin')->check(), 404, 'Access denied');
        
        if ($this->gate->forUser(auth('admin')->user())->allows('viewPulse')) {            
            return $next($request);
        }

        abort(404, 'Access denied');
    }
}

Then in the config/pulse.php

'middleware' => [
  'web',
   AuthorizePulse::class, // add this class
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants