Skip to content

Instantly share code, notes, and snippets.

@peimelo
Last active December 12, 2023 14:05
Show Gist options
  • Save peimelo/9f18128752ad281c050a8c6ec2c0c228 to your computer and use it in GitHub Desktop.
Save peimelo/9f18128752ad281c050a8c6ec2c0c228 to your computer and use it in GitHub Desktop.
Axios for devise_token_auth
import axios from 'axios';
import AsyncStorage from '@react-native-community/async-storage';
const api = axios.create({
baseURL: 'http://localhost:3000/api',
});
api.interceptors.request.use(async function (config) {
const accessToken = await AsyncStorage.getItem('@CHealth:access-token');
const client = await AsyncStorage.getItem('@CHealth:client');
const uid = await AsyncStorage.getItem('@CHealth:uid');
if (accessToken && client && uid) {
config.headers['access-token'] = accessToken;
config.headers['client'] = client;
config.headers['uid'] = uid;
}
return config;
});
api.interceptors.response.use(
async function (successRes) {
if (
successRes.headers['access-token'] &&
successRes.headers['client'] &&
successRes.headers['uid']
) {
await AsyncStorage.setItem('@CHealth:access-token', successRes.headers['access-token']);
await AsyncStorage.setItem('@CHealth:client', successRes.headers['client']);
await AsyncStorage.setItem('@CHealth:uid', successRes.headers['uid']);
}
return successRes;
},
function (error) {
return Promise.reject(error);
},
);
export default api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment