Firebase - How to write/read data per user after authentication

后端 未结 2 1437
心在旅途
心在旅途 2021-02-04 10:29

I have tried to understand but not able to see how and where might be the data I am storing after login is going.

public static final String BASE_URL = \"https:/         


        
相关标签:
2条回答
  • 2021-02-04 10:39

    First, modify the Firebase rules as follows:

    {
      "rules": {
        "users": {
          "$uid": {
            ".write": "$uid === auth.uid",
            ".read": "$uid === auth.uid"
          }
        }
      }
    }
    

    Then, in Java Code:

    Firebase rootRef = new Firebase("https://user-account.firebaseio.com/");
    // Assuming the user is already logged in.
    Firebase userRef = rootRef.child("users/" + rootRef.getAuth().getUid());
    userRef.child("message1").setValue("Hello World");
    

    In the end, the data would look something like this:

    0 讨论(0)
  • 2021-02-04 10:46

    For web end

    Javascript code:

    var user = firebase.auth().currentUser;
    
    if (user != null) {
       uid = user.uid;
       firebase.database().ref('/users/'+uid).push({
          foo: 'abc',
          bar: 'pqr'
       });
    }
    

    Read https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user for more information.

    0 讨论(0)
提交回复
热议问题