Skip to main content
adicionou 8 caracteres ao conteúdo; tags editadas
Fonte Link
Luiz Felipe
  • 36,5mil
  • 10
  • 74
  • 154

Estou tentando setar globalmente o token com base na resposta da funçãologinfunção login. Anteriormente rebiarecebia na reposta a informação do token, mas agora, não mais... Acessando as ferramentas do desenvolvedor, aba Network do Chrome, vejo que a informação esta la. PoremPorém, se utilizo o console.log(resp)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 Reposta 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

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

Estou tentando setar globalmente o token com base na resposta da função login. Anteriormente recebia na reposta a informação do token, mas agora, não mais... Acessando as ferramentas do desenvolvedor, aba Network do Chrome, vejo que a informação esta la. Porém, 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!"));
  }

Reposta 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
Fonte Link

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