Skip to main content
1 de 2

Obter Token de acesso apos login - React + Axios

Estou tentando setar globalmente o token com base na resposta da funçãologin. Anteriormente rebia na reposta a informação do token, agora não mais... Acessando as ferramentas do desenvolvedor, aba Network do Chrome vejo que a informação esta la. Porem se utilizo o console.log(resp), falta informação.

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!"));
  }

RESPOSTA do 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