Added fallback for injection

This commit is contained in:
VitoAlbano 2024-10-15 11:59:57 +01:00
parent f835af23c3
commit e81342ff7f
2 changed files with 10 additions and 6 deletions

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import { APP_INITIALIZER, inject, ModuleWithProviders, NgModule } from '@angular/core';
import { APP_INITIALIZER, inject, ModuleWithProviders, NgModule, InjectionToken } from '@angular/core';
import { AUTH_CONFIG, OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
import { AuthenticationService } from '../services/authentication.service';
import { AuthModuleConfig, AUTH_MODULE_CONFIG } from './auth-config';
@ -24,9 +24,10 @@ import { AuthRoutingModule } from './auth-routing.module';
import { AuthService } from './auth.service';
import { RedirectAuthService } from './redirect-auth.service';
import { AuthenticationConfirmationComponent } from './view/authentication-confirmation/authentication-confirmation.component';
import { JWT_STORAGE_SERVICE } from '../services/jwt-helper.service';
import { StorageService } from '../../common/services/storage.service';
export const JWT_STORAGE_SERVICE = new InjectionToken<OAuthStorage>('JWT_STORAGE_SERVICE');
/**
* Create a Login Factory function
*

View File

@ -15,11 +15,10 @@
* limitations under the License.
*/
import { inject, Injectable, InjectionToken } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { OAuthStorage } from 'angular-oauth2-oidc';
import { StorageService } from '../../common/services/storage.service';
export const JWT_STORAGE_SERVICE = new InjectionToken<OAuthStorage>('JWT_STORAGE_SERVICE');
import { JWT_STORAGE_SERVICE } from '../oidc/auth.module';
@Injectable({
providedIn: 'root'
@ -36,7 +35,11 @@ export class JwtHelperService {
static USER_PREFERRED_USERNAME = 'preferred_username';
static HXP_AUTHORIZATION = 'hxp_authorization';
private storageService: OAuthStorage = inject(JWT_STORAGE_SERVICE) ?? inject(StorageService);
private storageService: OAuthStorage;
constructor (@Inject(JWT_STORAGE_SERVICE) jwtStorageService: OAuthStorage, private defaultStorageService: StorageService) {
this.storageService = jwtStorageService ?? defaultStorageService;
}
/**
* Decodes a JSON web token into a JS object.