title: How to link Firebase's Authentication to Realtime Database?
body: <p>As the title said.
  I need to link authenticated user to database.
  So that different user can only see their data.</p>
  
  <p>I had successfully implement the Authentication function of Firebase.
  But the record does not save to Realtime Database and I don't know how to do this.</p>
  
  <p>Can anyone please teach me how do I do such function?
  I tried the database and I roughly know it use (push) to create a unique ID to differentiate user. But still I have no idea how to combine this to authentication function.</p>
  
  <p>The following code is my function of Login() and Register()</p>
  
  <pre><code>login (){
  
      const validate = this.refs.formId.getValue();
      if (validate) {
          firebase.auth().signInWithEmailAndPassword(validate.email, validate.password)
          .then(() =&gt; {
          })
          .catch((error) =&gt; {
              const errorCode = error.code;
              const errorMessage = error.message;
              if (errorCode === 'auth/wrong-password') {
  
              Toast.show({ text: `${Strings.ST30}`, position: 'bottom', buttonText: `${Strings.ST33}` })
  
              }
              else if (errorCode === 'auth/user-not-found') {
  
              Toast.show({ text: `${Strings.ST31}`, position: 'bottom', buttonText: `${Strings.ST33}` })
  
              }
              else{
              Toast.show({ text: `${Strings.ST32}`, position: 'bottom', buttonText: `${Strings.ST33}` })
              }
  
          });
      }
  
  }
      register () {
  
      const validate = this.refs.form.getValue();
      if(this.validate) {
          const errorHandler = ((e)=&gt;{
          console.log(e);
          if(e.code == 'auth/email-already-in-use'){
              Toast.show({ text: `${Strings.ST36}`, position: 'bottom', buttonText: `${Strings.ST33}` })
  
          } else {
              Toast.show({ text: `${Strings.ST32}`, position: 'bottom', buttonText: `${Strings.ST33}` })
          }
  
      })
      firebase.auth().createUserWithEmailAndPassword(validate.email,validate.password).then((response) =&gt; {
          firebase.auth().currentUser.updateProfile({
              displayName : validate.name,
          }).then(()=&gt;{
          }).catch(errorHandler);
  
      }).catch(errorHandler)
  }}
  </code></pre>
  
  <p>Thank you so much for your time and help. Appreciate it a lot!</p>
  
  <p><a href="https://i.stack.imgur.com/e6nd8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/e6nd8.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/vrtcb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vrtcb.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/3LP4i.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3LP4i.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/hvJ33.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hvJ33.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/oOU9E.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oOU9E.png" alt="enter image description here"></a></p>
  
  <p>Edit 2:</p>
  
  <pre><code>        firebase.auth()
      .createUserWithEmailAndPassword(validate.email,validate.password)
      .then((response) =&gt; {
          firebase.auth().currentUser.updateProfile({
              displayName : validate.name,
          }).then(()=&gt;{
              firebase.database()
              .ref('users/' + firebase.auth().currentUser.uid + "/profile")
              .set({
                  Username: validate.name,
                  UserEmail: validate.email,
                  CreationDate: d1.getFullYear() + "/" + (d1.getMonth() + 1) + "/" + d1.getDate()
              })
          }).catch(errorHandler);
      }).catch(errorHandler)
  }}
  </code></pre>
  
  <p>I had successfully link to Firebase Realtime Database.
  <a href="https://i.stack.imgur.com/qdR3G.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qdR3G.png" alt="enter image description here"></a></p>
  
  <p>But I can't write and read other function from my database.
  This is my rules for Firebase Database. May I know which part should I change? </p>
  
  <pre><code>{
  "rules": {
      ".read": false,
      ".write": false,
      "users":
      {
          "$userId":
          {
              ".read": "auth.uid === $userId",
              ".write": "auth.uid === $userId"
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Edit 3: (ExampleSendData.js)</p>
  
  <p>constructor(props) {
   super(props)<br>
   this.state = {
   message: '',
   messages: []
   }</p>
  
  <p>this.addItem = this.addItem.bind(this);
   }</p>
  
  <pre><code>    componentDidMount() {
          firebase
          .database()
          .ref()
          .child("messages")
          .once("value", snapshot =&gt; {
              const data = snapshot.val()
              if (snapshot.val()) {
              const initMessages = [];
              Object
                  .keys(data)
                  .forEach(message =&gt; initMessages.push(data[message]));
              this.setState({
                  messages: initMessages
              })
              }
          });
  
          firebase
          .database()
          .ref()
          .child("messages")
          .on("child_added", snapshot =&gt; {
              const data = snapshot.val();
              if (data) {
              this.setState(prevState =&gt; ({
                  messages: [data, ...prevState.messages]
              }))
              }
          })
  
      }
  
      addItem () {
          if (!this.state.message) return;
  
          const newMessage = firebase.database().ref()
                              .child("messages")
                              .push();
          newMessage.set(this.state.message, () =&gt; this.setState({message: ''}))
      }
  
      render() {
          return (
          &lt;View style={styles.container}&gt;
              &lt;View style={styles.msgBox}&gt;
              &lt;TextInput placeholder='Enter your message'
                  value={this.state.message}
                  onChangeText={(text) =&gt; this.setState({message: text})}
                  style={styles.txtInput}/&gt;
              &lt;Button title='Send' onPress={this.addItem}/&gt;
              &lt;/View&gt;
              &lt;FlatList data={this.state.messages}
              renderItem={
                  ({item}) =&gt; 
                  &lt;View style={styles.listItemContainer}&gt;
                  &lt;Text style={styles.listItem}&gt; {item}
                  &lt;/Text&gt;
                  &lt;/View&gt;
              }
  
              /&gt;
          &lt;/View&gt;
          );
      }
      }
  </code></pre>