Cookie doesn't send different domain django and react

20 Views Asked by At

I deployed my django rest framework application and react app separately on vercel. So my urls for these are 'https://myproject.vercel.app/' and 'https://mybackendproject.vercel.app/'.

I use the sessionAuthentication and cookie for authentication. However, it seems that cookie is not set at frontend. So I got the error "{"detail":"Authentication credentials were not provided."}"

I confirmed that the cookie is stored in developer's tools application section. But when I send the post request like the below with cookies, it is not set.

useEffect(() => {
client
.get("/api/user", { 
  withCredentials: true,
  credentials: 'include',
  headers: {
    "X-CSRFToken": Cookies.get("csrftoken"),
  },})
  .then(function (res) {
    setCurrentUser(true);
  })
  .catch(function (error) {
    console.log(Cookies.get("csrftoken"));
    setCurrentUser(false);
  });

}, []);

Is there any good way to send cookie from frontend to backend? Currently, the cookie's samesite=lax.

I appreciate your opinion and advise!

0

There are 0 best solutions below