Created
September 7, 2023 08:38
-
-
Save Yasir5247/0cc8e988c5d09ae50afa2c109639ef5a to your computer and use it in GitHub Desktop.
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
import {AuthorizeResult, refresh, RefreshResult} from 'react-native-app-auth'; | |
import {EFAAS_CONFIG} from './efaasConfig'; | |
import {getSecureValue, saveSecureValue} from './getToken'; | |
import {captureException} from './sentry'; | |
import {stores} from '../stores'; | |
import {STORAGE_KEYS} from './constants'; | |
export const TOKEN_TO_STORAGE_KEYS = { | |
access: STORAGE_KEYS.ACCESS_TOKEN, | |
refresh: STORAGE_KEYS.REFRESH_TOKEN, | |
} as const; | |
//after login save accessToken refreshToken and expiresAt | |
export const saveToken = async (response: AuthorizeResult) => { | |
console.log('accessToken', response.accessToken); | |
console.log('refreshToken', response.refreshToken); | |
// console.log('expiresAt', response.accessTokenExpirationDate); // "2023-08-10T09:50:25Z" | |
//convert expiration date to milliseconds | |
const expiresAt = Date.parse(response.accessTokenExpirationDate); | |
await saveSecureValue('token', response.accessToken); | |
await saveSecureValue('refreshToken', response.refreshToken); | |
await saveSecureValue('expiresAt', String(expiresAt)); | |
}; | |
let refreshAccessTokenPromise: Promise<RefreshResult | null> | null = null; | |
export const refreshAccessToken = async (): Promise<RefreshResult | null> => { | |
// Check if there's an existing refresh token promise | |
if (refreshAccessTokenPromise) { | |
return refreshAccessTokenPromise; | |
} | |
try { | |
// Attempt to get the refresh token | |
const refreshToken = await getSecureValue('refreshToken'); | |
if (!refreshToken) { | |
throw new Error('No refresh token present'); | |
} | |
// Refresh the access token | |
const result: RefreshResult = await refresh(EFAAS_CONFIG, { | |
refreshToken: refreshToken, | |
}); | |
return result; | |
} catch (error: any) { | |
console.error('refreshError', error); | |
console.error('error.message', error.message); | |
captureException(error); | |
if (error.message === 'invalid_grant') { | |
console.log('hahaha'); | |
stores.settingState.setRefreshTokenExpired(true); | |
} | |
return null; // Handle other errors gracefully | |
} finally { | |
// Reset the cached promise after retry attempts or errors | |
refreshAccessTokenPromise = null; | |
} | |
}; | |
export const getAccessToken = async (): Promise<string | null | undefined> => { | |
console.log('Im here at getAccessToken'); | |
const accessToken = await getSecureValue('token'); | |
const expiresAtString = await getSecureValue('expiresAt'); | |
// console.log({accessToken, expiresAtString}); | |
if (!accessToken || !expiresAtString) { | |
return null; //handle missing data gracefully | |
} | |
//convert expiresAt to milliseconds | |
// eslint-disable-next-line radix | |
const expiresAt = parseInt(expiresAtString); | |
if (Date.now() < expiresAt) { | |
return accessToken; | |
} | |
const data = await refreshAccessToken(); | |
saveToken(data as AuthorizeResult); | |
return data?.accessToken || null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment