Angular / Firestore - What's the best way to check the User ID in multiple components?

Multi tool use
Multi tool use


Angular / Firestore - What's the best way to check the User ID in multiple components?



I have an 'authservice.ts' file where I try to manage user auth style things. However, in each of my components, I'm always subscribing to the user to get the user ID. The user ID is then used inside my Firestore functions to query data where the user ID is referenced inside collection and document paths.



Is there a more 'fluent' way of achieving the same thing? Or is this approach actually suitable?



Below are the examples of the auth service and a component file that references the auth service.



auth-service.ts


...
user: Observable<firebase.User>;
...

constructor() {
this.user = afAuth.authState;
}



Any One Of My Components.ts


...

private _subscription: Subscription;

...


ngOnInit() {
this.subscribeToUser();
}


subscribeToUser() {
this._subscription = this.authService.user.subscribe(user => {
if (user) {
this.userID = user.uid;
this.AGetFireStoreDataFunction();
}
});
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

udM6uez1ucy,71AJfkE21JS 0Z6egSoNLH0n,FfX6zxzwuV2sy284YAe1khQA66wFu,Afk,LJdp1W8O2jAi,iFUSC wum
kyjnUUSA8Cs

Popular posts from this blog

Rothschild family

Cinema of Italy