Skip to content

Commit

Permalink
fix: synchronizer events not fired when localStorage is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ayZagen committed Oct 17, 2023
1 parent c5f09f4 commit 0cfae29
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class OIDCClient extends EventEmitter<EventTypes>{
throw new OIDCClientError( '"issuer" must be a valid uri.' )
}

this.synchronizer = new TabUtils( btoa( options.issuer ) )
this.synchronizer = new TabUtils( btoa( options.issuer ), this )

this.options = mergeObjects( {
secondsToRefreshAccessTokenBeforeExp: 60,
Expand Down
17 changes: 14 additions & 3 deletions src/utils/tab_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ https://github.com/jitbit/TabUtils
MIT license: https://github.com/jitbit/TabUtils/blob/master/LICENSE
*/

import type { EventEmitter } from '../helpers';

const currentTabId = `${ performance.now() }:${ Math.random() * 1000000000 | 0 }`;
const handlers: Record<string, any> = {};

export class TabUtils {
keyPrefix: string;

constructor( kid: string ) {
private events: EventEmitter<any>;

constructor( kid: string, fallbackEvents: EventEmitter<any> ) {
this.keyPrefix = kid;
this.events = fallbackEvents
}

//runs code only once in multiple tabs
Expand Down Expand Up @@ -46,7 +51,10 @@ export class TabUtils {
try { handlers[messageId]( eventData ); } //"try" in case handler not found
catch ( x ) { }

if ( !window.localStorage ) return; //no local storage. old browser
if ( !window.localStorage ){
this.events.emit( messageId, eventData )
return; //no local storage. old browser
}

const data = {
data: eventData,
Expand All @@ -62,7 +70,10 @@ export class TabUtils {

OnBroadcastMessage( messageId: string, fn: ( data: any ) => void ): void{
handlers[messageId] = fn;
if ( !window.localStorage ) return; //no local storage. old browser
if ( !window.localStorage ){
this.events.on( messageId, fn )
return; //no local storage. old browser
}

//first register a handler for "storage" event that we trigger above
window.addEventListener( 'storage', ( ev ) => {
Expand Down

0 comments on commit 0cfae29

Please sign in to comment.