inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.36k stars 428 forks source link

Not working with cross origin, while plain axios does #1490

Closed kskrlinnorth2 closed 1 year ago

kskrlinnorth2 commented 1 year ago

Versions:

Describe the problem:

Using Laravel with subdomain routing. Plan is to have single login route to which users can login from different subdomains. When using inertia form (useForm) this doesn't work, failing with 419 invalid csrf token.

When using axios everything works, but including plain axios along with inertia forms is another layer which would be good to avoid due the code maintenance and consistency.

Yes, I could duplicate login route for each subdomain (and all other shared routes), but that's not programming, just bad code.

Steps to reproduce:

Axios default setup

import axios from "axios";
window.axios = axios;

window.axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
// Without this example number 2 doesn't work
window.axios.defaults.withCredentials = true;

Example 1. Inertia form - not working 419 error

const form = useForm({
  email: "",
  password: "",
  remember: false,
});

// route('login') is myapp.com/login, request is sent from foo.myapp.com
const submit = () => {
  form.post(route("login"));
};

Example 2. Axios form - working 422 or 200 depends on data

// route('login') is myapp.com/login, request is sent from foo.myapp.com
const submit = () => {
  axios
    .post(route("login"), {
      email: "myemail@example.com",
      password: "password",
      remember: false,
    })
    .then(function (response) {
      console.log(response.data);
    })
    .catch(function (error) {
      console.log(error);
    });
};

How to make it work with Inertia since it's also using axios in the background?

RobertBoes commented 1 year ago

Inertia uses the History API, so cross-domain navigation wouldn't work anyway

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. https://developer.mozilla.org/en-US/docs/Web/API/History/pushState

kskrlinnorth2 commented 1 year ago

So only way is to use axios (or something similar) directly?

reinink commented 1 year ago

Hey! Thanks so much for your interest in Inertia.js and for sharing this issue/suggestion.

In an attempt to get on top of the issues and pull requests on this project I am going through all the older issues and PRs and closing them, as there's a decent chance that they have since been resolved or are simply not relevant any longer. My hope is that with a "clean slate" me and the other project maintainers will be able to better keep on top of issues and PRs moving forward.

Of course there's a chance that this issue is still relevant, and if that's the case feel free to simply submit a new issue. The only thing I ask is that you please include a super minimal reproduction of the issue as a Git repo. This makes it much easier for us to reproduce things on our end and ultimately fix it.

Really not trying to be dismissive here, I just need to find a way to get this project back into a state that I am able to maintain it. Hope that makes sense! ❤️