import { BearerToken } from 'teleport/services/session' ; import { KeysEnum } from './types' ; const storage = { clear ( ) { window . localStorage . clear ( ) ; } , subscribe ( fn ) { window . addEventListener ( 'storage' , fn ) ; } , unsubscribe ( fn ) { window . removeEventListener ( 'storage' , fn ) ; } , setBearerToken ( token ) { window . localStorage . setItem ( KeysEnum . TOKEN , JSON . stringify ( token ) ) ; } , getBearerToken ( ) { const item = window . localStorage . getItem ( KeysEnum . TOKEN ) ; if ( item ) { return JSON . parse ( item ) ; } return null ; } , getAccessToken ( ) { const bearerToken = this . getBearerToken ( ) ; return bearerToken ? bearerToken . accessToken : null ; } , getSessionInactivityTimeout ( ) { const bearerToken = this . getBearerToken ( ) ; const time = Number ( bearerToken . sessionInactiveTimeout ) ; return time ? time : 0 ; } , setLastActive ( expiry ) { window . localStorage . setItem ( KeysEnum . LAST_ACTIVE , `${ expiry }` ) ; } , getLastActive ( ) { const time = Number ( window . localStorage . getItem ( KeysEnum . LAST_ACTIVE ) ) ; return time ? time : 0 ; } , broadcast ( messageType , messageBody ) { window . localStorage . setItem ( messageType , messageBody ) ; window . localStorage . removeItem ( messageType ) ; } , } ; export default storage ;