waiting for initialization
This commit is contained in:
@@ -5,11 +5,14 @@ import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@a
|
||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
import { environment } from '../environments/environment';
|
||||
import { customKeycloakAdapter } from '../keycloak';
|
||||
import { routes } from './app.routes';
|
||||
import { LoadingInterceptor } from './interceptors/loading.interceptor';
|
||||
import { KeycloakInitializerService } from './services/keycloak-initializer.service';
|
||||
import { SelectOptionsService } from './services/select-options.service';
|
||||
import { createLogger } from './utils/utils';
|
||||
// provideClientHydration()
|
||||
const logger = createLogger('ApplicationConfig');
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
@@ -17,9 +20,10 @@ export const appConfig: ApplicationConfig = {
|
||||
{
|
||||
provide: APP_INITIALIZER,
|
||||
// useFactory: initializeKeycloak,
|
||||
useFactory: (keycloakInitializer: KeycloakInitializerService) => async () => await keycloakInitializer.initialize(),
|
||||
//useFactory: initializeKeycloak,
|
||||
useFactory: initializeKeycloak3,
|
||||
multi: true,
|
||||
// deps: [KeycloakService],
|
||||
//deps: [KeycloakService],
|
||||
deps: [KeycloakInitializerService],
|
||||
},
|
||||
{
|
||||
@@ -49,9 +53,32 @@ function initServices(selectOptions: SelectOptionsService) {
|
||||
await selectOptions.init();
|
||||
};
|
||||
}
|
||||
export function initializeKeycloak3(keycloak: KeycloakInitializerService) {
|
||||
return () => keycloak.initialize();
|
||||
}
|
||||
|
||||
export function initializeKeycloak2(keycloak: KeycloakService): () => Promise<void> {
|
||||
return async () => {
|
||||
const { url, realm, clientId } = environment.keycloak;
|
||||
const adapter = customKeycloakAdapter(() => keycloak.getKeycloakInstance(), {});
|
||||
if (window.location.search.length > 0) {
|
||||
sessionStorage.setItem('SEARCH', window.location.search);
|
||||
}
|
||||
const { host, hostname, href, origin, pathname, port, protocol, search } = window.location;
|
||||
await keycloak.init({
|
||||
config: { url, realm, clientId },
|
||||
initOptions: {
|
||||
onLoad: 'check-sso',
|
||||
silentCheckSsoRedirectUri: window.location.hostname === 'localhost' ? `${window.location.origin}/assets/silent-check-sso.html` : `${window.location.origin}/dealerweb/assets/silent-check-sso.html`,
|
||||
adapter,
|
||||
redirectUri: `${origin}${pathname}`,
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
function initializeKeycloak(keycloak: KeycloakService) {
|
||||
return async () => {
|
||||
logger.info(`###>calling keycloakService init ...`);
|
||||
const authenticated = await keycloak.init({
|
||||
config: {
|
||||
url: environment.keycloak.url,
|
||||
@@ -63,6 +90,6 @@ function initializeKeycloak(keycloak: KeycloakService) {
|
||||
silentCheckSsoRedirectUri: (<any>window).location.origin + '/assets/silent-check-sso.html',
|
||||
},
|
||||
});
|
||||
console.log(`--->${authenticated}`);
|
||||
logger.info(`+++>${authenticated}`);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router, UrlTree } from '@angular/router';
|
||||
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
|
||||
import { KeycloakAuthGuard, KeycloakService } from 'keycloak-angular';
|
||||
import { KeycloakInitializerService } from '../services/keycloak-initializer.service';
|
||||
|
||||
import { createLogger } from '../utils/utils';
|
||||
const logger = createLogger('AuthGuard');
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
@@ -11,14 +12,30 @@ export class AuthGuard extends KeycloakAuthGuard {
|
||||
super(router, keycloak);
|
||||
}
|
||||
|
||||
async isAccessAllowed(): Promise<boolean | UrlTree> {
|
||||
if (!this.keycloakInitializer.isInitialized()) {
|
||||
await this.keycloakInitializer.initialize();
|
||||
async isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean | UrlTree> {
|
||||
logger.info(`--->AuthGuard`);
|
||||
while (!this.keycloakInitializer.initialized) {
|
||||
logger.info(`Waiting 100 msec`);
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
// Force the user to log in if currently unauthenticated.
|
||||
const authenticated = this.keycloak.isLoggedIn();
|
||||
if (!authenticated) {
|
||||
await this.router.navigate(['/home']);
|
||||
if (!this.authenticated && !authenticated) {
|
||||
await this.keycloak.login({
|
||||
redirectUri: window.location.origin + state.url,
|
||||
});
|
||||
// return false;
|
||||
}
|
||||
return authenticated;
|
||||
|
||||
// Get the roles required from the route.
|
||||
const requiredRoles = route.data['roles'];
|
||||
|
||||
// Allow the user to proceed if no additional roles are required to access the route.
|
||||
if (!Array.isArray(requiredRoles) || requiredRoles.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Allow the user to proceed if all the required roles are present.
|
||||
return requiredRoles.every(role => this.roles.includes(role));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,39 +5,60 @@ import { createLogger } from '../utils/utils';
|
||||
const logger = createLogger('KeycloakInitializerService');
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class KeycloakInitializerService {
|
||||
private initialized = false;
|
||||
public initialized = false;
|
||||
|
||||
constructor(private keycloakService: KeycloakService) {}
|
||||
|
||||
async initialize(): Promise<void> {
|
||||
if (this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
const authenticated = await this.keycloakService.init({
|
||||
config: {
|
||||
url: environment.keycloak.url,
|
||||
realm: environment.keycloak.realm,
|
||||
clientId: environment.keycloak.clientId,
|
||||
},
|
||||
initOptions: {
|
||||
onLoad: 'check-sso',
|
||||
silentCheckSsoRedirectUri: (<any>window).location.origin + '/assets/silent-check-sso.html',
|
||||
flow: 'implicit',
|
||||
},
|
||||
// initOptions: {
|
||||
// pkceMethod: 'S256',
|
||||
// redirectUri: environment.keycloak.redirectUri,
|
||||
// checkLoginIframe: false,
|
||||
// },
|
||||
async initialize(): Promise<boolean> {
|
||||
return new Promise<boolean>(async (resolve, reject) => {
|
||||
try {
|
||||
await this.keycloakService.init({
|
||||
config: {
|
||||
url: environment.keycloak.url,
|
||||
realm: environment.keycloak.realm,
|
||||
clientId: environment.keycloak.clientId,
|
||||
},
|
||||
initOptions: {
|
||||
onLoad: 'check-sso',
|
||||
silentCheckSsoRedirectUri: (<any>window).location.origin + '/assets/silent-check-sso.html',
|
||||
// flow: 'implicit',
|
||||
},
|
||||
});
|
||||
this.initialized = true;
|
||||
resolve(true);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
const token = await this.keycloakService.getToken();
|
||||
logger.info(`--->${authenticated}:${token}`);
|
||||
// if (this.initialized) {
|
||||
// return;
|
||||
// }
|
||||
// logger.info(`###>calling keycloakService init ...`);
|
||||
// const authenticated = await this.keycloakService.init({
|
||||
// config: {
|
||||
// url: environment.keycloak.url,
|
||||
// realm: environment.keycloak.realm,
|
||||
// clientId: environment.keycloak.clientId,
|
||||
// },
|
||||
// initOptions: {
|
||||
// onLoad: 'check-sso',
|
||||
// silentCheckSsoRedirectUri: (<any>window).location.origin + '/assets/silent-check-sso.html',
|
||||
// // flow: 'implicit',
|
||||
// },
|
||||
// // initOptions: {
|
||||
// // pkceMethod: 'S256',
|
||||
// // redirectUri: environment.keycloak.redirectUri,
|
||||
// // checkLoginIframe: false,
|
||||
// // },
|
||||
// });
|
||||
// logger.info(`+++>authenticated: ${authenticated}`);
|
||||
// const token = await this.keycloakService.getToken();
|
||||
// logger.info(`--->${token}`);
|
||||
|
||||
this.initialized = true;
|
||||
// this.initialized = true;
|
||||
}
|
||||
|
||||
isInitialized(): boolean {
|
||||
return this.initialized;
|
||||
}
|
||||
// isInitialized(): boolean {
|
||||
// return this.initialized;
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user