You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Auth切换至employees表后认证成功仍重定向至登录页问题

Laravel Auth 切换到employees表后认证成功仍跳登录的解决方案

问题根源

你的配置里默认Guard还是web,关联的是users用户提供者,而实际用的是employees表。即使Auth::attempt返回true,默认Guard依然无法识别会话中的员工用户,导致auth()->check()返回false,进而触发登录重定向。

修复步骤

  1. 修改默认Guard和密码重置配置
    打开config/auth.php,把默认Guard和密码驱动改成employees:

    'defaults' => [
        'guard' => 'employees',
        'passwords' => 'employees',
    ],
    
  2. 补充用户提供者(Providers)配置
    你的配置缺少providers节点,必须添加employees提供者指向对应模型:

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'employees' => [
            'driver' => 'eloquent',
            'model' => App\Models\Employee::class, // 你的员工模型
            // 若表名不是默认复数形式,手动指定:
            // 'table' => 'employees',
        ],
    ],
    
  3. 确保认证时使用正确的Guard
    若代码未修改默认Guard调用,改完默认配置后会自动使用employeesGuard;也可明确指定:

    if (Auth::guard('employees')->attempt($credentials)) {
        return redirect()->intended(route('employee.dashboard'));
    }
    
  4. 路由中间件指定Guard
    给需要认证的路由中间件明确指定employeesGuard:

    Route::middleware('auth:employees')->group(function () {
        Route::get('/employee/dashboard', [EmployeeController::class, 'dashboard'])->name('employee.dashboard');
    });
    
  5. 清理配置缓存
    执行命令确保配置生效:

    php artisan config:cache
    php artisan cache:clear
    

额外检查

  • 确认Employee模型继承Illuminate\Foundation\Auth\User类,且设置了正确的$fillable或$guarded属性。
  • 检查storage/framework/sessions目录权限,确保会话驱动可正常写入。

内容的提问来源于stack exchange,提问作者Almas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:48:22