简体   繁体   中英

Laravel 9: after success login systems will log out immediately

I've created new guard for Admins in my laravel 9 project. Then I want to log in returns me true at that time but its log me out immediately

here is my admin model:

class Admin extends Authenticatable implements HasMedia
{
    use HasFactory, SoftDeletes, Notifiable, InteractsWithMedia, HasScopeWatcher;

    protected $guard = "admin";

    protected $fillable = [
        'first_name',
        'last_name',
        'username',
        'email',
        'password',
        'phone',
        'national_code'
    ];

    protected $appends = [
        'full_name',
        'avatar'
    ];

    protected $hidden = [
        'password',
    ];

Here Is auth.php:

<?php

return [


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

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins'
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'api-experts' => [
            'driver' => 'passport',
            'provider' => 'experts',
        ]
    ],

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

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

   

];

Here is my routes:

Route::get('/',function(){
    dd(Auth::guard('admin')->check()); // after I call /login this route returns me false
});
Route::get('/login',function(){
    Auth::guard('admin')->loginUsingId(1);
    dd(auth('admin')->check()); // Returns Me true
});

I also tries Both Auth::guard('admin')->attempt() and Auth::guard('admin')->loginUsingId(1); both result are same.

and also I tried to change my session driver to database but again same.

The response with the proper cookies to set the needed values to identify the admin for the next request(s) is not sent when you "dump and die" dd()

Just remove it

Route::get('/',function(){
    dd(Auth::guard('admin')->check()); // after I call /login this route returns me false
});
Route::get('/login',function(){
    Auth::guard('admin')->loginUsingId(1);
    // remove this dd() so a response with the proper encrypted cookies gets to the client side
});

In .env I've changed SESSION_DRIVER to cookie and everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM