如何从firebase获取当前用户身份验证ID。进入Web应用程序

时间:2018-01-09 05:51:30

标签: javascript html authentication firebase-authentication google-cloud-firestore

我尝试将注册用户详细信息存储在Web应用程序各自的电子邮件身份验证ID下。但是这些细节存储在以前的注册用户身份验证ID中。我尝试了很多,但总是显示相同。请给我解决方案存储在正确的用户身份验证ID下。我认为我的错是从firestore获取身份验证ID。 我的signup.html页面是:

<!doctype html>
<html lang="en">
    <head>
        <title> deyaPay</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
         <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-app.js"></script>
         <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-auth.js"></script>
         <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
        <script>
          var config = {
    apiKey: "AIzaSyAJsAstiMsrB2QGJyoqiTELw0vsWFVVahw",
    authDomain: "websignin-79fec.firebaseapp.com",
    databaseURL: "https://websignin-79fec.firebaseio.com",
    projectId: "websignin-79fec",
    storageBucket: "",
    messagingSenderId: "480214472501"
  };
   firebase.initializeApp(config);
   </script>
        <script src="https://www.gstatic.com/firebasejs/4.6.2/firebase-firestore.js"></script>
         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
        <link rel="stylesheet" href="main.css">
    </head>
    <body>
       <h1 style="text-align:center;"> Welcome to Signup page</h1>
    <div class="container">
    <form action="home.html" method ="post"  style="text-align:center"">
       <div class="form-group">
           <label> FullName</label>
           <input type="text" id="name"></br>
           <label> Email </label>
           <input type="email" id="email"></br>
           <label> Password </label>
           <input type="password" id="pass"></br> 
           <label>Phonenumber</label>
           <input type="text" id="phone"></br>
             <div class="form-check">
                   <label class="form-check-label">
                      <input class="form-check-input" type="checkbox"> Remember me
                   </label> 
            </div>
           <!--<button id="submit">Register</button>-->
           <button type="button" onclick="myValidation()"id="submit" class = "btn btn-primary">Register</button>
           <button type ="button" onclick="" id = "login" class = "btn btn-primary">Login</button>
           <a href="login.html">already registered?</a>
        </div>
       </div> 
      </form>  
      <script type ="text/javascript">

           function myValidation() {
           var fullname=document.getElementById("name").value;
           var email=document.getElementById("email").value;
           var pswd=document.getElementById("pass").value;
           var phone=document.getElementById("phone").value;

           if(fullname==''){
            alert("enter firstname and lastname"); 
            return  false;
           }
           if(email==''){
           alert("enter email");
           return false;
           }
          if((pswd=='')||(pswd.length<9)){
           alert("enter password and password must contain 9 characters");
           return false;
           }
           if((phone=='')||(phone.length>10)||(phone.length<10)){
           alert("phonenumber must contain 10 numbers.");
           return false ;
           }
           else{
           const inputTextfield = document.querySelector("#name");
           const Email = document.querySelector("#email");
           const pswd=document.querySelector("#pass");
           const phonenumber = document.querySelector("#phone");
           const submit=document.querySelector("#submit"); 
           submit.addEventListener("click",function() {
           const name = inputTextfield.value;
           const phonumber = phonenumber.value;
           const Email1 = Email.value;
           const password = pswd.value;
           firebase.auth().createUserWithEmailAndPassword(Email1,password).then(function(user) {
            console.log("signup sucessfully created auth id");
            }).catch(function(error) {
            alert(error.message);
            alert(error.code);
             });
             const uid = firebase.auth().currentUser.uid;
             var db = firebase.firestore();
             db.collection("deyaPayusers").doc(uid).set({
             FullName : name,
             Email : Email1,
             PhoneNumber : phonumber
             }).then(function() {
               alert("Document successfully written!");
                window.location.href="home.html";

               }).catch(function(error){
                alert("Got an error",error); 
                 });
             });
             return false;
             }
             }


    </script>
</body>
</html>  

1 个答案:

答案 0 :(得分:0)

当我将数据库部分放入createUserWithEmailAndPassword时,它对我有用。所以createUserWithEmailAndPassword的部分是:

var user = firebase.auth().currentUser;
firebase.auth().createUserWithEmailAndPassword(Email1,password).then(function(user) { // it create a new user starting point of create user
        user.sendEmailVerification().then(function() { // it send verification mail to the respective user
         console.log("verification email sent"); 
         var db = firebase.firestore();
           db.collection("deyaPayusers").doc(firebase.auth().currentUser.uid).set({
           FullName : name,
           Email : Email1,
           PhoneNumber : phonumber
         }).then(function() {
           alert("Document successfully written!");
           window.location.href="home.html";


           }).catch(function(error){  // for when document is not correctly wrote
            alert("Got an error",error); 
             });// end of the firestore data

         }).catch(function(error) {// for when verification email is not sent to respective mails
        alert(error.message);
        alert(error.code);
        });// end of verification mail
         }).catch(function(error) // error occur when new user is already registered
         {
           alert(error.message);
           alert(error.code);
         }); //end of create user method 
          });// end of addEventListener method
         }// end of else loop
        }// end of myValidation ().

我的想法很完美。