-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix: app login logic/#43
- Loading branch information
Showing
17 changed files
with
318 additions
and
234 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
import axios from 'axios'; | ||
import { getToken } from '@components/login/AuthService'; | ||
|
||
const instance = axios.create({ | ||
baseURL: process.env.BASE_URL, | ||
headers: { 'X-Custom-Header': 'foobar', 'Content-Type': 'application/json' }, | ||
timeout: 1000, | ||
timeout: 10000, | ||
}); | ||
|
||
instance.interceptors.request.use( | ||
async (config) => { | ||
console.log('Starting Request', JSON.stringify(config, null, 2)); | ||
if (config.url !== '/auth/google') { | ||
const accessToken = await getToken(); | ||
if (accessToken) { | ||
config.headers.Authorization = accessToken; | ||
config.withCredentials = true; | ||
} | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
instance.interceptors.response.use( | ||
(response) => { | ||
console.log('Response:', JSON.stringify(response, null, 2)); | ||
return response; | ||
}, | ||
(error) => { | ||
console.log('Error Response:', JSON.stringify(error, null, 2)); | ||
if (error.message === 'Network Error' && error.config) { | ||
error.config.__isRetryRequest = true; | ||
return axios(error.config); | ||
} | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
export default instance; |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,29 @@ | ||
import { Platform } from 'react-native'; | ||
import EncryptedStorage from 'react-native-encrypted-storage'; | ||
|
||
const TOKEN_KEY = 'authToken'; | ||
|
||
const saveToken = async (token) => { | ||
if (Platform.OS === 'web') { | ||
try { | ||
localStorage.setItem(TOKEN_KEY, token); | ||
} catch (e) { | ||
console.error('Failed to save token in web storage', e); | ||
} | ||
} else { | ||
try { | ||
const EncryptedStorage = (await import('react-native-encrypted-storage')).default; | ||
await EncryptedStorage.setItem(TOKEN_KEY, token); | ||
} catch (e) { | ||
console.error('Failed to save token in encrypted storage', e); | ||
} | ||
export const saveToken = async (token: string) => { | ||
try { | ||
await EncryptedStorage.setItem(TOKEN_KEY, token); | ||
} catch (e) { | ||
console.error('Failed to save token in encrypted storage', e); | ||
} | ||
}; | ||
|
||
const getToken = async () => { | ||
if (Platform.OS === 'web') { | ||
try { | ||
const token = localStorage.getItem(TOKEN_KEY); | ||
return token; | ||
} catch (e) { | ||
console.error('Failed to load token from web storage', e); | ||
return null; | ||
} | ||
} else { | ||
try { | ||
const EncryptedStorage = (await import('react-native-encrypted-storage')).default; | ||
const token = await EncryptedStorage.getItem(TOKEN_KEY); | ||
return token; | ||
} catch (e) { | ||
console.error('Failed to load token from encrypted storage', e); | ||
return null; | ||
} | ||
export const getToken = async () => { | ||
try { | ||
const token = await EncryptedStorage.getItem(TOKEN_KEY); | ||
return token; | ||
} catch (e) { | ||
console.error('Failed to load token from encrypted storage', e); | ||
return null; | ||
} | ||
}; | ||
|
||
const removeToken = async () => { | ||
if (Platform.OS === 'web') { | ||
try { | ||
localStorage.removeItem(TOKEN_KEY); | ||
} catch (e) { | ||
console.error('Failed to remove token from web storage', e); | ||
} | ||
} else { | ||
try { | ||
const EncryptedStorage = (await import('react-native-encrypted-storage')).default; | ||
await EncryptedStorage.removeItem(TOKEN_KEY); | ||
} catch (e) { | ||
console.error('Failed to remove token from encrypted storage', e); | ||
} | ||
export const removeToken = async () => { | ||
try { | ||
await EncryptedStorage.removeItem(TOKEN_KEY); | ||
} catch (e) { | ||
console.error('Failed to remove token from encrypted storage', e); | ||
} | ||
}; | ||
|
||
export { saveToken, getToken, removeToken }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const TOKEN_KEY = 'authToken'; | ||
|
||
export const saveToken = async (token: string) => { | ||
try { | ||
localStorage.setItem(TOKEN_KEY, token); | ||
} catch (e) { | ||
console.error('Failed to save token in web storage', e); | ||
} | ||
}; | ||
|
||
export const getToken = async () => { | ||
try { | ||
const token = localStorage.getItem(TOKEN_KEY); | ||
return token; | ||
} catch (e) { | ||
console.error('Failed to load token from web storage', e); | ||
return null; | ||
} | ||
}; | ||
|
||
export const removeToken = async () => { | ||
try { | ||
localStorage.removeItem(TOKEN_KEY); | ||
} catch (e) { | ||
console.error('Failed to remove token from web storage', e); | ||
} | ||
}; |
This file contains 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
Oops, something went wrong.