How to ensure currentUser.uid is available for service class methods that require it. Angular & Firebase?

15 views
Skip to first unread message

Jamie Hennelly

unread,
Oct 17, 2017, 4:15:26 PM10/17/17
to Angular and AngularJS discussion

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

Sander Elias

unread,
Oct 18, 2017, 3:46:06 AM10/18/17
to Angular and AngularJS discussion
Hi Jamie,

Can't you store it in your AuthService? Also, I would move getUserFeed stuff into its own service, that than can store the user for itself, without you even need to touch the 'user' in the component at all.

Regards
Sander

Jamie Hennelly

unread,
Oct 18, 2017, 5:55:47 AM10/18/17
to Angular and AngularJS discussion

Jamie Hennelly

unread,
Oct 18, 2017, 6:06:26 AM10/18/17
to Angular and AngularJS discussion
Sander, 

Thanks for the reply. Yes should have mentioned I was using an abbreviated version of my true code and negated abstracting it to a service in the question's code. 

I posted the answer that I went with after talking to some people in the gitter community. 
They suggested using a stream (getUserUid helper method to use with methods that need it).
https://stackoverflow.com/questions/46797323/how-to-get-current-authenticated-user-angularfire2-angular-so-that-it-is-usabl 

What you suggested is what I was thinking to do but confused. 
If I store it in the Auth service doesn't act in the same way to if I use a  using a AuthService (Service A) method to call it in and store in ServiceB (that is using the AuthService). 

I mean it still has to be instantiated when DI'ed. When DI'ed  it would then to act to store in the AuthService method. 

Or doe the AuthService Method get Instatiated on app bootstrapped(or at some point before the DI into Service B)
And thereforecan inject the UID that was already stored. 

Sander Elias

unread,
Oct 18, 2017, 10:47:29 PM10/18/17
to Angular and AngularJS discussion
Hi Jamie,

Services are instantiated by the DI upon first use. 
If you use DI to inject the authservice anywhere, it will get instantiated once. From then on, you will reuse the same instance of the service over and over again.

Regards
Sander

Jamie Hennelly

unread,
Oct 19, 2017, 12:26:21 AM10/19/17
to Angular and AngularJS discussion
Thank you Sander, that for sure helps my understanding better about DI(behavior) and opens up other ways to solve it then. 

Cheers,

Jamie
Reply all
Reply to author
Forward
0 new messages