Browse Source

feat: create OAuthConfigurationHandler

pull/4956/head
mehmet-erim 6 years ago
parent
commit
931442d1af
  1. 1
      npm/ng-packs/packages/core/src/lib/handlers/index.ts
  2. 35
      npm/ng-packs/packages/core/src/lib/handlers/oauth-configuration.handler.ts
  3. 11
      npm/ng-packs/packages/core/src/lib/states/config.state.ts

1
npm/ng-packs/packages/core/src/lib/handlers/index.ts

@ -1 +1,2 @@
export * from './oauth-configuration.handler';
export * from './routes.handler';

35
npm/ng-packs/packages/core/src/lib/handlers/oauth-configuration.handler.ts

@ -0,0 +1,35 @@
import { Inject, Injectable } from '@angular/core';
import { Actions, ofActionSuccessful } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import compare from 'just-compare';
import { filter, map } from 'rxjs/operators';
import { SetEnvironment } from '../actions/config.actions';
import { ABP } from '../models/common';
import { CORE_OPTIONS } from '../tokens/options.token';
@Injectable({
providedIn: 'root',
})
export class OAuthConfigurationHandler {
constructor(
private actions: Actions,
private oAuthService: OAuthService,
@Inject(CORE_OPTIONS) private options: ABP.Root,
) {
this.oAuthService.configure(this.options.environment.oAuthConfig);
this.listenToSetEnvironment();
}
private listenToSetEnvironment() {
this.actions
.pipe(ofActionSuccessful(SetEnvironment))
.pipe(
map(({ environment }: SetEnvironment) => environment.oAuthConfig),
filter(config => !compare(config, this.options.environment.oAuthConfig)),
)
.subscribe(config => {
this.oAuthService.configure(config);
});
}
}

11
npm/ng-packs/packages/core/src/lib/states/config.state.ts

@ -10,9 +10,6 @@ import { SetLanguage } from '../actions/session.actions';
import { ApplicationConfiguration } from '../models/application-configuration';
import { Config } from '../models/config';
import { SessionState } from './session.state';
import compare from 'just-compare';
import clone from 'just-clone';
import { OAuthService } from 'angular-oauth2-oidc';
@State<Config.State>({
name: 'ConfigState',
@ -32,7 +29,7 @@ export class ConfigState {
static getOne(key: string) {
const selector = createSelector([ConfigState], (state: Config.State) => {
return clone(state[key]);
return state[key];
});
return selector;
@ -197,7 +194,7 @@ export class ConfigState {
return selector;
}
constructor(private http: HttpClient, private store: Store, private oAuthService: OAuthService) {}
constructor(private http: HttpClient, private store: Store) {}
@Action(GetAppConfiguration)
addData({ patchState, dispatch }: StateContext<Config.State>) {
@ -241,10 +238,6 @@ export class ConfigState {
) {
const { oAuthConfig } = getState().environment;
if (!compare(oAuthConfig, environment.oAuthConfig)) {
this.oAuthService.configure(environment.oAuthConfig);
}
return patchState({
environment,
});

Loading…
Cancel
Save