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

解决Laravel+InertiaJS登录重定向时的409 Conflict错误

登录后重定向至仪表盘触发409 Conflict错误(Laravel 11 + InertiaJS + Vue 3)

环境详情

  • 后端:Laravel 11、InertiaJS、PHP 8.4
  • 前端:Vue 3、@inertiajs/inertia

前端代码

onSubmit() {
    this.$refs.login.validate((valid) => {
        if (valid) {
            this.form.post(route('login'), {
                onFinish: () => {
                    this.form.reset('password'),
                    this.loading = false;
                },
                onError: (errors) => {
                    if (errors.message) {
                        this.hasError = true
                        this.error = errors.message;
                    }
                }
            });
        }
    });
},

Laravel后端代码

public function login(Request $request) { 
    $validEmail = 'admin@admin.com'; 
    $validPassword = 'password';

    // Verify credentials
    if ($request->email === $validEmail && $request->password === $validPassword) {
        
        $request->session()->regenerate();

        if ($request->header('X-Inertia')) {
            return Inertia::location(route('dashboard'));
        }

        return redirect()->route('dashboard');
    }

    return Inertia::render('Auth/Login', [
        'errors' => ['message' => 'Invalid credentials.'],
    ]);
}

问题

登录成功后尝试重定向至/dashboard时触发409 Conflict错误,需明确InertiaJS中该场景的正确处理方式,排查后端逻辑或前端配置的遗漏点。

已尝试方案

  • 使用Inertia::location(route('dashboard'))
  • 返回redirect()->route('dashboard')
  • 返回JSON响应并在前端通过this.$inertia.visit('/dashboard')显式处理重定向
  • 检查X-Inertia、X-Inertia-Version、X-XSRF-TOKEN等请求头正确性

解决方案

1. 修正后端重定向逻辑

Inertia会自动识别Laravel的302重定向并处理客户端跳转,无需判断X-Inertia请求头,且Inertia::location()仅适用于外部非Inertia路由跳转,内部路由直接使用普通重定向即可。修改后端代码:

public function login(Request $request) { 
    $validEmail = 'admin@admin.com'; 
    $validPassword = 'password';

    // Verify credentials
    if ($request->email === $validEmail && $request->password === $validPassword) {
        $request->session()->regenerate();
        // 直接返回普通重定向,Inertia自动处理客户端跳转
        return redirect()->route('dashboard');
    }

    return Inertia::render('Auth/Login', [
        'errors' => ['message' => 'Invalid credentials.'],
    ]);
}

2. 确认前端表单实例正确性

确保前端使用的是Inertia提供的表单助手,而非自定义表单。如果是Vue选项式API,表单需通过this.$inertia.form()初始化:

export default {
    data() {
        return {
            form: this.$inertia.form({
                email: '',
                password: '',
            }),
            loading: false,
            hasError: false,
            error: ''
        }
    },
    // ... 其余代码
}

3. 修复Inertia版本哈希不匹配

409 Conflict错误最常见原因是前端资源版本哈希与后端记录不匹配,解决步骤:

  • 编辑config/inertia.php,确保version配置正确:
    'version' => Vite::hash(), // Laravel 11默认使用Vite,用此方法获取构建哈希
    
  • 重新构建前端资源:执行npm run build
  • 清除浏览器缓存(Ctrl+F5强制刷新),加载最新资源

4. 验证会话与CSRF配置

  • 检查config/session.php,确保domain和secure适配当前环境(本地开发可设domain => null,secure => false)
  • 确认dashboard路由已正确配置auth中间件,避免未认证访问:
    Route::get('/dashboard', [DashboardController::class, 'index'])
        ->name('dashboard')
        ->middleware('auth');
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 02:07:26