Back

[5.8] custom Auth::gaurd() not working as intended


I am a bit stuck using a custom login for my Admin users.

I pref to use another Model for my admin just to make sure everything is separated. I did see a lot examples to made a guard for the login. So watching some tutorials and read some articles I gave it a go and got stuck.

The guard kind of worked. When I change the default to 'admin' like this:

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

And try to login the regular way it worked. But when i try to use a different guard outside the default one like this: (default is set back to web)

Auth::guard('admin')->attempt($request->only('email', 'password'));

But then nothing happend. No errors and no logged in user. Is there something I am missing here? Or is there a very small change that its Laravel bug?

Code explanation

For my Admin I made a second guard.

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        //'hash' => false,
    ],
],

using providers:

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

And yes my Admin model is located as namespace App\Models\Users;

In the login controller I changed:

Auth::attempt($request->only('email', 'password'));

to:

Auth::guard('admin')->attempt($request->only('email', 'password'));

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us