-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-guard.service.ts
More file actions
27 lines (22 loc) · 954 Bytes
/
auth-guard.service.ts
File metadata and controls
27 lines (22 loc) · 954 Bytes
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
import {ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot} from '@angular/router';
import {Injectable} from '@angular/core';
import {AuthService} from '../auth.service';
import {Observable} from 'rxjs';
@Injectable({providedIn: 'root'})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
return this.authService.isAuthenticated().then((authenticate: boolean) => {
if (authenticate) {
return true;
} else {
this.router.navigate(['/']);
return false;
}
});
}
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
return this.canActivate(childRoute, state);
}
}