Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement idea - How to handle firebase data specific to a user #2

Open
sstorie opened this issue Sep 13, 2017 · 1 comment
Open

Comments

@sstorie
Copy link

sstorie commented Sep 13, 2017

One thing that puzzles me a bit is how to handle firebase data that uses the uid in the path with ngrx. This has to be a common thing, but is not yet covered in the example provided.

One thought I had was to use an effect that was only active while there was an authenticated user. It seems that OnRunEffects was made for this, but I couldn't quite get the effect pieced together in an understandable way. Right now I simply use a service that has code like this in it:

this.afAuth.authState
  .switchMap(authState => {
    if(authState) {
      return this.db.object(`/some/path/with/${authState.uid}`);
    }

    return Observable.of(null);
  });

With this approach I can automatically update the emit'd values whenever the user authenticates or not, so how would you handle this with ngrx?

@qubeyuval
Copy link

You can store the user or even just the uid in the store on login (authState change) then you have a store value available. If you need the uid in the path you first get a selector and then start the observable chain from it to ensure you get the uid for the rest of the observable piping for what you need to do. This is what I came up with:

user$: Observable<User>;

constructor(private db: AngularFireDatabase,
            private store: Store<State>) {
     this.user$ = this.store.select(getUser);
}
saveUserProfile(dataToUpdate: any) {
    return this.user$
         .map(u => this.db.object(`/users/${u.uid}`)
                                    .update(dataToUpdate));
}

You can add filter to filter only not null user object.
Hope this makes sense...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants