next-ucap-messenger/projects/ucap-webmessenger-app/src/app/guards/auth.guard.ts

42 lines
1002 B
TypeScript
Raw Normal View History

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';
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);
}
});
}
}