import { Injector } from '@angular/core'; import { Location} from '@angular/common'; import { Router,UrlSerializer } from '@angular/router'; import { AuthConfig, OAuthService, JwksValidationHandler, OAuthErrorEvent } from 'angular-oauth2-oidc'; import { AppConfig } from "./app.config"; import { IAuthconfigFactory } from './authconfigFactory'; export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory): () => Promise { return (): Promise => { return appConfig.load().then(() => { oauthService.events.subscribe((event) => { console.debug(event.type); if (event.type == 'token_error' || event.type == 'silent_refresh_timeout') { let e = event as OAuthErrorEvent; let p = e.params as any; if (event.type == 'silent_refresh_timeout' || (p.error && p.error == 'login_required')) { let router = injector.get(Router); console.debug("Session expired"); router.navigate(['loggedout'], { queryParams: { redirectTo: router.url } }); } } }); oauthService.configure(authconfigFactory.getAuthConfig(appConfig)); oauthService.tokenValidationHandler = new JwksValidationHandler(); oauthService.tokenValidationHandler.validateAtHash = function () { return new Promise((res) => { res(true); }) }; oauthService.setupAutomaticSilentRefresh(); let location = injector.get(Location); var urlPath =location.path(); oauthService.loadDiscoveryDocument().then(() => { oauthService.tryLogin({ onTokenReceived: (info) => { urlPath = info.state; } }).then(() => { let router = injector.get(Router); if (!oauthService.hasValidAccessToken()) { oauthService.initImplicitFlow(urlPath); } else { router.navigateByUrl(urlPath); } }); }) }); } }