简体   繁体   中英

How to take user Id when using “createUserWithEmailAndPassword(email, password)” in javascript and firebase

I am creating new web app using firebase and javascript. To be more precise, I registered new user with \\firebase.auth().createUserWithEmailAndPassword(email, password) It is working, but when I used this keyword I want to take UID of a new user and initialize it to a new variable at that moment. After this, I want to add new user in database using this key UID(ie name, age and etc.) I tried to show it using console.log(user.uid) as well in the browser but it is showing undefined. Please help me.

Code.html

<input type="email" id="txtEmail" placeholder="username">
<input type="email" id="txtPass" placeholder="password">

<button id="btnSignUp" type="submit" onclick="Press()"> SignUp</button>

<script src="https://www.gstatic.com/firebasejs/5.2.0/firebase.js"></script>
<script >
        // Initialize Firebase
  var config = {
    apiKey: "api key",
    authDomain: "bigpro-c6a4c.firebaseapp.com",
    databaseURL: "https://bigpro-c6a4c.firebaseio.com",
    projectId: "bigpro-c6a4c",
    storageBucket: "",
    messagingSenderId: "id"
  };
  firebase.initializeApp(config);
</script>

<script>
     function Press(){

  var txtEmail = document.getElementById('txtEmail');
  var txtPass = document.getElementById('txtPass');
  var txtEmail = document.getElementById('txtEmail');
  var txtPass = document.getElementById('txtPass');

    var email = txtEmail.value;
    var password=txtPass.value;
  firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function(user){
          // console.log('uid',user.uid);
          console.log(user.uid);
        }).catch(function(error) {

        });

}

</script>

createUserWithEmailAndPassword returns a promise comtaining a UserCredential -- not a User .

Try this snippet:

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(function(userCredential) {
      console.log(userCredential.user.uid);
    }).catch(function(error) {

    });

The client-side SDK's createUserWithEmailAndPassword() function does not return a user object on successful creation, check out these docs here which explains that " If the new account was created, the user is signed in automatically " and does not mention the user object being returned, and none of their examples have such a thing.

You're thinking of ( or looking at ) the Admin SDK - which DOES return the user object .

Instead, for client side, you need to access the newly created ( and thus currently signed in ) user....

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(function () {
        console.log(firebase.auth().currentUser.uid)
    }).catch(function (error) {
        console.log(error)
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM