angularroleprovidernebular

Problem with Nebular NbRoleProvider in production mode


This is my role.provider.ts:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators/map';

import { NbAuthService, NbAuthJWTToken } from '@nebular/auth';
import { NbRoleProvider } from '@nebular/security';

@Injectable()
export class RoleProvider implements NbRoleProvider {

  constructor(private authService: NbAuthService) {
  }

  getRole(): Observable<string> {
    return this.authService.onTokenChange()
      .pipe(
        map((token: NbAuthJWTToken) => {
          // console.log(token.getPayload());
          return token.isValid() ? token.getPayload()['role'] : 'guest';
        }),
      );
  }
}

It works fine when in development mode, but when I use production mode I get the error:

ERROR TypeError: Object(...) is not a function
    getRole http://localhost:4200/main.c8da7df94aad960dcfeb.js:1
    value http://localhost:4200/main.c8da7df94aad960dcfeb.js:1        

Solution

  • Solved for me:

    import { of } from 'rxjs';
    import { NbAuthService, NbAuthJWTToken } from '@nebular/auth';
    
    getRole(): Observable<string> {
      let result;
      this.authService.onTokenChange().subscribe((token: NbAuthJWTToken) => {
        if (token.isValid()) {
          result = token.getPayload()['roles'];
        } else {
          result = [];
        }
      });    
      return of(result);
    }