admin管理员组

文章数量:1124074

I'm encountering a 409 Conflict error when trying to redirect a user to the dashboard after a successful login in my Laravel + InertiaJS application. I've tried several approaches, but the issue persists, and I'm looking for a clear solution to resolve this problem.

Setup Details: Backend (Laravel):

Laravel 11 InertiaJS PHP 8.4 Frontend (Vue): Using @inertiajs/inertia Vue 3

Front end:

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;
                            }
                        }
                    });
                }
            });
        },

From laravel:

public function login(Request $request) { $validEmail = '[email protected]'; $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.'],
]);
}

Question: What is the best way to handle this scenario in InertiaJS to ensure proper redirection to /dashboard after login? Am I missing something in the backend logic or the frontend configuration?

Thank you in advance for your help!

What I've Tried: Using Inertia::location(route('dashboard')). Returning a redirect()->route('dashboard'). Returning JSON responses and handling redirection explicitly on the frontend using this.$inertia.visit('/dashboard'). Checked for correct headers like X-Inertia, X-Inertia-Version, and X-XSRF-TOKEN.

本文标签: phpHow to Resolve 409 Conflict with InertiaJS on Login Redirect in LaravelStack Overflow