import axios from 'axios'; import { AsyncStorage } from 'react-native'; import jwtDecode from 'jwt-decode'; const setAuthToken = async () => { const authorizationToken = await AsyncStorage.getItem('@token'); axios.defaults.headers.common['Authorization'] = authorizationToken; }; export default const authenticateUser = async (email: String, password: String) => { try { const { data: { token } } = await axios.post('/authenticate', { email, password }); const { uuid } = await jwtDecode(token); await AsyncStorage.multiSet([['@token', `Bearer ${token}`], ['@uuid', uuid]]); await setAuthToken(); } catch (e) { throw e; } }; export const clearAuthorization = async () => { const keys = ['@uuid', '@token']; try { await AsyncStorage.multiRemove(keys); axios.defaults.headers.common.Authorization = null; } catch (e) { throw e; } };