export interface TokenInfo { access_token : string ; expires_in ? : number ; refresh_expires_in ? : number ; refresh_token ? : string ; token_type ? : string ; } export interface JwtToken { sub : string ; name : string ; preferred_username : string ; } const TOKEN_KEY = 'auth_token' ; const REFRESH_TOKEN_KEY = 'refresh_token' ; export function getAuthToken ( ) { return localStorage . getItem ( TOKEN_KEY ) ; } export function setAuthToken ( token ) { if ( token == null ) { localStorage . removeItem ( TOKEN_KEY ) ; } else { localStorage . setItem ( TOKEN_KEY , token ) ; } } export function parseTokenInfoFromQuery ( search ) { let tokenStr ; const query = search . substr ( 1 ) ; query . split ( '&' ) . forEach ( ( part ) => { const item = part . split ( '=' ) ; if ( item [ 0 ] === 'token_json' ) { tokenStr = decodeURIComponent ( item [ 1 ] ) ; } } ) ; if ( tokenStr ) { try { const tokenInfo = JSON . parse ( tokenStr ) as TokenInfo ; processTokenInfo ( tokenInfo ) ; return tokenInfo ; } catch ( e ) { } } return undefined ; } function processTokenInfo ( token ) { localStorage . setItem ( TOKEN_KEY , token . access_token ) ; localStorage . setItem ( REFRESH_TOKEN_KEY , token . refresh_token ) ; } export function parseJwtToken ( token ) { return JSON . parse ( atob ( token . split ( '.' ) [ 1 ] ) ) ; }