Skip to content

Commit

Permalink
[AAE-12521] allow to set the preventClearHashAfterLogin value by forR…
Browse files Browse the repository at this point in the history
…oot method to choose if clear hash fragment after the lib read the token
  • Loading branch information
alep85 committed Jan 18, 2024
1 parent 503ec42 commit 7c8b9eb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/core/src/lib/auth/oidc/auth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { InjectionToken } from '@angular/core';

export interface AuthModuleConfig {
readonly useHash: boolean;
preventClearHashAfterLogin?: boolean;
}

export const AUTH_MODULE_CONFIG = new InjectionToken<AuthModuleConfig>('AUTH_MODULE_CONFIG');
1 change: 1 addition & 0 deletions lib/core/src/lib/auth/oidc/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function loginFactory(oAuthService: OAuthService, storage: OAuthStorage,
})
export class AuthModule {
static forRoot(config: AuthModuleConfig = { useHash: false }): ModuleWithProviders<AuthModule> {
config.preventClearHashAfterLogin = config.preventClearHashAfterLogin ?? true;
return {
ngModule: AuthModule,
providers: [{ provide: AUTH_MODULE_CONFIG, useValue: config }]
Expand Down
7 changes: 5 additions & 2 deletions lib/core/src/lib/auth/oidc/redirect-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T>(value: T | Promise<T>): value is Promise<T> => value && typeof (value as Promise<T>).then === 'function';

@Injectable()
export class RedirectAuthService extends AuthService {

readonly authModuleConfig: AuthModuleConfig = inject(AUTH_MODULE_CONFIG);

onLogin: Observable<any>;

private _loadDiscoveryDocumentPromise = Promise.resolve(false);
Expand Down Expand Up @@ -129,7 +132,7 @@ export class RedirectAuthService extends AuthService {

async loginCallback(loginOptions?: LoginOptions): Promise<string | undefined> {
return this.ensureDiscoveryDocument()
.then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: true }))
.then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: this.authModuleConfig.preventClearHashAfterLogin }))
.then(() => this._getRedirectUrl());
}

Expand Down

0 comments on commit 7c8b9eb

Please sign in to comment.