axios get react with authentication token code example
Example 1: how to authenticate token in react using axios
const api = `your api here`
axios.get(api, { headers: {"Authorization" : `Bearer ${token}`} })
.then(res => {
console.log(res.data);
this.setState({
items: res.data,
isLoaded : true,
redirectToReferrer: false
})
Example 2: get token from post axios react
const refreshToken = () => { let currUser = JSON.parse(localStorage.getItem("my_app_user")); let getUserFormData = new FormData(); getUserFormData.append("grant_type", "refresh_token"); getUserFormData.append("refresh_token", currUser.refresh_token); return new Promise((resolve, reject) => { my_app .post(`${URL}/token/url/`, getUserFormData, { headers: { Authorization: "Basic {secret_key}" } }) .then(async response => { resolve(response); }) .catch(error => { reject(error); }); });};