Problem: I need to route to the login page if firebase-auth does not have a user signed in and to an alternate page if it does have a user logged in.
To achieve this it would invoke a function in a constructor service.
private isLoginRequired() {
// Firebase auth service observable
let authSub = this.afAuth.authState;
// Current route service observable
let routeSub = this.activatedRoute.url;
//Alternate App Page
//this.router.navigate(["tool/event-management/events"]);
//Login Page
//this.router.navigate(["tool/event-management/login"]);
}
Now, I need to combine these observables in such a way that if a value of any one of them changes I could collect current values of both the observables via single subscribe.
something.subscribe(observableSnapshots => {
let snapshot_auth = observableSnapshots[0];
let snapshot_route = observableSnapshots[1];
if(condition<snapshot_route, snapshot_auth>){
// do anything positive
}
else{
// do anything negative
}
});
I understand that this can be achieved by nesting the subscriptions but that's surely not an efficient way to do it.
I have also tried mergeMap but could not achieve the requirement, here is the snippet (which does not work)
routeSub
.pipe(mergeMap(event1 => authSub.pipe(map(event2 => "test"))))
.subscribe(result => {
console.log(result);
});
Please show me a better way out. Thanks in advance.
Dependency: "rxjs": "~6.3.3"