1 |
2ddd52c9
|
hlavja
|
import { Injectable } from '@angular/core';
|
2 |
|
|
import {
|
3 |
|
|
CanActivate,
|
4 |
|
|
ActivatedRouteSnapshot,
|
5 |
|
|
RouterStateSnapshot,
|
6 |
|
|
UrlTree, Router
|
7 |
|
|
} from '@angular/router';
|
8 |
|
|
import { Observable } from 'rxjs';
|
9 |
|
|
import { AuthService } from '../services/auth.service';
|
10 |
|
|
|
11 |
|
|
@Injectable({
|
12 |
|
|
providedIn: 'root'
|
13 |
|
|
})
|
14 |
|
|
export class AuthGuard implements CanActivate {
|
15 |
|
|
|
16 |
|
|
constructor(
|
17 |
|
|
private authService: AuthService,
|
18 |
|
|
private router: Router
|
19 |
|
|
) {}
|
20 |
|
|
|
21 |
|
|
canActivate(
|
22 |
|
|
next: ActivatedRouteSnapshot,
|
23 |
|
|
state: RouterStateSnapshot
|
24 |
|
|
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
25 |
2c5da396
|
hlavja
|
if (!this.authService.getUser()) {
|
26 |
2ddd52c9
|
hlavja
|
this.router.navigate(['']);
|
27 |
|
|
}
|
28 |
2c5da396
|
hlavja
|
return !!this.authService.getUser();
|
29 |
2ddd52c9
|
hlavja
|
}
|
30 |
|
|
}
|