Enhancement idea - How to handle firebase data specific to a user
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?
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...