1

am using firebase authentication, I refer this link

"users": {
        ".read": "auth != null && root.child('admins').child(auth.uid).val() == true",
        ".write": "auth != null && (root.child('admins').child(auth.uid).val() == true)",
        "$uid": {
          ".read": "auth != null && (auth.uid == $uid || root.child('admins').child(auth.uid).val() == true)",
          ".write": "auth != null && (auth.uid == $uid || root.child('admins').child(auth.uid).val() == true)"
        }
      },

This is my firebase user table rules.

my services.js

signupUser: function(newUser) {
          var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary");
          var usersRef = firebase.database().ref().child('users');
          return secondaryApp.auth()
          .createUserWithEmailAndPassword(newUser.email, newUser.password)
          .then(function(firebaseUser) {
            secondaryApp.auth().signOut();

            var newUserWithoutPwd = _.extend({}, newUser);
            delete newUserWithoutPwd.password;
            return usersRef
            .child(firebaseUser.uid)
            .set(newUserWithoutPwd)
            .then(function() {
              return newUserWithoutPwd;
            });
          });
        },

Authendication is success. But user table show in permission denied error.

Show below screen shot

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
tamilselvancst
  • 478
  • 1
  • 3
  • 19

2 Answers2

3

My guess is that you want the newly created user to write their user profile to the database. In that case it is important that:

  1. you write the profile data as the current user: firebase.database(secondaryApp).ref().child('users')
  2. you don't sign out the user, until the writing is completed

In code:

signupUser: function(newUser) {
      var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary");
      var usersRef = secondaryApp.database().ref().child('users');
      return secondaryApp.auth()
      .createUserWithEmailAndPassword(newUser.email, newUser.password)
      .then(function(firebaseUser) {
        var newUserWithoutPwd = _.extend({}, newUser);
        delete newUserWithoutPwd.password;
        return usersRef
          .child(firebaseUser.uid)
          .set(newUserWithoutPwd)
          .then(function() {
            secondaryApp.auth().signOut();
            return newUserWithoutPwd;
          });
      });
    },
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1
  1. Remove secondaryApp.auth().signOut();
  2. Update this return firebase.database(secondaryApp).ref().child('users').

    signupUser: function(newUser) {
    var secondaryApp = firebase.initializeApp(FirebaseAppConfig, "Secondary");
    return secondaryApp.auth()
      .createUserWithEmailAndPassword(newUserInsert.email, newUserInsert.password)
      .then(function(firebaseUser) {
        var newUserWithoutPwd = _.extend({}, newUserInsert);
        delete newUserWithoutPwd.password;
    
        return firebase.database(secondaryApp).ref().child('users')
         .child(firebaseUser.uid)
         .set(newUserWithoutPwd)
         .then(function() {
       return newUserWithoutPwd;
       });
    });
    },
    
tamilselvancst
  • 478
  • 1
  • 3
  • 19