salmanaacvf
salmanaacvf

Reputation: 257

How to get a user id from Firebase Authentication?

how can I get an ID of a user after that has been created , i am creating the user by using createUserWithEmailAndPassword function

firebase.auth().createUserWithEmailAndPassword(email,password)
.then(resp => handleUserCreated(resp.uid, name...))

in handleUserCreated function i try to insert into database that user , the problem is that the ID that it gives me = undefined

const handleUserCreated  = (userId, name ...) =>
firebase.database().ref(`profles/${userId}`)

my rules in database

  {
      "rules": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    }

could you help me to solve that issue of undefined ID ?

Upvotes: 1

Views: 9859

Answers (1)

Manish Ahire
Manish Ahire

Reputation: 1343

Try the following solution, its working fine.

createUser() {

   const { email, password } = this.state;

   firebase.auth().createUserWithEmailAndPassword(email, password)
   .then(data => {  
      console.log("User ID :- ", data.user.uid);
   })
   .catch(error => {
      console.log(error);
   });

}

Upvotes: 3

Related Questions