Error while logging in with React using Fetch

Asked

Viewed 80 times

0

I am building a software that has a Node API and a client in React.

The backend is configured and working. You are logging in and when logged in bring back the data you need.

However, with my React software, I am trying to log in through Fetch:

static logar(usuario) {
return (
  fetch("http://localhost:5000/login", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    body: JSON.stringify(usuario),
    credentials: "same-origin"
  })
    .then(parseJSON)
    .catch(function(error) {
      console.log("request failed", error);
    })
);

So far everything seems to work perfectly. It really logs, brings user data etc.

However, when consulting a new route, it simply returns to me saying that I am not logged in:

static getData() {
return (
  fetch("http://localhost:5000/teste", {
    method: "GET",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json"
    },
    credentials: "include",
    mode: "cors"
  })
    //   .then(checkStatus)
    .then(parseJSON)
    .catch(function(error) {
      console.log("request failed", error);
    })
);

I just need a light, because I’m not finding a solution and I’m already going crazy with it.

1 answer

1


I just found the problem:

While doing the post, I was putting same-origin in credentials. I switched to include and solved the problem in all software.

Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.