I have a situation where there is an auth guard and all routes are below it.
The auth service determines if the user is logged in or not by sending a request to the backend
private subject = new BehaviorSubject<User>(undefined)
user$: Observable<User> = this.subject.asObservable().filter(user => !!user)
isLoggedIn$: Observable<boolean> = this.user$.map(user => !!user.id)
constructor (private http: HttpClient) {
http.get<User>(API['AUTH']['GET_AUTHENTICATED_USER'])
.do(console.log)
.subscribe(user => this.subject.next(user ? user : ANONYMOUS_USER))
}
However auth guard does not seem to wait for the request to finish and gets a boolean false as the predefined value of (isLoggedIn$ observable)
auth guard
constructor (private authService: AuthService, private router: Router) {
this.authService.isLoggedIn$.subscribe(isLoggedIn => this.isLoggedIn = isLoggedIn)
}
is there a way to wait until the request completes or at least get the second value emitted through the observable?
CanActivateorCanLoadthen you should have a methodcanActivateorcanLoadrespectively that returnsObservable<boolean>. You shouldn't need to subscribe. - bygrace