How to add additional information to firebase.auth()

戏子无情 提交于 2019-12-28 15:50:08

问题


How can I add extra attributes phone number and address to this data set? It seems like Firebase documentation doesn't specify anything about that.

I have implemented the login, register and update using firebase.auth()

Login :

//Email Login
firebase.auth().signInWithEmailAndPassword(email, password).then(
   ok => {
        console.log("Logged in User",ok.user);              
    },
    error => {
        console.log("email/pass sign in error", error);
    }
);

Register:

 //Sign Up
firebase.auth().createUserWithEmailAndPassword(email, password).then(
    ok => {
        console.log("Register OK", ok);
    },
    error => {
        console.log("Register error", error);
    }
)

Update:

//User Authentication
firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    $scope.data=user;
  } else {
    // No user, Redirect to login page
  }
});

//Save Function
$scope.save=function(values){

    $scope.data.updateProfile({

      displayName: "Test User",
      email: "test@gmail.com",
     /* phone: 123412341,
      address: "Temp Address",*/
      photoURL: "www.example.com/profile/img.jpg"

    }).then(function() {

     // Update successful.

    }, function(error) {

     // An error happened.

    }); 

};  

回答1:


As far as I know, you have to manage the users profiles by yourself if you want to have more fields than the default user provided by Firebase.

You can do this creating a reference in Firebase to keep all the users profiles.

users: {
  "userID1": {
    "name":"user 1",
    "gender": "male" 
  },
  "userID2": {
    "name":"user 2",
    "gender": "female" 
  }
}

You can use onAuthStateChanged to detect when the user is logged in, and if it is you can use once() to retrieve user's data

firebaseRef.child('users').child(user.uid).once('value', callback)

Hope it helps



来源:https://stackoverflow.com/questions/37926672/how-to-add-additional-information-to-firebase-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!