Firebase自定义登录失败(JavaScript)

时间:2015-04-19 19:27:00

标签: javascript firebase-security

我试图开始使用firebase,现在使用它的安全部分。我尝试使用Firebase网站上的指南和代码段来尽可能简单地开始使用它。

为了简单起见,我有一个包含密码(id" Code")和用户输入字段(id" Door")的网页。如何检查在#34; Code"字段中输入的密码?等于已存储在节点https://mydatabase.firebaseio.com/loapp_users/BAAJ/password中的密码,BAAJ是存储在节点loapp_users中的一个用户的用户ID,所有用户都有子节点"密码"?

下面的代码似乎没有做到这一点。

$(document).ready(function(){  
    // Monitoring User Authentication State

    // Use the onAuth() method to listen for changes in user authentication state

    // Create a callback which logs the current auth state
    function authDataCallback(authData) {
      if (authData) {
        console.log("User " + authData.uid + " is logged in with " + authData.provider);
      } else {
        console.log("User is logged out");
      }
    }
    // Register the callback to be fired every time auth state changes
    var ref = new Firebase("https://mydatabase.firebaseio.com");
    ref.onAuth(authDataCallback);

    $("#logout").click(
        function logout() {
            ref.unauth();
            ref.offAuth(authDataCallback);
        }
    );

    // LOGIN
    // The code to authenticate a user varies by provider and transport method, but they all have similar signatures and 
    // accept a callback function. Use it to handle errors and process the results of a successful login.

    // Create a callback to handle the result of the authentication
    function authHandler(error, authData) {
      if (error) {
        console.log("Login Failed!", error);
      } else {
        console.log("Authenticated successfully with payload:", authData);
      }
    };

    $("#login").click(
        function() {
            var usersRef = new Firebase("https://mydatabase.firebaseio.com/loapp_users");
            // Authenticate users with a custom Firebase token
            var _user = $("#Door").val();
            var _level = "docent";
            var _password = $("#Code").val();
            var userRef = usersRef.child(_user);
            // Attach an asynchronous callback to read the data at our user reference
            userRef.on("value", function(snapshot) {
                console.log(snapshot.val());
                if (snapshot.val().child("password").text() == _password) {
                    ref.authWithCustomToken("eyJ0e....etc...mlhdCI6MTQyOTM4Mzc0M30.Vn1QF7cRC6nml8HB9NAzpQXJgq5lDrAie-zIHxtOmFk", authHandler);
                    } else {
                    console.log("Gebruikersnaam en code komen niet overeen")
                    }
                }, function (errorObject) {
                  console.log("The read failed: " + errorObject.code);
                });
        }
    );
});

1 个答案:

答案 0 :(得分:0)

snapshot.val().child("password").text() 

应改为:

snaphot.val().password

然后它有效。