2019-09-18 06:02:21 +00:00
|
|
|
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';
|
|
|
|
|
2019-09-19 09:22:13 +00:00
|
|
|
import { AppAuthenticationService } from '../services/authentication.service';
|
2019-09-18 06:02:21 +00:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthGuard implements CanActivate {
|
|
|
|
constructor(
|
|
|
|
private router: Router,
|
2019-09-19 09:22:13 +00:00
|
|
|
private appAuthenticationService: AppAuthenticationService
|
2019-09-18 06:02:21 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
canActivate(
|
|
|
|
route: ActivatedRouteSnapshot,
|
|
|
|
state: RouterStateSnapshot
|
|
|
|
):
|
|
|
|
| boolean
|
|
|
|
| UrlTree
|
|
|
|
| Observable<boolean | UrlTree>
|
|
|
|
| Promise<boolean | UrlTree> {
|
|
|
|
return new Promise<boolean | UrlTree>((resolve, reject) => {
|
2019-09-19 09:22:13 +00:00
|
|
|
if (this.appAuthenticationService.authenticated()) {
|
2019-09-18 06:02:21 +00:00
|
|
|
resolve(true);
|
|
|
|
} else {
|
|
|
|
this.router.navigateByUrl('/account/login');
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|