2

The default auth guard name in Laravel is 'web', but I found this confusing as it's provider is 'users'. Especially because for the admin guard the name is admin and the provider is admins (I'm using this for Nova). I'm on Laravel 6.5.2.

So I wanted to change this. In my config/auth I have:

'defaults' => [
 'guard' => 'user',
 'passwords' => 'users',
],

and

'guards' => [
 'user' => [
 'driver' => 'session',
 'provider' => 'users',
 ],
 'admin' => [
 'driver' => 'session',
 'provider' => 'admins',
 ]
],

and

'providers' => [
 'users' => [
 'driver' => 'eloquent',
 'model' => App\Models\User::class,
 ],
 'admins' => [
 'driver' => 'eloquent',
 'model' => App\Models\Admin::class,
 ],
],

In App\Models\User I have

protected $guard = 'user';

In App\Http\Controllers\Auth\LoginController I have

public function __construct()
{
 $this->middleware('guest')->except('logout');
 $this->middleware('guest:user')->except('logout');
 $this->middleware('guest:admin')->except('logout');
}

Now I get the error Auth guard [web] is not defined when logging in. Is it a bad idea to change it from web to user?

I found setting up the guards quite confusing, because you can define things in:

  • config/auth
  • In the model (protected $guard = 'guard_name';)
  • In the LoginController, RegisterController and ResetPasswordController as per the Laravel docs (protected function guard() { return Auth::guard('guard-name');})

In this article it's described that it's possible to do what I'm trying to achieve.

asked Nov 24, 2019 at 20:28
2
  • Please try to clear cache php artisan cache:clear. Commented Nov 24, 2019 at 20:54
  • Tried that, as well as php artisan config:clear and php artisan view:clear, didn't work unfortunately. Commented Nov 24, 2019 at 20:57

3 Answers 3

1

I think that happens because in the default app/Providers/Route service provider.php class, there is this method that registers your roites/web.php file with the web middleware. You should change the middleware name here as well:

protected function mapWebRoutes()
{
 Route::middleware('web')
 ->namespace($this->namespace)
 ->group(base_path('routes/web.php'));
}
answered Nov 24, 2019 at 21:08
Sign up to request clarification or add additional context in comments.

2 Comments

Changing web to user there gives Target class [user] does not exist.. I'm starting to think the conclusion is that it was indeed a bad idea to change it.
yeah this would just change what middleware group to us.
0

If you're using sanctum, make sure to add

'guard' => 'user',

to your sanctum.php config file.

You'll then need to run php artisan config:cache

answered Jan 27, 2023 at 3:11

Comments

0
  1. Change app/Providers/RouteServiceProvider.php
protected $namespaceUser = 'App\Http\Controllers\User';
public function boot()
{
 ...
 $this->routes(function () {
 
 Route::middleware('user')
 ->namespace($this->namespaceUser)
 ->group(base_path('routes/web.php'));
 ...
 });
}
  1. Redefine guard() function on LoginController.php
protected function guard()
{
 return Auth::guard('user');
}
answered Jan 27, 2023 at 3:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.