0.0.2 • Published 2 years ago

ngx-rbac-adapter v0.0.2

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

📖 Implementation

First step after installing is add the next properties at: environment.ts.

export const environment = {
  production: false,
  server: {rbacServer},
  group: {rbacGroupId},
  code: {rbacApplicationId}
};

Then, you have to add NgxRbacAdapterModule into imports of app.module.ts, and instance forRoot() method with enviroments values:

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';

import { environment } from '../environments/environment';

import { NgxRbacAdapterModule } from 'ngx-rbac-adapter';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgxRbacAdapterModule.forRoot({
        server: environment.server,
        group: environment.group,
        code: environment.code,
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

For the last, you have to call fetchRoles({email}) method of NgxRbacAdapterService, when you log in the application:

Example of login method

...

public constructor(
    private login: LoginService,
    private _rbacService: NgxRbacAdapterService
) { }

public login(): void {
    this._authService.login().subscribe({
        next: (data: any): void => {
            this._rbacService.fetchRoles({email: 'theUserLogEmail@email.com'}).subscribe();
        }
        error: (error: any): void => { console.log('Error while logging in...'); }
        complete: (): void => { console.log('Login completed!'); }
    });
}
...

🔥 Usage

You simply have to protect de route with NgxRbacAdapterGuard using canActivate and canLoad, and add the code at data: {} route:

import { NgxRbacAdapterGuard } from 'ngx-rbac-adapter';
...
    { 
        path: 'home', 
        component: HomeComponent, 
        canActivate: [NgxRbacAdapterGuard], 
        canLoad: [NgxRbacAdapterGuard], 
        data: { code: 'function-code' } 
    },