title: Routing and Authorization in Angular 7
body: <p>I try to implement standard JWT authorization with routing in Angular 7. The part of my app-routing.module.ts:</p>
  
  <pre><code>{
      path: '',
      component: MainComponent,
      canActivate: [AuthGuard]
        },
    {
      path: '401',
      component: UnauthorizeComponent
    }
  </code></pre>
  
  <p>I also have auth.service.ts:</p>
  
  <pre><code>const helper = new JwtHelperService();
  const myRawToken = localStorage.getItem('token');
  const isExpired = helper.isTokenExpired(myRawToken);
  
  @Injectable()
  export class AuthService {
  
  ...
  
    public isAuthenticated(): boolean {
      return !isExpired;
  </code></pre>
  
  <p>Also - auth-guard.service.ts:</p>
  
  <pre><code>@Injectable()
  export class AuthGuardService  implements CanActivate {
  
  ...
  
    canActivate(): boolean {
      if (!this.auth.isAuthenticated()) {
        this.router.navigate(['401']);
        return false;
      }
      return true;
    }
  }
  </code></pre>
  
  <p>And in Header component I have dropdown-list with "login" item and my code there is:</p>
  
  <pre><code>sendLogin() {
  
      const body = new HttpParams()
        .set('username', this.loginForm.value.login)
        .set('password', this.loginForm.value.password);
  
      this.authService.login(body.toString())
        .pipe(
          catchError(err =&gt; {
            return ......;
          }))
        .pipe(
          tap(resp =&gt; {
            localStorage.setItem('token', resp.headers.get('Authorization'));
            this.router.navigate(['']);
          }))
        .subscribe();
    }
  </code></pre>
  
  <p>So, it works fine, but <em>this.router.navigate([''])</em> in my header component doesn't work. Instead of '' url browser opens <em>this.router.navigate(['401']);</em> because helper.isTokenExpired(myRawToken) can't refresh token. But if I press F5 (refresh) in browser and go to localhost:4200/ - my page opens. </p>
  
  <p>What am I doing wrong? </p>
  
  <p>How to refresh token?</p>