Skip to content

Routing and KeycloakGuard

Stan Silvert edited this page Feb 5, 2018 · 4 revisions

Routing with the KeycloakGuard

By default, keycloak-schematic will secure your entire application and force the user to log in before visiting any pages/routes. But if you only wish to secure certain routes, you can do that too. keycloak-schematic installs a KeycloakGuard that you can use for this purpose.

To get started, go to main.ts

Change onLoad: 'login-required' to onLoad: 'check-sso'.

KeycloakService.init(configOptions, {onLoad: 'check-sso'})

Now, add the KeycloakGuard to your routing module.

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PrivateComponent } from './private/private.component';
import { PublicComponent } from './public/public.component';
import {KeycloakGuard} from './keycloak-service/keycloak.guard';

const routes: Routes = [
    { path: 'public', component: PublicComponent },
    { path: 'private', component: PrivateComponent, canActivate: [KeycloakGuard] }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [KeycloakGuard]
})
export class AppRoutingModule { }

Clone this wiki locally