jwt token authourize guard in angular

Solutions on MaxInterview for jwt token authourize guard in angular by the best coders in the world

showing results for - "jwt token authourize guard in angular"
Michela
28 Aug 2018
1import { Injectable } from '@angular/core';
2import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
3import { Observable } from 'rxjs'; 
4import { JWTTokenService } from './jwt-token.service';
5import { LocalStorageService } from './storage.service';
6import { LoginService } from './login.service';
7 
8@Injectable({
9  providedIn: 'root'
10})
11export class AuthorizeGuard implements CanActivate {
12  constructor(private loginService: LoginService,
13              private authStorageService: LocalStorageService,
14              private jwtService: JWTTokenService) {
15  }
16  canActivate(
17    next: ActivatedRouteSnapshot,
18    state: RouterStateSnapshot): Observable | Promise | boolean {
19      if (this.jwtService.getUser()) {
20          if (this.jwtService.isTokenExpired()) {
21            // Should Redirect Sig-In Page
22          } else {
23            return true;
24          }
25      } else {
26        return new Promise((resolve) => {
27          this.loginService.signIncallBack().then((e) => {
28             resolve(true);
29          }).catch((e) => {
30            // Should Redirect Sign-In Page
31          });
32        });
33      }
34  }
35}
36
37/*** code for routing***/
38{
39    path: 'app',
40    component: AppComponent,
41    canActivate: [AuthorizeGuard]
42}
43
44
45
46
47
48
49
50
51
52
53
54
55
56