student.auth.guard.ts 782 Bytes
Newer Older
1
import { Injectable } from '@angular/core';
2
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
3
4
import { AuthService } from '../services/auth.service';
import { STUDENT_ROLE } from '../constants';
5
import { Router } from '@angular/router';
6
7
8
9

@Injectable()
export default class StudentAuthGuard implements CanActivate {

10
  constructor(private authService: AuthService, private router: Router) {}
11

12
13
  canActivate(route: ActivatedRouteSnapshot,
  state: RouterStateSnapshot) {
14
15
16
17
18
19
20
21
22
    return this.authService.isLoggedIn(STUDENT_ROLE).then(loggedIn => {
        if (!loggedIn) {
            this.router.navigate(['/logout']);
        }
        return loggedIn;
    }).catch(err => {
        console.log("exception");
        return false;
    });
23
24
  }
}