-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-auth-guard.service.ts
44 lines (39 loc) 路 1.39 KB
/
no-auth-guard.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, CanActivateChild } from '@angular/router';
import { AuthService } from './auth.service';
import { RoleType } from 'thesis-dais-internship-manager-core';
import { canExec } from '../helpers/can-exec.helper';
/**
* The NoAuth guard service. Used to determine if the user can access the desierd routes.
*/
@Injectable()
export class NoAuthGuardService implements CanActivate, CanActivateChild {
/**
* Creates an instance of NoAuthGuardService.
* @param {AuthService} auth The authService
* @param {Router} router The router
*/
constructor(
public auth: AuthService,
public router: Router) {
}
/**
* Return true if the current user can navigate the given route, false otherwise.
* @param route The route the user is navigating to
*/
canActivate(route: ActivatedRouteSnapshot): boolean {
if (this.auth.currentUser && this.auth.currentUser.role && this.auth.token) {
console.log("User is authenticated, redirect to '/auth'");
this.router.navigate(['/auth']);
return false;
}
return true;
}
/**
* Return true if the current user can navigate the children of the route, false otherwise.
* @param route The route the user is navigating to
*/
canActivateChild(route: ActivatedRouteSnapshot): boolean {
return this.canActivate(route);
}
}