Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional information to firebase.auth()

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: "[email protected]",
     /* phone: 123412341,
      address: "Temp Address",*/
      photoURL: "www.example.com/profile/img.jpg"

    }).then(function() {

     // Update successful.

    }, function(error) {

     // An error happened.

    }); 

};  
like image 634
Arjun Sunil Kumar Avatar asked Jun 20 '16 15:06

Arjun Sunil Kumar


Video Answer


2 Answers

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

like image 146
Devid Farinelli Avatar answered Jan 01 '23 21:01

Devid Farinelli


This can be done by directly storing your custom data in Firebase Auth as "custom claims" on each user via the Admin SDK on your backend.

Note this can't be done purely client-side, your server (or you can use a Cloud Function as per the linked guide if you don't already have a server/API set up) needs to make a request through the Admin SDK to securely set the data using the admin.auth().setCustomUserClaims() method:

https://firebase.google.com/docs/auth/admin/custom-claims#defining_roles_via_an_http_request

like image 39
wyqydsyq Avatar answered Jan 01 '23 21:01

wyqydsyq