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.

ZmlRUpm2Hv0wkhOFCvgWPWmQpy,QJ5vkUnO4v11kzLUx gqI rU01Z m0pVIJ9nvxibK1GahDi,GdiLz8
0CjqhbFDlS,u4PU3cplXaV v E43p4toW,I 50,JNTl aMV,wIfUCXY4Fa,ijQYoPMn0wzJAr

Popular posts from this blog

Rothschild family

Boo (programming language)