I created an application with two guards, web and admin. If the admin login and create an new user and append role to user, the guard name of the user should be admin instead of web.
Database


auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'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", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| 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.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
],
],
];
Admin.php
<?php
namespace App;
use Hyn\Tenancy\Traits\UsesSystemConnection;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
use Notifiable, UsesSystemConnection;
protected $guard = 'admin';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
I don't see anything in your screenshots or code that is forcing it to web.
Instead, I see that your CustomerController is calling assignRole, and the error is saying role-does-not-exist for admin, which maybe isn't surprising since the screenshot of your database shows it as being defined as Admin.
I use an custom guard and would like to create an new user with guard ‚web‘ There is my current problem.
I'm not sure I understand your question fully.
Your posts indicate that you have 2 models: User and Admin, associated with guard web and admin respectively.
Since the non-Admin model is User, to create one you would User::create().
Closing for now. If this is still an issue, please post back.
Can i use both api and web guard??
@programmermarvin wrote:
Can i use both api and web guard??
Yes and no. Depends what you're really asking. Do you mind opening a separate issue to discuss it, and provide more explanation of what you're trying to accomplish and the reasoning behind it?
Hello, i have the same problem, how do you define a user's guard?
` //Create a new user object
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'guard_name' => 'api',
]);
$role = Role::create(['name' => 'administrator', 'guard_name' => 'api']);
$permission = Permission::create(['name' => 'administration', 'guard_name' => 'api']);
$user->assignRole('administrator');
$user->save();`
There is my code, i got this message: There is no role named administrator.
Please help me
@Nvservicing if you need to be that specific, you can do the assignment like this:
$roleToAssign = Role:: findByName('administrator', 'api');
$user->assignRole($roleToAssign);
View/Components/User/CseForm.php
public function roles() : array {
return DB::table('roles')->select(['name', 'guard_name'])->get()->toArray();
}
views/components/user/cse-form.blade.php
@foreach($roles as $role)
<input name="roles[]" type="checkbox" class="" value="{{ json_encode($role) }}">
{{ $role->name }}
<br>
@endforeach
UserController @store & @update
#If validation passed sync the roles first
$roles = collect($request->roles)->map(function($role) use (&$roles){
$role = json_decode($role);
$roles = Role::findByName($role->name, $role->guard_name);
return $roles;
});
$user->update($request->except(['_token','roles']))->syncRoles($roles);
Most helpful comment
@Nvservicing if you need to be that specific, you can do the assignment like this: