In my React Native App, I want to create an Axios instance to send headers to the backend with a token taken from AsyncStorage
. However, the following Token()
always returns an object( Promise ) instead of actual token so that 'Authorization' header is something like [Object object]
.
import axios from 'react-native-axios'
import AsyncStorage from "@react-native-community/async-storage"
const Token = async () => {
try {
await AsyncStorage.getItem("token").then(
token => token
);
} catch (error) {
return null;
}
}
export default axios.create({
baseURL: 'http://192.168.1.100:8080/api',
headers: {
'Authorization': 'Bearer '+ Token(),
'Content-Type': 'application/json'
}
})
How can I take the actual token and use it in axios instance?