I have many methods that require the currentUserId. I have been injecting the my AuthService getting and storing the user like so:
// AuthService code
getAuthorizationState() {
return this.afAuth.authState;
}
// Feed Component Code
constructor(...auth: AuthService...) {
this.user = this.auth.getAthorizationState();
this.user.subscribe(
(value) => {
console.log('value', value);
this.currentUser = value;
}
);
}
// This tends to throw an error can not get property uid of undefined
getUserFeed(uri: string): Obervable<any> {
return this.getFeed(`feed/${this.currentUser.uid})`)
}
// So have been resorting to this
getUserFeed(uri: string): Obervable<any> {
this.user.map((user) => {return user.uid})
.flatMap((currentUserId) => {
return this.getFeed(`feed/${currentUserId})`)
});
}
OR: if I do a setTimeout before I run getFeed (instead of chaining to the user Observer) it works as well.
Issue : Been having to do that with everymethod. In angularjs I remember I believer using the resolve method to help with this issue.
Question: Is a there an 'app global' way to get and store current authenticated user's info so it is already there when calling a method in a component or injector service?
My stackoverflow question:
https://stackoverflow.com/questions/46797323/how-to-get-current-authenticated-user-angularfire2-angular-so-that-it-is-usabl