import { Auth0Client } from 'context/auth0-context' ; export function ensureClient ( client ) { if ( ! client ) { throw new Error ( 'Auth0Client was not initialized' ) ; } return client ; } export const DEFAULT_SCOPE = 'openid profile email' ; const dedupe = ( arr ) : any => arr . filter ( ( x , i ) => arr . indexOf ( x ) === i ) ; export function getUniqueScopes ( ... scopes ) { const scopeString = scopes . filter ( Boolean ) . join ( ) ; return dedupe ( scopeString . replace ( /\s/g , ',' ) . split ( ',' ) ) . join ( ' ' ) . trim ( ) ; } export function getAccessTokenFromCache ( client , audience , scope ) { const cacheContainer = ensureClient ( client ) ; const { cache } = cacheContainer ; const token = cache . get ( { scope : getUniqueScopes ( DEFAULT_SCOPE , scope ) , audience : audience || 'default' } ) ; return token && token . access_token ; }