Get Access Token after login - React + Axios

Asked

Viewed 59 times

0

I’m trying to globally set the token based on the login function response. Previously received in response the token information, but now, no more... Accessing the developer tools, Chrome Network tab, I see that the information is there. However, if I use the console.log(resp), lack of information.

function login(login) {
    axios
      .post("/login", login)
      .then((resp) => {
        setValidateToken(resp.headers.authorization);
        setStatusResponse(resp.status);
        axios.defaults.headers.common["Authorization"] =
          resp.headers.authorization;
      })
      .catch(() => alert("Nom d'utilisateur ou mot de passe invalide!"));
  }

Answer to the Axios:

{data: "", status: 200, statusText: "", headers: {…}, config: {…}, …}
config:
adapter: ƒ xhrAdapter(config)
baseURL: "*******"
data: "{"email":"*******","password":"******"}"
headers:
Accept: "application/json, text/plain, */*"
Content-Type: "application/json;charset=utf-8"
__proto__: Object
maxBodyLength: -1
maxContentLength: -1
method: "post"
timeout: 0
transformRequest: [ƒ]
transformResponse: [ƒ]
url: "/login"
validateStatus: ƒ validateStatus(status)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
__proto__: Object
data: ""
headers:
cache-control: "no-cache, no-store, max-age=0, must-revalidate"
content-length: "0"
expires: "0"
pragma: "no-cache"
__proto__: Object
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 200
statusText: ""
__proto__: Object
  • you are using the local state of the component, so when you enter the other component you lose that value. You should then use Contextapi or Redux to store and retrieve globally using this on all components if needed.

  • An example I myself implemented: https://answall.com/questions/476140/problemas-usando-react-context/476150#476150

  • the local state I use only p/ validate login, after redirecting, I select the token globally in the Xios.

  • 1

    Ultimately the problem was in the backend and not in the function I did... thank you.

No answers

Browser other questions tagged

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