npm install keycloak-angular [email protected]
import { KeycloakAngularModule, KeycloakService } from 'keycloak-angular';
@NgModule({
imports: [KeycloakAngularModule, ...],
...
})
export class AppModule {
constructor(private keycloak: KeycloakService) {}
}
import { KeycloakService } from 'keycloak-angular';
export class AppComponent {
constructor(private keycloakService: KeycloakService) {
this.keycloakService.init({
config: {
url: 'https://<your-keycloak-server>/auth',
realm: 'your-realm',
clientId: 'your-client-id',
},
initOptions: {
onLoad: 'login-required',
checkLoginIframe: false
},
bearerExcludedUrls: []
});
}
}
import { KeycloakGuard } from 'keycloak-angular';
const routes: Routes = [
{
path: '',
component: PagesComponent,
canActivate: [KeycloakGuard],
children: [
...
],
},
{ path: '**', redirectTo: 'pages/dashboard' },
];
import { KeycloakService } from 'keycloak-angular';
export class SomeComponent {
constructor(private keycloakService: KeycloakService) {}
someMethod() {
if (this.keycloakService.authenticated) {
const token = this.keycloakService.getToken();
// Use the token to make authenticated requests
}
}
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.