npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nes/angular-auth-keycloak

v0.3.8

Published

Keycloak OIDC Authorization Code Flow authentication for Angular 6 web apps.

Downloads

20

Readme

angular-auth-keycloak

A library that wrap Keycloak ( an OpenID Connect server implementation ) own Javascript Adapter, allowing an Angular app to rely on Authorization Code Flow for user authentication.

Authorization Code Flow is the default option and the one for which this library has been built. Other flows could work too, but are not officially supported.

It has been developed with Angular 6 and Keycloak 4.5 as targets. It's possible the library could work also with different targets version but, again, it's not officially supported.

Installation

npm install --save @nes/angular-auth-keycloak

Usage

Add Keycloak Javascript Adapter script to your page, you can source it directly from your running Keycloak server instance.

<script type="text/javascript" src="http://my-keycloak-server-address/auth/js/keycloak.js"></script>

Import AngularAuthKeycloakModule into the root application module, providing configuration settings.

Using forRoot module method:

  • the first argument define Keycloak context
  • the second argument define the behavior that AuthenticatedUserGuard must adopt in case the user is not authenticated, there are two built-in implementations:
    • LoginIfUnauthenticated: redirect to Keycloak login page, it's the default behavior if none is specified
    • NavigateToRouteIfUnauthenticated: navigate to a route of current Angular application specified through UNAUTHENTICATED_USER_REDIRECTION_ROUTE injection token
  • the third argument is optional, and define the behavior that AuthorizedUserGuard must adopt in case the user account does not meet authorization criteria specified in the route, there is one built-in implementation:
    • NavigateToRouteIfUnauthorized: navigate to a route of current Angular application specified through UNAUTHORIZED_USER_REDIRECTION_ROUTE injection token

You are free to roll your own behaviors by extending UnauthenticatedUserReaction and UnauthorizedUserReaction abstract classes.

const oidcSettings = {
  url: 'http://my-keycloak-server-address/auth',
  realm: 'my-secured-realm',
  clientId: 'this-relying-client-id'
};

@NgModule({
  declarations: [...],
  imports: [
    ...
    AngularAuthKeycloakModule.forRoot(
      oidcSettings,
      LoginIfUnauthenticated,
      NavigateToRouteIfUnauthorized
    ),
    ...
  ],
  providers: [
    ...
    {
      provide: UNAUTHORIZED_USER_REDIRECTION_ROUTE,
      useValue: '/unauthorized'
    },
    ...
  ],
  bootstrap: [...]
})
export class AppModule { }

Use AuthenticatedUserGuard to protect routes you want only authenticated user to access:

const routes: Routes = [
  ...
  { path: 'protected-page', component: ProtectedPageComponent, canActivate: [ AuthenticatedUserGuard ] }
  ...
];

Use AuthorizedUserGuard to protect routes you want only user with a specific role to access:

const routes: Routes = [
  ...
  { path: 'protected-page', component: ProtectedPageComponent, data: { authorization: [ 'admin' ] }, canActivate: [ AuthorizededUserGuard ] }
  ...
];

Development

Library

The source code is located in ./projects/angular-auth-keycloak/

To build it use ng build angular-auth-keycloak, it will then be available in ./dist/angular-auth-keycloak/.

Sample App

The source code is located in ./src/app/

To run the sample execute ng serve, it will then be available at http://localhost:4200.