Luis Cerv
Luis Cerv

Reputation: 78

How to set user data after createUserWithEmailAndPassword (web)

I have search on other questions but I can't find anything, so I'm sorry if this is a duplicated.

I'm trying to save extra user data on sign up to database, the signup is not a problem but I can't store the data, I'm trying this:

//add a real time listener
   firebase.auth().onAuthStateChanged(firebaseUser =>{



    if (user != null) {


      firebase.database().ref().child( firebaseUser.uid ).set( {
        /**store data**/
      } );

    console.log("Sign-in provider: "+ firebaseUser.providerId);
    console.log("Provider-specific UID: "+ firebaseUser.uid);
    console.log("name: "+firebaseUser.name);
    console.log("email: "+firebaseUser.email);
    console.log("country:"+firebaseUser.country);
     //console.log("  Photo URL: "+firebaseUser.photoURL);



    //window.location.href = "index.html";


     }else{
      console.log('not logged in');

     }
});

Upvotes: 3

Views: 1038

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600036

You can store any of the properties of the Firebase User. This should work:

firebase.database().ref().child( firebaseUser.uid ).set( {
    firebaseUser.displayName,
    lastSignInTimestamp: Date.now()
});

If that doesn't work, your user might not have permission to write to the location in the database. To troubleshoot that, attach an error listener:

firebase.database().ref().child( firebaseUser.uid ).set( {
    firebaseUser.displayName,
    lastSignInTimestamp: Date.now()
}.catch(function(error) {
    console.error(error);
});

If indeed it's a security problem, see the first blue note in the Firebase documentation about writing data to the database.

Upvotes: 1

Related Questions