0

I would like to create variables that will be accessible in all components of the Angular application.

I create a service that allows the user to connect, I would like to retrieve the user information at the time of connection and assign them in variables that can be accessible in all components.

As for console.log (data.val ()) it shows me that the data is well presented. I use Angular 6.

recupInfo(){
 firebase.auth().onAuthStateChanged((user) => {
 if (user) {
   var ref = firebase.database().ref('users/' + user.uid + 
              '/username').on('value',(data) =>
      var username = data.val() ;   
  );
}
});
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • And what exactly is your question/problem? – André Kool Aug 27 '18 at 14:13
  • I would like to retrieve the information of the user, in variables at the time of the connection. Because the user can create users, so the uid will normally change when the new user is created. – Happy rotter Aug 27 '18 at 14:21
  • 1
    I'm having a hard time understanding what you are asking exactly. What is _"the time of the connection"_? And when you say _"so the uid will normally change when the new user is created"_ are you talking about [this problem](https://stackoverflow.com/q/37517208/4916627)? Also what have you tried yourself so far? Where exactly are you failing, having a problem with? – André Kool Aug 27 '18 at 14:29
  • Yes that's my problem, that's why I wanted to store the information when connecting variables as its uid change will not pose any problems. [My problem](https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user). – Happy rotter Aug 27 '18 at 15:15

1 Answers1

1

A - You can save it to localStorage and then get it when you need it,

B - You can make a public property of a service and inject that service where you need it, so the public propert will became available to the component.

C -Same as B, but using a BehavourSubject, so if you subscribe to it in your component, you will be notified of changes.

I am sure there are many other ways of achieving this.

wFitz
  • 1,266
  • 8
  • 13