emarketingo
1 year ago
463
14
Laravel

Laravel Auth retrieve wrong/unexpected user

Posted 1 year ago by emarketingo

I have a problem with my laravel application, it is like a nightmare to me ? I've tried everything I know to solve it, but unfortunately I don't have enough luck for that, so I posted this question for you guys to guide me through this, the issue is that the auth system sometimes retrieve the wrong user as a current user... I mean sometimes when I am working in the admin area and while submitting a form or refreshing the page the application shows a wrong auth user (not the one I've logged in with)...and sometimes it logging me out completely. I don't know what causes this, I've tried many theories but it didn't work.

Any ideas on how to solve this?

Update :

My laravel version is 5.6.1 and I deployed the application on siteground servers.

I'm using database-based session, I also use multiple auth, one for regular users and the other for the admins. I tried to separate the users' table into two different tables one for regular users and the other for admin, however, it didn't solve the problem.

I've registered the multiple auth as the following

Config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],

And i'm doing route midleware for admin like this :

Route::group(['middleware' => [ 'auth:admin']], function()
{     

}

and for retrieving current admin I use

$user = Auth::guard('admin') 

Please sign in or create an account to participate in this conversation.