Im new here with Keycloak angular..
I am facing these error and not able to understand and debug.
ERROR undefined ERROR undefined
I am able to get my access token on keycloak here
Below is my configurations:
ng version
Angular CLI: 15.2.10
Node: 16.13.0
Package Manager: npm 8.1.0
OS: win32 x64
Angular: 15.2.10
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1502.11
@angular-devkit/build-angular 15.2.11
@angular-devkit/core 15.2.10
@angular-devkit/schematics 15.2.10
@angular/cdk 15.2.9
@angular/material 15.2.9
@schematics/angular 15.2.10 (cli-only)
rxjs 7.4.0
typescript 4.8.4</kbd>
app.module.ts:
import { KeycloakAngularModule, KeycloakService } from 'keycloak-angular';
function initializeKeycloak(keycloak: KeycloakService) {
return () =>
keycloak.init({
config: {
url: 'http://localhost:7080/',
realm: 'realm-data',
clientId: 'client-id-data'
},
initOptions: {
pkceMethod: 'S256',
redirectUri: 'http://localhost:4200',
},
loadUserProfileAtStartUp: false
});
}
…
..
.
providers: [
provideAnimations(),
DatePipe,
{
provide: APP_INITIALIZER,
useFactory: initializeKeycloak,
multi: true,
deps: [KeycloakService]
}
],
app-routing module ts
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{
path: 'home',
title: 'Home',
component: HomeComponent,
canActivate: [AuthKeyClockGuard],
data: {
roles: ['ADMIN']
}
},
..
.
auth route ts
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { KeycloakAuthGuard, KeycloakService } from 'keycloak-angular';
import { KeycloakProfile } from 'keycloak-js';
import { LoginUser } from '../model/class/authenticateModel/login-user';
@Injectable({
providedIn: 'root'
})
export class AuthKeyClockGuard extends KeycloakAuthGuard {
user = new LoginUser();
public userProfile: KeycloakProfile | null = null;
constructor(protected override readonly router: Router, protected readonly keycloak: KeycloakService) {
super(router, keycloak);
}
public async isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
// Force the user to log in if currently unauthenticated.
console.log(this.authenticated);
if (!this.authenticated) {
await this.keycloak.login({
redirectUri: window.location.origin + state.url
});
} else {
this.userProfile = await this.keycloak.loadUserProfile();
console.log(this.userProfile);
this.user.authStatus = 'AUTH';
this.user.firstName = this.userProfile.firstName || '';
this.user.email = this.userProfile.email || '';
window.sessionStorage.setItem('userdetails', JSON.stringify(this.user));
}
// Get the roles required from the route.
const requiredRoles = route.data['roles'];
// Allow the user to to proceed if no additional roles are required to access the route.
if (!(requiredRoles instanceof Array) || requiredRoles.length === 0) {
return true;
}
// Allow the user to proceed if all the required roles are present.
return requiredRoles.some((role) => this.roles.includes(role));
}
}
Hoping somebody could help me solve this error.
I tried changing init on keycloak but still the same. Tried also changing config on Keycloak admin. And also try to find on other issues with similar error but failed.
Armstrong17 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.