I'm trying to make an angular 2 version of an old fashion application. There is a service that sends recurring request to a server to check if user is being logged in or not. The login guard will check the polling to see if the login session result( get from the request) is valid or not to return a right signal to the route.
This is the function is my service:
getUser(): Observable<User> {
return this.http.get<User>(this.userUrl);
}
This is the canActive function from my guard:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
// const isLoggedIn = this.authService.isLoggedIn();
return this.authService.getUser().flatMap((data) => {
console.log(data);
if (data.id === 1 && data.name === 'Beany' ) {
console.log(1111)
return Observable.of(true);
} else {
console.log(2222)
this.router.navigate(['pages/login']);
return Observable.of(false);
}
});
The route works but only 1 time. Not sure if it's possible to make this action repeat (the service will resend the check to the service when the request is finish, of course I should make a little timeout for it)?