Hello,
I just went though an Angular course and the example there is using a
subject inside a function, which returns an observable.
For the example the firebase api is used and that works nice.
Now I rebuild that with my own api, using ng2-webstorage.
This works like the example with the menubar, but it throws off the router
when called with CanActivate in a guard.
The code inside my AuthService looks like that:
isAuthenticated() {
const state = new Subject<boolean>();
this.localStorageService.observe('myToken')
.subscribe((newToken: MyToken) => {
if ((newToken !== null) && (newToken.token !== '')) {
state.next(true);
} else {
state.next(false);
}
});
return state.asObservable();
}
the code in the example looks like that:
isAuthenticated() {
const state = new Subject<boolean>();
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
state.next(true);
} else {
state.next(false);
}
});
return state.asObservable();
}
the menubar I mentioned before subscribes to isAuthentificated in its
constructor and that works as expected.
the rooter guard looks like that in both cases:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authService.isAuthenticated().first();
}
as I said before, the example is working fine, but with my
isAuthentificated version the router ends up in an endless loop, bcause the
function always returns a null observeable.
I posted that already on stackoverflow
<http://stackoverflow.com/questions/43960678/angularjs-and-rxjs-subject-headache-with-canactivate>,
but didn't receive any answer, which would explain this different behaviour
to me.
So I hope noone gets upset here about crossposting, in case I apologize,
I'd just really like to understand what's going on here or better to say
why I always get a null observeable back.
Thanks
mat
As I'm
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.