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 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-roles-based-authorisation

v0.0.3

Published

The purpose of this library is to provide no access page, guard and authorisation service which handles whether a user is allowed to enter a page based on his roles. It provides the following:

Downloads

213

Readme

Angular Roles Based Authorisation

The purpose of this library is to provide no access page, guard and authorisation service which handles whether a user is allowed to enter a page based on his roles. It provides the following:

  • No access page
  • Roles based authorisation service
  • Authorisation guard

Installation

Angular Roles Based Authorisation library is available through npm. Install it by using the following command in the terminal:

npm install angular-roles-based-authorisation

You should now be able to see the dependency in the package.json file.

Usage

After successful instalation, start by importing AngularRolesBasedAuthorisationService in the Angular module:

import { AngularRolesBasedAuthorisationService } from "angular-roles-based-authorisation";

Create initializeApp function which will run required initialisation logic when the application starts, for example, login, translation, etc. After the user is logged in, define user roles during application startup by using angularRolesBasedAuthorisationService.setUserRoles() function.

export function initializeApp(
  angularRolesBasedAuthorisationService: AngularRolesBasedAuthorisationService
) {
  return () => {
    // Set user roles at app initialization
    angularRolesBasedAuthorisationService.setUserRoles(['user']);
  };
}

NOTE: Register the APP_INITIALIZER to the providers array of the Angular module to ensure that the initialization logic runs before the application starts:

providers: [    
    {      
        provide: APP_INITIALIZER,
        useFactory: initializeApp,
        deps: [AngularRolesBasedAuthorisationService],
        multi: true
        
    }  
]

After we have set user roles, we have to set up the application routing by assigning required roles that can acces the given route and use the authorisation guard. Here is an example of how to do that:

import { AuthorisationGuard, NoAccessPageComponent } from "angular-roles-based-authorisation";


const routes = [
  { path: '', redirectTo: '', pathMatch: 'full' },
  {
    path: someAdminPath,
    component: someAdminComponent,
    canActivate: [AuthorisationGuard],
    data: { roles: 'admin' }
  },
  {
    path: 'someUserPath',
    component: someUserComponent,
    canActivate: [AuthorisationGuard],
    data: { roles: ['admin', 'user'] }
  },
  { 
    path: 'no-access', 
    component: NoAccessPageComponent,
    data: { contactPerson: 'personToContact' }
  },
];

AuthorisationGuard checks the current user's roles (previously set in the angular module) and only allows access if the user has the required roles specified in the data.roles property.

Defining Roles in Route Data:

You can pass a string or an array of roles depending on your access requirements. For example:

  • data: { roles: 'admin' }: This defines that only users with the admin role can access the someAdminPath route.

  • data: { roles: ['admin', 'user'] }: This allows users with either the admin or user role to access the someUserPath route.

No access page:

When a user tries to access a restricted route without the required roles, they will be redirected to the NoAccessPageComponent.

  • path: 'no-access': The route for the no-access page should be explicitly set to no-access because this path is hardcoded in the library’s authorisation guard to handle access denial.

  • data: { contactPerson: 'adminContact' }: You can pass a custom contactPerson through the route's data property. This allows you to dynamically display a person to contact on the no access page.