最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Correct way to use axios interceptors - Stack Overflow

programmeradmin5浏览0评论

I want to add jwt token to my axiosinstance in Login.js but it is giving me error

IDX12729: Unable to decode the header '[PII is hidden. For more details, see .]' as Base64Url encoded ...]

Here is my code:

Login.js

const printValues = e =>{

      axiosInstance.post('/auth', data)
.then(res =>{

  console.log("adding token");
  
  const config = axiosInstance.interceptors.request.use(function (config) {
    
    config.headers.Authorization =  res.data.token;

    return config;
  });

  axiosInstance.get('/User/GetUserByID/0', config)
  .then(res =>{
    //set user details
  })
  .catch(err =>{
    console.log(err);
  })
}

I want to add jwt token to my axiosinstance in Login.js but it is giving me error

IDX12729: Unable to decode the header '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' as Base64Url encoded ...]

Here is my code:

Login.js

const printValues = e =>{

      axiosInstance.post('/auth', data)
.then(res =>{

  console.log("adding token");
  
  const config = axiosInstance.interceptors.request.use(function (config) {
    
    config.headers.Authorization =  res.data.token;

    return config;
  });

  axiosInstance.get('/User/GetUserByID/0', config)
  .then(res =>{
    //set user details
  })
  .catch(err =>{
    console.log(err);
  })
}
Share Improve this question edited Feb 1, 2022 at 0:45 Samra asked Jan 31, 2022 at 2:35 SamraSamra 2,0239 gold badges41 silver badges84 bronze badges 4
  • the first request does not need authorization header, only second one does – Samra Commented Jan 31, 2022 at 2:43
  • Have you inspected what res.data.token actually contain? – Ricky Mo Commented Jan 31, 2022 at 3:03
  • yes it all works good if I use it directly in the headers, I will add details of what is working in my post – Samra Commented Jan 31, 2022 at 3:07
  • What is data in that POST /auth request? – Phil Commented Jan 31, 2022 at 3:59
Add a ment  | 

2 Answers 2

Reset to default 2

use doesn't return a config for you to pass into requests. As long as you are using the same instance, the config would get altered.

  axiosInstance.interceptors.request.use(function (config) {
    
    config.headers.Authorization =  res.data.token;

    return config;
  });

  axiosInstance.get('/User/GetUserByID/0')
  .then(res =>{
    //set user details
  })
  .catch(err =>{
    console.log(err);
  })

First, don't define interceptors within a response handler. That means you'll be adding an interceptor every time you make that request.

Typically you would keep your token state and interceptor separate from other application code. Wherever you created your axiosInstance is a good candidate.

For example...

import axios from "axios"

const axiosInstance = axios.create({
  // ..
})

const token = null // initial state

axiosInstance.interceptors.request.use(async config => {
  if (!token) {
    // note, use a separate axios instance for this request
    const { data } = await axios.post("/auth", dataFromSomewhere)
    token = data.token
  }

  config.headers.Authorization = `Bearer ${token}` // you may need "Bearer" here
  return config
})

Now you can use axiosInstance to make requests and it will transparently resolve your authorisation token if required before continuing.

const printValues = e => {
  axiosInstance.get("/User/GetUserByID/0")
  .then(res =>{
    //set user details
  })
  .catch(err =>{
    console.log(err);
  })
}
发布评论

评论列表(0)

  1. 暂无评论