-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcore.module.ts
More file actions
47 lines (43 loc) · 1.65 KB
/
core.module.ts
File metadata and controls
47 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { ModuleWithProviders, NgModule, inject, provideAppInitializer } from '@angular/core';
import { AuthConfig, OAuthModule, OAuthModuleConfig, OAuthStorage } from 'angular-oauth2-oidc';
import { authAppInitializerFactory } from './auth-app-initializer.factory';
import { authConfig } from './auth-config';
import { AuthGuardWithForcedLogin } from './auth-guard-with-forced-login.service';
import { AuthGuard } from './auth-guard.service';
import { authModuleConfig } from './auth-module-config';
import { AuthService } from './auth.service';
// We need a factory since localStorage is not available at AOT build time
export function storageFactory(): OAuthStorage {
return localStorage;
}
@NgModule({
imports: [OAuthModule.forRoot()], providers: [
AuthService,
AuthGuard,
AuthGuardWithForcedLogin,
provideHttpClient(withInterceptorsFromDi()),
]
})
export class CoreModule {
static forRoot(): ModuleWithProviders<CoreModule> {
return {
ngModule: CoreModule,
providers: [
provideAppInitializer(() => {
const initializerFn = (authAppInitializerFactory)(inject(AuthService));
return initializerFn();
}),
{ provide: AuthConfig, useValue: authConfig },
{ provide: OAuthModuleConfig, useValue: authModuleConfig },
{ provide: OAuthStorage, useFactory: storageFactory },
]
};
}
constructor() {
const parentModule = inject(CoreModule, { optional: true, skipSelf: true });
if (parentModule) {
throw new Error('CoreModule is already loaded. Import it in the AppModule only');
}
}
}