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

42 lines
1017 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 { AppAuthenticationService } from '../services/authentication.service';
@Injectable({
providedIn: 'root'
})
export class AppAuthGuard implements CanActivate {
constructor(
private router: Router,
private appAuthenticationService: AppAuthenticationService
) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
):
| boolean
| UrlTree
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree> {
return new Promise<boolean | UrlTree>((resolve, reject) => {
if (this.appAuthenticationService.authenticated()) {
resolve(true);
} else {
this.router.navigateByUrl('/account/login');
resolve(false);
}
});
}
}