Repository navigation
Problem in intiating keycloak in Angular +17 (standalone) #32427
Unanswered
Skanderlass
asked this question in
Q&A
Replies: 3 comments 5 replies
|
Please close this discussion [1] and reach out to the third-party library's vendor for support [2], after trying out this tutorial [3]. [1] https://docs.github.com/en/discussions/managing-discussions-for-your-community/managing-discussions#closing-a-discussion |
0 replies
|
I issue was mismatch between angular and keycloak version |
2 replies
|
thank |
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi folks!
In order to explore keyclaok i've integrated keycloak with traditional angular (with ngModule) everything works fine.
I tried with Angular standalone I faced initialzing keyclaok issues i followed the official docs.
-> https://www.npmjs.com/package/keycloak-angular
versions:
Angular CLI: 17.3.8
Node: 20.9.0
Package Manager: npm 10.1.0
OS: win32 x64
My attempt
main.ts
`import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);
`
config
`import { APP_INITIALIZER, ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { KeycloakService, KeycloakBearerInterceptor } from 'keycloak-angular';
import {
HTTP_INTERCEPTORS,
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import { initializeKeycloak } from './init/init.factory';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
KeycloakService,
{
provide: APP_INITIALIZER,
useFactory: initializeKeycloak,
multi: true,
deps: [KeycloakService],
},
{
provide: HTTP_INTERCEPTORS,
useClass: KeycloakBearerInterceptor,
multi: true,
},
provideHttpClient(
withInterceptorsFromDi() // tell httpClient to use interceptors from DI
),
],
};
`
initate keyclaok
`import { KeycloakService } from 'keycloak-angular';
export function initializeKeycloak(keycloak: KeycloakService) {
return () => {
keycloak.init({
config: {
url: 'http://localhost:8080',
realm: 'api',
clientId: 'api-sk',
},
initOptions: {
onLoad: 'check-sso',
silentCheckSsoRedirectUri:
window.location.origin + '/assets/silent-check-sso.html',
},
});
};
}
`
-- Guards
`import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
Router,
RouterStateSnapshot,
} from '@angular/router';
import { KeycloakAuthGuard, KeycloakService } from 'keycloak-angular';
@Injectable({
providedIn: 'root',
})
export class AuthGuard extends KeycloakAuthGuard {
constructor(
protected override readonly router: Router,
protected readonly keycloak: KeycloakService
) {
super(router, keycloak);
console.log('🍾');
}
public async isAccessAllowed(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) {
const keycloakInstance = this.keycloak.getKeycloakInstance();
console.log('🍉');
return new Promise((resolve) => {
keycloakInstance. (authenticated) => {
console.log('🥝[authenticated out]:', authenticated);
if (!authenticated) {
console.log('🥝[authenticated]:', authenticated);
console.log(
'🥝[redirectUri]:',
this.keycloak.login({
redirectUri: window.location.origin + state.url,
})
);
await this.keycloak.login({
redirectUri: window.location.origin + state.url,
});
resolve(false);
return;
}
}
}
`
-- routes
`export const routes: Routes = [
{ path: '', component: HomeComponent, canActivate: [AuthGuard] },
{ path: '404', component: AppComponent },
{ path: '**', redirectTo: '404' },
];
`
I got token response with success

I got two main issues

Issue 1
issue 2
i enter to infint loop
http://localhost:4200/home#state=28751f41-5b91-4bdc-8261-a3106873fbaf&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=719fd44c-3f44-443b-b844-8480b2def447.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=ba49a31a-97d2-47ce-896e-64194fa6bfe2&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=60345bb4-0f86-4941-b44a-515cf346b837.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=7906cb39-e907-49ab-8a23-a055f6056eac&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=daf61721-0f54-424c-a234-15ea537cce72.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=cfa2a135-7275-4670-87f6-7792c248316e&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=79319911-bc2e-4c0c-b783-876513e837b9.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=c4ead43a-558b-4995-8299-c427c391d2aa&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=2fe703a0-d0bb-4a4f-96cb-ac0ab8cce36d.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=7440777a-4507-4ecc-a4fd-f8947f9cc1ca&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=5158bcfd-7b23-45ce-bcf7-81656923b0a8.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=f989b2ec-b39e-4fe8-93fe-3e816d8ec8e3&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=bd0b01a5-da33-4f0b-901c-ab4230439389.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=43cec4cb-9705-4d5a-83c4-89105ad024de&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=4647c72f-459c-4bf6-a087-08f5a4dbabc6.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=1e5e47e6-bf83-437f-8db1-3175f7097bd1&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=d1903654-de00-4a99-9876-e68b74bdd9e2.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=cab0fbac-2de4-444e-83fd-0ea212b73e78&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=da23f2a2-144c-4610-8c40-c127a75ce8ef.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=dcbab03f-59f6-46ba-95ec-3dd2ebfa17cc&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=924de49e-f54e-4f86-84a2-13b6600680a2.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=b69743d3-081a-4e9d-b8ab-591abc8bdddf&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=c196bda7-8792-4d4e-bd5b-fc5ed8dbeb7c.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=fc7700f4-308a-41ec-aeff-ccb247a59c55&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=e4eed428-771c-44b0-881c-a0e4806d8e96.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=a836dd0f-01d7-403e-9a0f-9dffd1f15a2d&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=9711912a-8dec-4a74-90f1-f8d3e69ed240.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=41767e30-070e-4333-8956-a797bdad446b&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=81be70b5-28b4-4db6-b70a-7f9c6735c0ca.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=c832197f-6b3d-46b3-87db-332d6f840c54&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=74bb57d3-7c21-4213-ab64-a5cdec19f974.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=38b7bd06-ee33-456b-819d-687dd47510d1&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=c75092b0-1c2f-45c9-9cd6-9a82e8f24be6.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=37f3fcfe-9a0e-4986-80c7-0c5810134348&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=e15eb34f-9c6a-45f9-a95a-b13187a10fff.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=6fb88c51-2f11-438e-8bbc-e25a304bcb9e&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=be9ede81-f445-47cb-a74a-e8abf732cc41.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=14c7a73a-89e9-46d3-8c2b-83da82cc84c0&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=3205d704-18ac-44cf-86c2-74cb7d2b158c.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=9ee4d009-3027-4369-bfa3-bd7d4c7f789c&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=faa941bf-d960-4016-8e19-190895903d92.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=1f80ff38-e008-4274-9286-32f5d9fef637&session_state=ff2e51fd-0b7a-4bb7-8890-d9 8000 8597d96267&iss=http://localhost:8080/realms/api&code=a5ba7353-2a98-4c76-bb1d-026f105d6ed1.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=70cd4282-c18b-415e-8056-af659a012ee6&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http://localhost:8080/realms/api&code=39713b91-3fa2-4393-83d0-d21a75edf547.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292&state=f2aaf774-fe22-4229-8a72-bb3776f2df9e&session_state=ff2e51fd-0b7a-4bb7-8890-d98597d96267&iss=http%3A%2F%2Flocalhost%3A8080%2Frealms%2Fapi&code=a4bae0e6-f216-4384-b1d9-4b3d689a68d0.ff2e51fd-0b7a-4bb7-8890-d98597d96267.0d3669e1-070b-486e-8169-4f6941820292
here is my keyclaok config

best regards
All reactions