Last active
June 1, 2018 09:38
-
-
Save PrimeTimeTran/3de2907bb0b1573ad3b06fda147fe293 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import { AsyncStorage } from 'react-native'; | |
# Works | |
const client = () => { | |
let token = `Token eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE1NTkyNjY4MDl9.KlSSy_o-C8eACIgCrDPDCfUp5O6rqNuc1B5v-z7RoSpZmWc9Lo1BbWueZCVpHvkxOIjdZuTXxdR0mvk-CzwtvA'`; | |
console.log('Token is:', token); | |
const defaultOptions = { | |
headers: { | |
Authorization: token ? `Token ${token}` : '', | |
}, | |
}; | |
return { | |
get: (url, options = {}) => axios.get(url, { ...defaultOptions, ...options }), | |
post: (url, data, options = {}) => axios.post(url, data, { ...defaultOptions, ...options }), | |
put: (url, data, options = {}) => axios.put(url, data, { ...defaultOptions, ...options }), | |
delete: (url, options = {}) => axios.delete(url, { ...defaultOptions, ...options }), | |
}; | |
}; | |
# Does not work | |
const client = async () => { | |
let token = await AsyncStorage.getItem('auth_token'); | |
console.log('Token is:', token); | |
const defaultOptions = { | |
headers: { | |
Authorization: token ? `Token ${token}` : '', | |
}, | |
}; | |
return { | |
get: (url, options = {}) => axios.get(url, { ...defaultOptions, ...options }), | |
post: (url, data, options = {}) => axios.post(url, data, { ...defaultOptions, ...options }), | |
put: (url, data, options = {}) => axios.put(url, data, { ...defaultOptions, ...options }), | |
delete: (url, options = {}) => axios.delete(url, { ...defaultOptions, ...options }), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment