Browse Source

token storage worker added

pull/24050/head
erdemcaygor 12 months ago
parent
commit
e087db8d4b
  1. 19
      npm/ng-packs/packages/oauth/src/lib/services/memory-token-storage.service.ts

19
npm/ng-packs/packages/oauth/src/lib/services/memory-token-storage.service.ts

@ -1,5 +1,4 @@
// shared-worker-token-storage.service.ts import { DOCUMENT, inject, Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { OAuthStorage } from 'angular-oauth2-oidc'; import { OAuthStorage } from 'angular-oauth2-oidc';
import { AbpLocalStorageService } from '@abp/ng.core'; import { AbpLocalStorageService } from '@abp/ng.core';
@ -18,6 +17,7 @@ export class MemoryTokenStorageService implements OAuthStorage {
private port?: MessagePort; private port?: MessagePort;
private cache = new Map<string, string>(); private cache = new Map<string, string>();
private localStorageService = inject(AbpLocalStorageService); private localStorageService = inject(AbpLocalStorageService);
private _document = inject(DOCUMENT);
private useSharedWorker = false; private useSharedWorker = false;
constructor() { constructor() {
@ -43,13 +43,16 @@ export class MemoryTokenStorageService implements OAuthStorage {
switch (action) { switch (action) {
case 'set': case 'set':
this.checkAuthStateChanges(key);
this.cache.set(key, value); this.cache.set(key, value);
break; break;
case 'remove': case 'remove':
this.cache.delete(key); this.cache.delete(key);
this.refreshDocument();
break; break;
case 'clear': case 'clear':
this.cache.clear(); this.cache.clear();
this.refreshDocument();
break; break;
} }
}; };
@ -111,4 +114,16 @@ export class MemoryTokenStorageService implements OAuthStorage {
} }
this.cache.clear(); this.cache.clear();
} }
private checkAuthStateChanges(key: string): void {
console.log("current access token:", this.cache.get('access_token'));
if (key !== 'access_token' && !this.cache.get('access_token')) {
this.refreshDocument();
}
}
private refreshDocument(): void {
this._document.defaultView?.location.reload();
}
} }

Loading…
Cancel
Save