42 lines
1002 B
TypeScript
42 lines
1002 B
TypeScript
|
import { Injectable } from '@angular/core';
|
||
|
import {
|
||
|
CanActivate,
|
||
|
ActivatedRouteSnapshot,
|
||
|
RouterStateSnapshot,
|
||
|
UrlTree,
|
||
|
Router
|
||
|
} from '@angular/router';
|
||
|
import { Store, select } from '@ngrx/store';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { map, take } from 'rxjs/operators';
|
||
|
|
||
|
import { AuthenticationService } from '../services/authentication.service';
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root'
|
||
|
})
|
||
|
export class AuthGuard implements CanActivate {
|
||
|
constructor(
|
||
|
private router: Router,
|
||
|
private authenticationService: AuthenticationService
|
||
|
) {}
|
||
|
|
||
|
canActivate(
|
||
|
route: ActivatedRouteSnapshot,
|
||
|
state: RouterStateSnapshot
|
||
|
):
|
||
|
| boolean
|
||
|
| UrlTree
|
||
|
| Observable<boolean | UrlTree>
|
||
|
| Promise<boolean | UrlTree> {
|
||
|
return new Promise<boolean | UrlTree>((resolve, reject) => {
|
||
|
if (this.authenticationService.authenticated()) {
|
||
|
resolve(true);
|
||
|
} else {
|
||
|
this.router.navigateByUrl('/account/login');
|
||
|
resolve(false);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|