I have a component (actually, the app component) that calls a firebase anonymous sign in function on ngAfterContentInit
:
ngAfterContentInit(){
firebase.auth().signInAnonymously()
.then(() => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('the uid of the anonymous user is '+ user.uid);
// User is signed in
} else {
// User is signed out
}
});
// Signed in..
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
}
It is working, and the uid
of the anonymous user is getting printed.
However, now I need to access this uid
from another component. I haven't set up an auth.service.ts file or anything, just using the above function.
How would I go about this? I am new to Angular (coming from ReactJS), and in ReactJS I would have put the user information inside the Redux State. Is there something similar in Angular? [I am already using the required modules to connect Angular to Firebase Auth].
Alternatively, is there an easy way to access the signed-in user information without having to deal with state management in the first place?
Many thanks for any help provided.