Browse Source

add local storage listener service to logout from other tabs

pull/19489/head
Sinan997 3 years ago
parent
commit
8847babc29
  1. 8
      npm/ng-packs/packages/oauth/src/lib/oauth.module.ts
  2. 1
      npm/ng-packs/packages/oauth/src/lib/services/index.ts
  3. 19
      npm/ng-packs/packages/oauth/src/lib/services/local-storage-listener.service.ts

8
npm/ng-packs/packages/oauth/src/lib/oauth.module.ts

@ -13,7 +13,7 @@ import {
noop, noop,
PIPE_TO_LOGIN_FN_KEY, PIPE_TO_LOGIN_FN_KEY,
} from '@abp/ng.core'; } from '@abp/ng.core';
import { AbpOAuthService, OAuthErrorFilterService } from './services'; import { AbpOAuthService, LocalStorageListenerService, OAuthErrorFilterService } from './services';
import { OAuthConfigurationHandler } from './handlers/oauth-configuration.handler'; import { OAuthConfigurationHandler } from './handlers/oauth-configuration.handler';
import { OAuthApiInterceptor } from './interceptors/api.interceptor'; import { OAuthApiInterceptor } from './interceptors/api.interceptor';
import { AbpOAuthGuard, abpOAuthGuard } from './guards/oauth.guard'; import { AbpOAuthGuard, abpOAuthGuard } from './guards/oauth.guard';
@ -64,6 +64,12 @@ export class AbpOAuthModule {
deps: [OAuthConfigurationHandler], deps: [OAuthConfigurationHandler],
useFactory: noop, useFactory: noop,
}, },
{
provide: APP_INITIALIZER,
multi: true,
deps: [LocalStorageListenerService],
useFactory: noop,
},
OAuthModule.forRoot().providers as Provider[], OAuthModule.forRoot().providers as Provider[],
{ provide: OAuthStorage, useClass: AbpLocalStorageService }, { provide: OAuthStorage, useClass: AbpLocalStorageService },
{ provide: AuthErrorFilterService, useExisting: OAuthErrorFilterService }, { provide: AuthErrorFilterService, useExisting: OAuthErrorFilterService },

1
npm/ng-packs/packages/oauth/src/lib/services/index.ts

@ -1,2 +1,3 @@
export * from './oauth.service'; export * from './oauth.service';
export * from './oauth-error-filter.service'; export * from './oauth-error-filter.service';
export * from './local-storage-listener.service';

19
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();
}
});
}
}
Loading…
Cancel
Save