My authentication service works almost fine, but the problem is that I'm getting the information if user is logged or not from an asynchro http request.
ActivationGuard.ts
import {Injectable} from '@angular/core';
import {Router, RouterStateSnapshot, ActivatedRouteSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {UserAuthenticationService} from './UserAuthenticationService';
interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean
}
@Injectable()
export class ActivationGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userService.isUserAuthenticated == false) {
this.router.navigate(['/']);
return false;
}
return true;
}
}
UserAuthenticationService.ts
import {Injectable, OnInit} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class UserAuthenticationService implements OnInit {
isUserAuthenticated: boolean = false;
username: string = 'admin';
constructor(private http: Http) {}
ngOnInit(){
this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.subscribe(res => {
this.isUserAuthenticated = res.json();
},
err => {
console.error('An error occured.' + err);
});
}
}
Everytime I'm getting false
(it doesn't allow me to enter the app even if I'm logged).
But if I change the isUserAuthenticated: boolean = false;
line from the UserAuthenticationService.ts
file to true
- it never kicks me out - isUserAuthenticated
is always true.
May it be caused by the http request and it's asynchro nature? Or maybe there's a better way to deal with it?
Looking forward for any help. Thank you.