From da125ab77e5da5dbc5f8a53dabaeaa1a7fc08d08 Mon Sep 17 00:00:00 2001 From: Amedeo Lepore Date: Thu, 18 Jan 2024 18:02:08 +0100 Subject: [PATCH] [AAE-12521] allow to set the preventClearHashAfterLogin value by forRoot method to choose if clear hash fragment after the lib read the token --- lib/core/src/lib/auth/oidc/auth-config.ts | 1 + lib/core/src/lib/auth/oidc/auth.module.ts | 1 + lib/core/src/lib/auth/oidc/redirect-auth.service.ts | 7 +++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/core/src/lib/auth/oidc/auth-config.ts b/lib/core/src/lib/auth/oidc/auth-config.ts index adfc5bf4c1..5adbb11f9a 100644 --- a/lib/core/src/lib/auth/oidc/auth-config.ts +++ b/lib/core/src/lib/auth/oidc/auth-config.ts @@ -19,6 +19,7 @@ import { InjectionToken } from '@angular/core'; export interface AuthModuleConfig { readonly useHash: boolean; + preventClearHashAfterLogin?: boolean; } export const AUTH_MODULE_CONFIG = new InjectionToken('AUTH_MODULE_CONFIG'); diff --git a/lib/core/src/lib/auth/oidc/auth.module.ts b/lib/core/src/lib/auth/oidc/auth.module.ts index f3450b0e40..062e22013b 100644 --- a/lib/core/src/lib/auth/oidc/auth.module.ts +++ b/lib/core/src/lib/auth/oidc/auth.module.ts @@ -65,6 +65,7 @@ export function loginFactory(oAuthService: OAuthService, storage: OAuthStorage, }) export class AuthModule { static forRoot(config: AuthModuleConfig = { useHash: false }): ModuleWithProviders { + config.preventClearHashAfterLogin = config.preventClearHashAfterLogin ?? true; return { ngModule: AuthModule, providers: [{ provide: AUTH_MODULE_CONFIG, useValue: config }] diff --git a/lib/core/src/lib/auth/oidc/redirect-auth.service.ts b/lib/core/src/lib/auth/oidc/redirect-auth.service.ts index cc078b6152..61d5975ec3 100644 --- a/lib/core/src/lib/auth/oidc/redirect-auth.service.ts +++ b/lib/core/src/lib/auth/oidc/redirect-auth.service.ts @@ -15,18 +15,21 @@ * limitations under the License. */ -import { Inject, Injectable } from '@angular/core'; +import { Inject, Injectable, inject } from '@angular/core'; import { AuthConfig, AUTH_CONFIG, OAuthErrorEvent, OAuthService, OAuthStorage, TokenResponse, LoginOptions } from 'angular-oauth2-oidc'; import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks'; import { from, Observable } from 'rxjs'; import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators'; import { AuthService } from './auth.service'; +import { AUTH_MODULE_CONFIG, AuthModuleConfig } from './auth-config'; const isPromise = (value: T | Promise): value is Promise => value && typeof (value as Promise).then === 'function'; @Injectable() export class RedirectAuthService extends AuthService { + readonly authModuleConfig: AuthModuleConfig = inject(AUTH_MODULE_CONFIG); + onLogin: Observable; private _loadDiscoveryDocumentPromise = Promise.resolve(false); @@ -129,7 +132,7 @@ export class RedirectAuthService extends AuthService { async loginCallback(loginOptions?: LoginOptions): Promise { return this.ensureDiscoveryDocument() - .then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: true })) + .then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: this.authModuleConfig.preventClearHashAfterLogin })) .then(() => this._getRedirectUrl()); }