简体   繁体   English

Firebase使用电子邮件登录失败,但未提供错误

[英]Firebase Sign In With Email fails without providing an error

In my JavaScript Firebase application, I have tried to set up user authentication via email, and at the same time sync user data to Firebase's realtime database. 在我的JavaScript Firebase应用程序中,我尝试通过电子邮件设置用户身份验证,同时将用户数据同步到Firebase的实时数据库。 While our Google sign in worked with no problems, the function that makes accounts, firebase.auth().createUserWithEmailAndPassword(email, password); 虽然我们的Google登录没有问题,但可以创建帐户的功能firebase.auth().createUserWithEmailAndPassword(email, password); fails to execute and (annoyingly) does not throw any error messages. 无法执行,并且(令人讨厌)不会引发任何错误消息。 Here is the code: main.js: (The problematic section is submitAcc()) 这是代码:main.js :(有问题的部分是submitAcc())

var config = {
    apiKey: "censored",
    authDomain: "censored",
    databaseURL: "censored",
    projectId: "censored",
    storageBucket: "censored",
    messagingSenderId: "censored"
};
firebase.initializeApp(config);
var database = firebase.database();
function showAccCreate() { //Hides and shows account create button
    var x = document.getElementById("hiddenaccountcreation");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
function submitAcc() { //On submit button pressed
    alert("Signing in");
    var email = document.getElementById("emailinput").value;
    var password = document.getElementById("passinput").value;
    var username = document.getElementById("usernameinput").value;
    //console.log(email + password +username);
    var user;
    alert("recorded values");
    firebase.auth().createUserAndRetrieveDataWithEmailAndPassword(email,password).then(function(result) {
        alert("Gets into .then");
        var user = firebase.auth().currentUser;
        var uidvalue = user.uid;
        console.log(uidvalue);
        console.log(uidvalue);
        alert("User value recorded");
        writeUserDataFromEmailSignin(email, username,uidvalue);
        alert(user.uid);
    }).catch(function(error) {
        alert(error.message);
        console.log(error.message);
        console.log(error.code);
    });
}


//Testing if auth state changes
firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
        alert("User is signed in.");
        document.getElementById("debugtest").innerHTML = "Signed in";
    }
});
function writeUserDataFromEmailSignin(email, name, uuid) { //Writes user data to database if user signs in
    alert("Entered function");
    database.ref('users/' + uuid).set({
        "name": name,
        "email": email,
        "uid": uuid,
    }).then(function() {
        alert("Completed");
    }).catch(function() {
        console.log(error.message);
        console.log(error.code);
    })
}
function showsignin() {
    var x = document.getElementById("hiddensignin");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
function googlesignin() { //Signs people into app via Google
    var provider = new firebase.auth.GoogleAuthProvider();
    provider.addScope("https://www.googleapis.com/auth/contacts.readonly");
    firebase.auth().languageCode = 'en';
    firebase.auth().signInWithPopup(provider).then(function(result) {
        var token = result.credential.accessToken; //Google Auth access token
        var user = result.user; //Contains all user info that Google provided us
        writeToDatabaseFromGoogle(user.email, user.displayName, user.uid, user.photoUrl);
    }).catch(function(error) {
        console.log(error.message);
        console.log(error.code);
    });


}
function writeToDatabaseFromGoogle(email, name, uuid, image_url) { //Writes user data to database from Google signin
    database.ref("users/" + uuid).set({
        "name": name,
        "email": email,
        //"imageUrl": image_url,
        "uid": uuid,
    }).catch(function(error) {
        console.log(error.message);
        console.log(error.code);
    });
}
function signInUser() { //Uses email sign-in so signin to existing account
    var email = document.getElementById("emailreauth");
    var pass = document.getElementById("passreauth");
    // noinspection JSUnresolvedFunction
    firebase.auth().signInWithEmailAndPassword(email, pass).catch(function (error) {
        //Handle errors here
        let errorCode = error.code;
        let errorMessage = error.MESSAGE;
        console.log(errorCode);
        console.log(errorMessage);
    });
}

and the index.html file: 和index.html文件:

<!DOCTYPE html>
<!--suppress HtmlRequiredLangAttribute -->
       <html>
    <head>
        <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase.js"></script>
        <script>
            // Initialize Firebase
            var config = {
                apiKey: "AIzaSyAhglAXFWaJhtvOrfeugAMgJHrBw5CUNEc",
                authDomain: "projectcrosscomm.firebaseapp.com",
                databaseURL: "https://projectcrosscomm.firebaseio.com",
                projectId: "projectcrosscomm",
                storageBucket: "projectcrosscomm.appspot.com",
                messagingSenderId: "412861101382"
            };
            firebase.initializeApp(config);
        </script>
        <!-- Firebase App is always required and must be first -->
        <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-app.js"></script>

        <!-- Add additional services that you want to use -->
        <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-auth.js"></script>
        <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-database.js"></script>


        <!-- Comment out (or don't include) services that you don't want to use -->
        <!-- <script src="https://www.gstatic.com/firebasejs/5.8.5/firebase-storage.js"></script> -->


        <script src="main.js" rel="script"></script>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Project Cross Comm!</title>
        <link rel="stylesheet" href="stylesheet.css">
    </head>
    <body>
    <button id="accountcreate" onclick="showAccCreate()">Create your account here!</button>
    <button id="showsignin" onclick="showsignin()">Already have an account?</button>
    <button2 id="googlesignin" onclick="googlesignin()">Sign in with Google</button2>

        <h1>Project Cross Comm!</h1>
        <h2 id="subtitle">
        Welcome to <strong>Project Cross Comm!</strong>
        </h2>
        <img height="200px" src="https://i.kym-cdn.com/entries/icons/mobile/000/013/564/doge.jpg" width="260px" alt="If you can't see this image you're a pleb">
        <h2></h2>
        <p id="desc"> Project Cross Comm is a free to use chatting program that runs in your browser. All the chats are encrypted, so no one can read your chats. Enjoy the program and chat away.</p>
        <div id="hiddenaccountcreation">
            <form>
                <fieldset>
                    <legend>Account Creation</legend> <!--Create account via email sign-in-->
                    <p>
                        <label for="usernameinput">Username</label>
                        <input type="text" id="usernameinput" name="createUsername" placeholder="Username">
                    <p>
                        <label for="emailinput">Email</label>
                        <input type="email" id="emailinput" value="" placeholder="example@example.com" name="createEmail">
                    </p>
                    <p>
                        <label for="passinput">Password</label>
                        <input type="password" id="passinput" value="" placeholder="password" name="createPass">
                    </p>
                    <button id="submit" onclick="submitAcc()">SUBMIT</button>
                </fieldset>
            </form>
        </div>
        <div id="hiddensignin">
            <form>
                <fieldset>
                    <legend>Sign In</legend>
                    <p>
                        <label for="usernamereauth">Username</label>
                        <input type="text" id="usernamereauth" value="">
                    <p>
                        <label for="emailreauth">Email</label>
                        <input type="email" id="emailreauth" value="">
                    </p>
                    <p>
                        <label for="passreauth">Password</label>
                        <input type="password" id="passreauth" value="">
                    </p>
                    <button id="signin" onclick="signInUser()">SUBMIT</button>
                </fieldset>
            </form>
        </div>
        <div id="getthisblockoutofmygoddamnedway"> <!--Problematic code block that another member of my team put there -->
            <a style = "color: white; "id="link" href="InfoPage.html">Click here for more information.</a>
            <h6></h6>
            <a style = "color: white; "id="link2" href="ChatLayout.html">Click Here To Chat</a>
            <h6></h6>
            <a style = "color: white; "id="link3" href="https://app.termly.io/document/privacy-policy/0ae020d8-ee05-4202-a0c7-d4ff19e8f661">Privacy Policy </a>

        </div>
    </body>
    <footer>
        <p id="debugtest" class="debug">Haven't Been Signed In Yet</p>
        <noscript>Man, sucks for you! We only support modern, functioning browsers. Maybe you should get JavaScript </noscript>
    </footer>
</html>

The farthest alert my program gets to is alert("recorded values"); 我的程序获得的最远的警报是alert("recorded values"); , no further alerts are executed. ,不再执行任何警报。 Javascript does not throw any errors during the process; 在此过程中,Javascript不会引发任何错误; the console is empty. 控制台为空。 Is there any way to find out what's wrong, or even to force Javascript to be more verbose and log its memory every so often? 有什么方法可以找出问题所在,或者甚至迫使Javascript变得更加冗长,并经常记录其内存?

Can you please try this? 你可以试试看吗? This works in my case. 这对我来说有效。

    firebase.auth().signInWithEmailAndPassword(email, password)
        .then(response => {
            const uid = response.user.uid;   // you have uid
            response.user.getIdToken()
                .then(token => {
                    // do anything with token
                })
                .catch(error => {
                    // any error handling
                })
        })
        .catch(error => {
           // any error handling
        })

Your Current issue is you are not able to store the Values into the DB with the method submitAcc() .This method is called when the user creates the account. 当前的问题是您无法使用submitAcc() method将值存储到数据库中。当用户创建帐户时,将调用此method I have corrected and made some changes please test and let me know if that works for you. 我已更正并进行了一些更改,请进行测试,并让我知道是否适合您。

I have added two functions logout() and status() to understand where the problem is. 我添加了两个函数logout()status()来了解问题所在。 I'd suggest you remove them. 我建议您将其删除。

I have also observed in the method signInUser() . 我还在方法signInUser()观察到了。 You have missed the .value to Email and Password and corrected it. 您已经错过了.valueEmail and Password并更正了它。

See below image once the user clicks to Create the Account.I have logged his input to console. 用户单击创建帐户后,请参见下图。我已将其输入记录到控制台。

在此处输入图片说明

Database Saving user's info: 数据库保存用户信息:

在此处输入图片说明

Code

 var database = firebase.database(); function showAccCreate() { //Hides and shows account create button var x = document.getElementById("hiddenaccountcreation"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } function submitAcc() { //On submit button pressed alert("Signing in"); var email = document.getElementById("emailinput").value; var password = document.getElementById("passinput").value; var username = document.getElementById("usernameinput").value; console.log(email + password +username); alert("recorded values"); firebase.auth().createUserWithEmailAndPassword(email,password).then(function(result) { alert("Gets into .then"); var user = firebase.auth().currentUser; var uidvalue = user.uid; console.log(uidvalue); console.log(uidvalue); alert("User value recorded"); writeUserDataFromEmailSignin(email, username,uidvalue); alert(user.uid); }).catch(function(error) { alert(error.message); console.log(error.message); console.log(error.code); }); } function writeUserDataFromEmailSignin(email, name, uuid) { //Writes user data to database if user signs in alert("Entered function"); database.ref('users/' + uuid).set({ "name": name, "email": email, "uid": uuid, }).then(function() { alert("Completed"); }).catch(function() { console.log(error.message); console.log(error.code); }) } function logout() { firebase.auth().signOut().then(function() { // Sign-out successful. }).catch(function(error) { // An error happened. }); } function status() { firebase.auth().onAuthStateChanged(function(user) { if (user) { var emailv =user.email; console.log("User is signed in. em ankunav enti "+ emailv); } else { console.log("No user is signed in."); } }); } //Testing if auth state changes firebase.auth().onAuthStateChanged(function (user) { if (user) { alert("User is signed in."); document.getElementById("debugtest").innerHTML = "Signed in"; } else { document.getElementById("debugtest").innerHTML = "NOT LOGGED IN "; } }); function showsignin() { var x = document.getElementById("hiddensignin"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } function signInUser() { //Uses email sign-in so signin to existing account var email = document.getElementById("emailreauth").value; var pass = document.getElementById("passreauth").value; // noinspection JSUnresolvedFunction firebase.auth().signInWithEmailAndPassword(email, pass).catch(function (error) { //Handle errors here let errorCode = error.code; let errorMessage = error.MESSAGE; console.log(errorCode); console.log(errorMessage); }); } 
 <!DOCTYPE html> <!--suppress HtmlRequiredLangAttribute --> <html> <head> <script src="https://www.gstatic.com/firebasejs/5.8.6/firebase.js"></script> <script> // Initialize Firebase var config = { apiKey: "hmcalreac", authDomain: "kbckyc", databaseURL: "https://abc.dmc", projectId: "test12d", storageBucket: "t11", messagingSenderId: "11" }; firebase.initializeApp(config); </script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Project Cross Comm!</title> </head> <body> <button id="accountcreate" onclick="showAccCreate()">Create your account here!</button> <button id="showsignin" onclick="showsignin()">Already have an account?</button> <button2 id="googlesignin" onclick="googlesignin()">Sign in with Google</button2> <h1>Project Cross Comm!</h1> <h2 id="subtitle"> Welcome to <strong>Project Cross Comm!</strong> </h2> <img height="200px" src="https://i.kym-cdn.com/entries/icons/mobile/000/013/564/doge.jpg" width="260px" alt="If you can't see this image you're a pleb"> <h2></h2> <p id="desc"> Project Cross Comm is a free to use chatting program that runs in your browser. All the chats are encrypted, so no one can read your chats. Enjoy the program and chat away.</p> <div id="hiddenaccountcreation"> <fieldset> <legend>Account Creation</legend> <!--Create account via email sign-in--> <p> <label for="usernameinput">Username</label> <input type="text" id="usernameinput" name="createUsername" placeholder="Username"> <p> <label for="emailinput">Email</label> <input type="email" id="emailinput" value="" placeholder="example@example.com" name="createEmail"> </p> <p> <label for="passinput">Password</label> <input type="password" id="passinput" value="" placeholder="password" name="createPass"> </p> <button id="submit" onclick="submitAcc()">SUBMIT TO CREATE ACCOUNT </button> </fieldset> </div> <div id="hiddensignin"> <fieldset> <legend>Sign In</legend> <p> <label for="usernamereauth">Username</label> <input type="text" id="usernamereauth" value=""> <p> <label for="emailreauth">Email</label> <input type="email" id="emailreauth" value=""> </p> <p> <label for="passreauth">Password</label> <input type="password" id="passreauth" value=""> </p> <button id="signin" onclick="signInUser()">SUBMIT To Signin to console</button> </fieldset> </div> <button id=mystat onclick="status()">CLICK me to GET Status</button> <button id=mystat onclick="logout()">CLICK me to logout </button> <div id="getthisblockoutofmygoddamnedway"> <!--Problematic code block that another member of my team put there --> <a style = "color: white; "id="link" href="InfoPage.html">Click here for more information.</a> <h6></h6> <a style = "color: white; "id="link2" href="ChatLayout.html">Click Here To Chat</a> <h6></h6> <a style = "color: white; "id="link3" href="https://app.termly.io/document/privacy-policy/0ae020d8-ee05-4202-a0c7-d4ff19e8f661">Privacy Policy </a> </div> <script src="ne2.js" rel="script"></script> </body> <footer> <p id="debugtest" class="debug">Haven't Been Signed In Yet</p> <noscript>Man, sucks for you! We only support modern, functioning browsers. Maybe you should get JavaScript </noscript> </footer> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌云/firebase 存储错误:没有 `client_email` 无法签署数据。 ...名称:'SigningError' - Google cloud/firebase storage Error: Cannot sign data without `client_email`. ... name: 'SigningError' Firebase密码和电子邮件身份验证失败,并显示错误 - Firebase password and email authentification fails with error 使用Firebase注册电子邮件/密码时出现“网络错误”错误 - “A network error has occurred” error on email/password sign up with Firebase react-firebase-hook sign in with email and password error massage 问题 - react-firebase-hook sign in with email and password error massage problem 如何使用 email 登录用户并使用 firebase + svelte 登录密码? - How to sign in a user with email and password with firebase + svelte? 使用电子邮件和密码 firebase 以角度硬编码登录 - Hardcode sign in with email and password firebase in angular Firebase 函数从 Firestore 数据库读取失败且没有错误 - Firebase Functions Read from Firestore Database fails without an error 我的 firebase 登录表单出现问题,因为错误:“电子邮件”必须是有效字符串。” - Having problems on my sign in form for firebase because of Error:“email” must be a valid string." Google firebase 首次登录时显示没有 email 选项 - Google firebase Sign-in show no email option on first sign-in 使用smtp客户端发送邮件无需提供密码 - Use smtp client to send email without providing password
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM