[AAE-12521] allow to set the preventClearHashAfterLogin value by forRoot method to choose if clear hash fragment after the lib read the token

This commit is contained in:
Amedeo Lepore
2024-01-18 18:02:08 +01:00
parent 6a80413880
commit da125ab77e
3 changed files with 7 additions and 2 deletions

View File

@@ -19,6 +19,7 @@ import { InjectionToken } from '@angular/core';
export interface AuthModuleConfig { export interface AuthModuleConfig {
readonly useHash: boolean; readonly useHash: boolean;
preventClearHashAfterLogin?: boolean;
} }
export const AUTH_MODULE_CONFIG = new InjectionToken<AuthModuleConfig>('AUTH_MODULE_CONFIG'); export const AUTH_MODULE_CONFIG = new InjectionToken<AuthModuleConfig>('AUTH_MODULE_CONFIG');

View File

@@ -65,6 +65,7 @@ export function loginFactory(oAuthService: OAuthService, storage: OAuthStorage,
}) })
export class AuthModule { export class AuthModule {
static forRoot(config: AuthModuleConfig = { useHash: false }): ModuleWithProviders<AuthModule> { static forRoot(config: AuthModuleConfig = { useHash: false }): ModuleWithProviders<AuthModule> {
config.preventClearHashAfterLogin = config.preventClearHashAfterLogin ?? true;
return { return {
ngModule: AuthModule, ngModule: AuthModule,
providers: [{ provide: AUTH_MODULE_CONFIG, useValue: config }] providers: [{ provide: AUTH_MODULE_CONFIG, useValue: config }]

View File

@@ -15,18 +15,21 @@
* limitations under the License. * 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 { AuthConfig, AUTH_CONFIG, OAuthErrorEvent, OAuthService, OAuthStorage, TokenResponse, LoginOptions } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks'; import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
import { from, Observable } from 'rxjs'; import { from, Observable } from 'rxjs';
import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { AUTH_MODULE_CONFIG, AuthModuleConfig } from './auth-config';
const isPromise = <T>(value: T | Promise<T>): value is Promise<T> => value && typeof (value as Promise<T>).then === 'function'; const isPromise = <T>(value: T | Promise<T>): value is Promise<T> => value && typeof (value as Promise<T>).then === 'function';
@Injectable() @Injectable()
export class RedirectAuthService extends AuthService { export class RedirectAuthService extends AuthService {
readonly authModuleConfig: AuthModuleConfig = inject(AUTH_MODULE_CONFIG);
onLogin: Observable<any>; onLogin: Observable<any>;
private _loadDiscoveryDocumentPromise = Promise.resolve(false); private _loadDiscoveryDocumentPromise = Promise.resolve(false);
@@ -129,7 +132,7 @@ export class RedirectAuthService extends AuthService {
async loginCallback(loginOptions?: LoginOptions): Promise<string | undefined> { async loginCallback(loginOptions?: LoginOptions): Promise<string | undefined> {
return this.ensureDiscoveryDocument() return this.ensureDiscoveryDocument()
.then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: true })) .then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: this.authModuleConfig.preventClearHashAfterLogin }))
.then(() => this._getRedirectUrl()); .then(() => this._getRedirectUrl());
} }