ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - mselerin/ngx-security: Security directives for your Angular application to show/hide elements based on a user roles / permissions.
Security directives for your Angular application to show/hide elements based on a user roles / permissions. - mselerin/ngx-security
Visit Site

GitHub - mselerin/ngx-security: Security directives for your Angular application to show/hide elements based on a user roles / permissions.

GitHub - mselerin/ngx-security: Security directives for your Angular application to show/hide elements based on a user roles / permissions.

Ngx-Security

License: MIT npm CI codecov

:closed_lock_with_key: Security directives for your Angular application to show/hide elements based on a user roles / permissions.

View changelog

View migration guide

Installation

Install the library with:

npm install ngx-security --save

Then import it in your AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

// Import your library
import { NgxSecurityModule } from 'ngx-security';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    
    // Importing ngx-security module
    NgxSecurityModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage

The security directives use a security state controlled by the NgxSecurityService.
You need to set/change this state to use the directives:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NgxSecurityService } from 'ngx-security';

@Component({
  selector: 'app-sample',
  templateUrl: './sample.component.html'
})
export class SampleComponent
{
  constructor(
    private http: HttpClient,
    private security: NgxSecurityService
  ) {}

  login() {
    this.security.setAuthenticationChecker(() => {
      return of(true);
    });
    
    this.security.setPermissionChecker((perm: string) => {
      return this.http.get(`/api/auth/permissions/has/${perm}`).pipe(
        map(() => true)
      );
    });
  }
  
  logout() {
    // Reset the security state to it's initial value
    this.security.reset();
  }
}

Of course, you can change the security state wherever and whenever you want !

You can now use the differents directives and the guard.

Directives

IsAuthenticated

<div *secuIsAuthenticated>I'm authenticated !</div>

IsAnonymous

<div *secuIsAnonymous>I'm an anonymous user (not authenticated)</div>

HasRoles / HasPermissions / IsMemberOf

<div *secuHasRoles="'ADMIN'">I have the role 'ADMIN'</div>
<div *secuHasRoles="['CREATOR', 'EDITOR']; else roleElse">I have the role 'CREATOR' and 'EDITOR'</div>
<ng-template #roleElse>
  <div>I don't have the roles</div>
</ng-template>

HasAnyRoles / HasAnyPermissions / IsMemberOfAny

<div *secuHasAnyRoles="['CREATOR', 'EDITOR']; else roleElse">I have the role 'CREATOR' or 'EDITOR'</div>
<ng-template #roleElse>
  <div>I don't have the roles</div>
</ng-template>

HasNotRoles / HasNotPermissions / IsNotMemberOf

<div *secuHasNotRoles="'POWERUSER'">I don't have the role 'POWERUSER'</div>

Route Guard

The NgxSecurityGuard can prevent an unauthorized user to load / access parts of your application.

import {
  ActivatedRouteSnapshot,
  Route, Routes,
  RouterStateSnapshot
} from '@angular/router';

import { NgxSecurityGuard } from 'ngx-security';

export const ROUTES: Routes = [
  {
    path: 'secured-page',
    canActivate: [ NgxSecurityGuard ],
    data: {
      security: {
        isAuthenticated: true,
        hasAllRoles: ['ADMIN', 'USER'],
        redirectTo: '/access-denied',
        unauthorizedHandler: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => {
          console.warn('No, no, no, you cannot access this !');
        }
      }
    },
    component: SecuredComponent
  }
];

Tips

Log unauthorized access

You can use the unauthorizedHandler to log unauthorized access to route path :

unauthorizedHandler: (route: Route | ActivatedRouteSnapshot, state?: RouterStateSnapshot) => {
  let path = (state ? state.url : null);
    if (!path && route) {
      path = '/' + (route as Route).path;
    }
  
    console.warn('Unauthorized access', path);
}

Contributing

Feel free to introduce a feature request, an issue or a pull request. :ok_hand:

Changelog

Changelog is available here.

License

MIT

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory