1
|
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
|
if (!this.authService.getUser()) {
|
26
|
this.router.navigate(['']);
|
27
|
}
|
28
|
return !!this.authService.getUser();
|
29
|
}
|
30
|
}
|