title: How to get user role from JWT
body: <p>In my application I have 3 different roles, ADMIN, USER, TEACHER. I log in and the REST API returns me the token. The problem is that I want to get the type of user it is, for example, if it is admin, but it is not working and I don't understand why. The code i hace to detect if is an admin is</p>
  <pre><code>public isAdmin(): boolean{
      if(!this.isLogged()){
        return false;
      }
      const token = this.getToken();
      const payload = token.split('.')[1];
      const decoded = window.atob(payload)
      const values = JSON.parse(decoded);
      const roles = values.sub;
      if(roles.indexOf('ROLE_ADMIN') &lt; 0){
        return false;
      }
      return true;
    }
  </code></pre>
  <p>After this, what I want to do is to redirect to one route or another according to this role. In addition to hide elements in the view with *ngIf(!isAdmin). I don't know if you can help me, thanks.</p>
  <p>This is the header component</p>
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { Router } from '@angular/router';
  import { TokenService } from '../services/token.service';
  
  @Component({
    selector: 'app-header',
    templateUrl: './header.component.html',
    styleUrls: ['./header.component.css']
  })
  export class HeaderComponent implements OnInit {
  
  
    isAuthenticated = false;
    isAdmin = false;
  
    constructor(
      private tokenService: TokenService,
      private router: Router
    ) { }
  
    ngOnInit() {
      this.isLogged();
      this.isAdmin = this.tokenService.isAdmin();
    }
  
    onLogOut(): void{
      this.tokenService.logOut();
      this.router.navigate(['/']);
    }
  
    isLogged(): boolean{
      return this.isAuthenticated = this.tokenService.isLogged();
    }
  
  
  
  }
  </code></pre>
  <pre><code>&lt;nav class=&quot;navbar navbar-expand-lg navbar-light bg-light&quot;&gt;
      &lt;div class=&quot;container-fluid&quot;&gt;
        &lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;KanbanSense 2.0.&lt;/a&gt;
        &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#navbarSupportedContent&quot; aria-controls=&quot;navbarSupportedContent&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
          &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
        &lt;/button&gt;
        &lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent&quot; *ngIf=&quot;isAdmin&quot;&gt;
          &lt;ul class=&quot;navbar-nav me-auto mb-2 mb-lg-0&quot;&gt;
            &lt;li class=&quot;nav-item&quot;&gt;
              &lt;a class=&quot;nav-link active&quot; aria-current=&quot;page&quot; href=&quot;#&quot;&gt;Inicio&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class=&quot;nav-item&quot; &gt;
              &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Almacenes&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class=&quot;nav-item dropdown&quot;&gt;
              &lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbarDropdown&quot; role=&quot;button&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;
                Material
              &lt;/a&gt;
              &lt;ul class=&quot;dropdown-menu&quot; aria-labelledby=&quot;navbarDropdown&quot;&gt;
                &lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Buscar&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Stocks&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;Informes&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
          &lt;form class=&quot;d-flex me-2&quot;&gt;
            &lt;div *ngIf=&quot;!isLogged()&quot;&gt;
              &lt;button class=&quot;btn btn-primary&quot; routerLink=&quot;/login&quot;&gt;Login&lt;/button&gt;
            &lt;/div&gt;
            &lt;div *ngIf=&quot;isLogged()&quot;&gt;
              &lt;button class=&quot;btn btn-danger&quot; (click)=&quot;onLogOut()&quot;&gt;Logout&lt;/button&gt;
            &lt;/div&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/nav&gt;
  </code></pre>
  <p>Thanks</p>