Skip to content

Commit e276b4d

Browse files
authored
Merge pull request abpframework#5060 from abpframework/fix/4839
Added the ngxsStoragePluginOptions property to forRoot method options of CoreModule
2 parents a8bab41 + dbd1944 commit e276b4d

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

npm/ng-packs/packages/core/src/lib/core.module.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { APP_INITIALIZER, Injector, ModuleWithProviders, NgModule } from '@angul
44
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
55
import { RouterModule } from '@angular/router';
66
import { NgxsRouterPluginModule } from '@ngxs/router-plugin';
7-
import { NgxsStoragePluginModule } from '@ngxs/storage-plugin';
7+
import {
8+
NgxsStoragePluginModule,
9+
NGXS_STORAGE_PLUGIN_OPTIONS,
10+
StorageOption,
11+
} from '@ngxs/storage-plugin';
812
import { NgxsModule, NGXS_PLUGINS } from '@ngxs/store';
913
import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
1014
import { AbstractNgModelComponent } from './abstracts/ng-model.component';
@@ -118,7 +122,7 @@ export class BaseCoreModule {}
118122
LocalizationModule,
119123
NgxsModule.forFeature([ReplaceableComponentsState, ProfileState, SessionState, ConfigState]),
120124
NgxsRouterPluginModule.forRoot(),
121-
NgxsStoragePluginModule.forRoot({ key: ['SessionState'] }),
125+
NgxsStoragePluginModule.forRoot(),
122126
OAuthModule.forRoot(),
123127
],
124128
})
@@ -216,7 +220,23 @@ export class CoreModule {
216220
useFactory: noop,
217221
},
218222
{ provide: OAuthStorage, useFactory: storageFactory },
223+
{
224+
provide: NGXS_STORAGE_PLUGIN_OPTIONS,
225+
useValue: {
226+
storage: StorageOption.LocalStorage,
227+
serialize: JSON.stringify,
228+
deserialize: JSON.parse,
229+
beforeSerialize: ngxsStoragePluginSerialize,
230+
afterDeserialize: ngxsStoragePluginSerialize,
231+
...options.ngxsStoragePluginOptions,
232+
key: [...(options.ngxsStoragePluginOptions?.key || []), 'SessionState'],
233+
},
234+
},
219235
],
220236
};
221237
}
222238
}
239+
240+
export function ngxsStoragePluginSerialize(data) {
241+
return data;
242+
}

npm/ng-packs/packages/core/src/lib/models/common.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { Router } from '@angular/router';
33
import { Subject } from 'rxjs';
44
import { eLayoutType } from '../enums/common';
55
import { Config } from './config';
6+
import { NgxsStoragePluginOptions } from '@ngxs/storage-plugin';
67

78
export namespace ABP {
89
export interface Root {
910
environment: Partial<Config.Environment>;
1011
skipGetAppConfiguration?: boolean;
1112
sendNullsAsQueryParam?: boolean;
1213
cultureNameLocaleFileMap?: Dictionary<string>;
14+
ngxsStoragePluginOptions?: NgxsStoragePluginOptions & { key?: string[] };
1315
}
1416

1517
export interface Test {

npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { Injector } from '@angular/core';
2+
import { Router } from '@angular/router';
23
import { Store } from '@ngxs/store';
34
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
4-
import { ConfigState } from '../states/config.state';
5-
import { CORE_OPTIONS } from '../tokens/options.token';
6-
import { Router } from '@angular/router';
75
import { Observable, of } from 'rxjs';
8-
import { RestService } from '../services/rest.service';
9-
import { switchMap } from 'rxjs/operators';
6+
import { switchMap, tap } from 'rxjs/operators';
107
import { GetAppConfiguration } from '../actions/config.actions';
8+
import { RestOccurError } from '../actions/rest.actions';
9+
import { RestService } from '../services/rest.service';
10+
import { ConfigState } from '../states/config.state';
1111

1212
export abstract class AuthFlowStrategy {
1313
abstract readonly isInternalAuth: boolean;
1414

15+
protected store: Store;
1516
protected oAuthService: OAuthService;
1617
protected oAuthConfig: AuthConfig;
1718
abstract checkIfInternalAuth(): boolean;
1819
abstract login(): void;
1920
abstract logout(): Observable<any>;
2021
abstract destroy(): void;
2122

22-
private catchError = err => {
23-
// TODO: handle the error
24-
};
23+
private catchError = err => this.store.dispatch(new RestOccurError(err));
2524

2625
constructor(protected injector: Injector) {
26+
this.store = injector.get(Store);
2727
this.oAuthService = injector.get(OAuthService);
2828
this.oAuthConfig = injector.get(Store).selectSnapshot(ConfigState.getDeep('environment.oAuthConfig'));
2929
}
@@ -74,10 +74,9 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
7474
}
7575

7676
logout() {
77-
const store = this.injector.get(Store);
7877
const rest = this.injector.get(RestService);
7978

80-
const issuer = store.selectSnapshot(ConfigState.getDeep('environment.oAuthConfig.issuer'));
79+
const issuer = this.store.selectSnapshot(ConfigState.getDeep('environment.oAuthConfig.issuer'));
8180
return rest
8281
.request(
8382
{
@@ -88,10 +87,8 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
8887
issuer,
8988
)
9089
.pipe(
91-
switchMap(() => {
92-
this.oAuthService.logOut();
93-
return store.dispatch(new GetAppConfiguration());
94-
}),
90+
tap(() => this.oAuthService.logOut()),
91+
switchMap(() => this.store.dispatch(new GetAppConfiguration())),
9592
);
9693
}
9794

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { InjectionToken } from '@angular/core';
2-
import { ABP } from '../models/common';
32
import differentLocales from '../constants/different-locales';
3+
import { ABP } from '../models/common';
44

55
export const CORE_OPTIONS = new InjectionToken<ABP.Root>('CORE_OPTIONS');
66

7-
export function coreOptionsFactory({
8-
cultureNameLocaleFileMap: localeNameMap = {},
9-
...options
10-
}: ABP.Root) {
7+
export function coreOptionsFactory({ cultureNameLocaleFileMap = {}, ...options }: ABP.Root) {
118
return {
129
...options,
13-
cultureNameLocaleFileMap: { ...differentLocales, ...localeNameMap },
10+
cultureNameLocaleFileMap: { ...differentLocales, ...cultureNameLocaleFileMap },
1411
} as ABP.Root;
1512
}

0 commit comments

Comments
 (0)