Vite doesn't send cookie headers... what am I missing? #6455
-
I'm trying to configure a proxy, but the cookie headers are not being sent. I'm trying to reach the following server URL:
The request being made:
I configured Vite like this:
The server requires authentication, a In the Vite application, the
Then it works, which means the cookie header is not being sent by Vite. What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 25 replies
-
same issue. @rodrigocfd did u solve this? |
Beta Was this translation helpful? Give feedback.
-
After trying many things, I found out that you need to set the entry from the root of the URL, so the cookies are saved with the correct path. In the example above, it would be: proxy: {
'/trunk': {
target: 'http://localhost:8080',
changeOrigin: true,
},
}, And then the fetch would be: fetch('/api/some/resource') I believe this should be in the documentation. I hope this works for you too, @monteki. |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issue described in the title, except I'm not doing any proxy stuff. I have a backend on one localhost port and a frontend (Vite dev server) on another localhost port. The server sends the cookies, but the frontend is not saving them in the browser. Anybody else working with SPAs having this issue? |
Beta Was this translation helpful? Give feedback.
-
I have same issue with vite +react. I don't know how to solve this. but it is work with CRA. |
Beta Was this translation helpful? Give feedback.
-
similar issue with my code. I have a Vue project with Vite and Tomcat as backend. vite.config.ts: export default defineConfig({
plugins: [vue()],
server: {
proxy: {
"/api/v1": {
target: "http://localhost:8080/bookit",
changeOrigin: true,
},
},
},
}); then i do login with basic auth using axios: async function login() {
axios.defaults.withCredentials = true;
axios
.get("/api/v1/login", {
withCredentials: true,
auth: {
username: username.value,
password: password.value,
},
})
.then(response => {
// here i store the session id in a pinia store
});
} and finally, when i'm trying to fetch some authorized data, i get async function callApi() {
axios
.get("/api/v1/users", {
headers: {
Authorization: "Bearer " + session,
Cookie: `JSESSIONID=${session}; Path=/bookit; HttpOnly;`,
},
})
.then((response) => {
console.log(response.data);
})
.catch((err) => {
console.log(err.message);
});
} when i look the at the cookies exchanged in the Network pane of firefox i get just the response cookie and obviously no request one. i'm kinda stuck and don't know what to do. any help would be greatly appreciated |
Beta Was this translation helpful? Give feedback.
-
configured the vite like this export default defineConfig(({command, mode}) => ({ when you are fetching data this url should be same /trunk/api/some/resource not required to write localhost |
Beta Was this translation helpful? Give feedback.
-
FYI: I ran into this problem as well and the solutions only mostly worked for me. Here's what I also had to do:
I post this all here in case it helps someone :-) |
Beta Was this translation helpful? Give feedback.
After trying many things, I found out that you need to set the entry from the root of the URL, so the cookies are saved with the correct path. In the example above, it would be:
And then the fetch would be:
I believe this should be in the documentation.
I hope this works for you too, @monteki.