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

Cannot use multiple guards #2794

Open
bimix opened this issue Jan 24, 2025 · 8 comments
Open

Cannot use multiple guards #2794

bimix opened this issue Jan 24, 2025 · 8 comments

Comments

@bimix
Copy link

bimix commented Jan 24, 2025

Description

I have a controller where I create users. I currently have 2 guards: 'web' and 'mobile'. When creating users with guard_name in roles/permissions as 'web' it works fine because the Spatie functions such as syncRoles and assignRole use 'web' by default. But how do I force it to use 'mobile' guard name when the user role is 'app-user' ?
Note: I am not talking about the authentication part, only the user creation.

I marked this as a bug because the package doc states that it does support multiple guard names for roles but doesn't seem to support it.

public function store(UserRequest $request)
 {
     $validated = $request->validated();
     $role = Role::findOrFail($validated['role_id']);
     $guardName = $role->name === RolesEnum::APPUSER->value ? GuardEnum::MOBILE->value : GuardEnum::WEB->value;
     $role->guard_name = $guardName;        
     $user = User::create($validated);
     $user->syncRoles([$role->name]); // It fails in syncRoles function
     return ModelResource::make($user)->withCode(201);
 }    

My auth.php configs:

'defaults' => [
     'guard' => env('AUTH_GUARD', 'web'),
     'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
 ],
 'guards' => [
     'web' => [
         'driver' => 'session',
         'provider' => 'users',
     ],
     'mobile' => [
         'driver' => 'sanctum',
         'provider' => 'users',
         'hash' => false,
     ],
     'api' => [
         'driver' => 'sanctum', // You may use 'sanctum' or 'passport' if configured
         'provider' => 'users',
         'hash' => false, // Optional, useful if using Laravel Passport or Sanctum
     ],
 ]

and the User model:

class User extends Authenticatable
{
	use HasApiTokens, HasFactory, Notifiable, HasRoles;

	protected $table = 'users';

	protected $guard_name = ['web', 'mobile'];
}

Error:

There is no role named app-user for guard web

Steps To Reproduce

  1. Follow all steps in doc to install and configure the package.
  2. Create a controller to create users.
  3. Create two users with two different guard names.
    ...

Example Application

No response

Version of spatie/laravel-permission package:

^6.10

Version of laravel/framework package:

^11.0

PHP version:

8.3.15

Database engine and version:

No response

OS: Windows/Mac/Linux version:

Ubuntu 22.04.5 LTS

@parallels999
Copy link
Contributor

Maybe related #2729 (comment)

@bimix
Copy link
Author

bimix commented Jan 24, 2025

Maybe related #2729 (comment)

@parallels999 not related to my issue. That post talks about the wrong default guard. In my case, the correct default guard is being used, but I am having trouble assigning a role to a new user because that role uses another guard (not the default one).

@parallels999
Copy link
Contributor

So, you could add a default unique guard for everything, Would that solve your problem?
or do you need to have different guard roles/permissions?

@bimix
Copy link
Author

bimix commented Jan 24, 2025

So, you could add a default unique guard for everything, Would that solve your problem? or do you need to have different guard roles/permissions?

@parallels999 I need 2 guards as I have web and mobile app users. The 'web' guard is not typically used for mobile apps.

@parallels999
Copy link
Contributor

It seems that you did not understand correctly

I'm not saying that you delete guards in Laravel, I'm saying that you use a default guard_name in Laravel permission, so you can reuse the same roles/permissions in any Laravel guard

@bimix
Copy link
Author

bimix commented Jan 24, 2025

guard_name

@parallels999 ok so I can use the same guard_name for all roles/permissions, but I can keep using different guards for web and app users in Laravel?

@parallels999
Copy link
Contributor

so you can reuse the same roles/permissions in any Laravel guard

@bimix
Copy link
Author

bimix commented Jan 24, 2025

This is a workaround but its fine with me. I will leave this open meanwhile.

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

2 participants