diff --git a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts index 2adac2a23a..6f5830e938 100644 --- a/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts +++ b/npm/ng-packs/packages/oauth/src/lib/oauth.module.ts @@ -13,7 +13,7 @@ import { noop, PIPE_TO_LOGIN_FN_KEY, } from '@abp/ng.core'; -import { AbpOAuthService, OAuthErrorFilterService } from './services'; +import { AbpOAuthService, LocalStorageListenerService, OAuthErrorFilterService } from './services'; import { OAuthConfigurationHandler } from './handlers/oauth-configuration.handler'; import { OAuthApiInterceptor } from './interceptors/api.interceptor'; import { AbpOAuthGuard, abpOAuthGuard } from './guards/oauth.guard'; @@ -64,6 +64,12 @@ export class AbpOAuthModule { deps: [OAuthConfigurationHandler], useFactory: noop, }, + { + provide: APP_INITIALIZER, + multi: true, + deps: [LocalStorageListenerService], + useFactory: noop, + }, OAuthModule.forRoot().providers as Provider[], { provide: OAuthStorage, useClass: AbpLocalStorageService }, { provide: AuthErrorFilterService, useExisting: OAuthErrorFilterService }, diff --git a/npm/ng-packs/packages/oauth/src/lib/services/index.ts b/npm/ng-packs/packages/oauth/src/lib/services/index.ts index 375177f3b5..204a156e98 100644 --- a/npm/ng-packs/packages/oauth/src/lib/services/index.ts +++ b/npm/ng-packs/packages/oauth/src/lib/services/index.ts @@ -1,2 +1,3 @@ export * from './oauth.service'; export * from './oauth-error-filter.service'; +export * from './local-storage-listener.service'; diff --git a/npm/ng-packs/packages/oauth/src/lib/services/local-storage-listener.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/local-storage-listener.service.ts new file mode 100644 index 0000000000..7c267c285b --- /dev/null +++ b/npm/ng-packs/packages/oauth/src/lib/services/local-storage-listener.service.ts @@ -0,0 +1,19 @@ +import { DOCUMENT } from '@angular/common'; +import { Injectable, inject } from '@angular/core'; +import { OAuthService } from 'angular-oauth2-oidc'; + +@Injectable({ + providedIn: 'root', +}) +export class LocalStorageListenerService { + protected readonly window = inject(DOCUMENT).defaultView; + protected readonly authService = inject(OAuthService); + + constructor() { + this.window.addEventListener('storage', event => { + if (event.key === 'access_token' && event.newValue === null) { + this.authService.logOut(); + } + }); + } +}