0

我正在尝试使用 Firebase 和 Twitter 设置用户身份验证。此答案中的代码在一定程度上有效:

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, data) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      user = data;          
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();

我可以使用 auth 变量访问dataStore.getAuth()并且 user.id 被记录到控制台。调用dataStore.getAuth().login('twitter')也可以,我被重定向到 twitter 登录页面。

但是,如果我尝试使用外部返回用户 ID,则会dataStore.getUser().id出现cannot read property 'id' of undefined错误。

我想我可能只是user以错误的方式返回,所以如果有人能对这个问题有所了解,我将非常感激。

更新: 感谢@BenjaminWarren 和@SpinyNorman 的回答,我更新了代码以使用正确的变量,如下所示:

var dataStore = (function(){
  var userObj = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, user) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      userObj = user;
      console.log('User ID: ' + userObj.id + ', Provider: ' + userObj.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getUser : function() {
      if (userObj) return userObj;
      // else show some error that it isn't loaded yet;
    },
    getAuth : function() {
      if (auth) return auth;
      // else show some error that it isn't loaded yet;
    }
  }
})();

jQuery(document).ready(function($) {
  console.log(dataStore.getUser().id);
});

控制台输出如下所示:

Uncaught TypeError: Cannot read property 'id' of undefined main.js:37
User ID: 222936694, Provider: twitter main.js:14

所以dataStore.getUser().id在 auth 对象被返回之前被记录,因此是未定义的。我在 getUser() 返回时尝试了不同的 setTimout 值等,但它总是在返回之前返回auth

干杯,

詹姆士

4

2 回答 2

0

尝试这个:

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, user) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (user) {
      // user authenticated with Firebase
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();

查看其他一些参考资料,function(error, user)似乎是常见的格式。你让它看起来的方式else if (user)总是返回 false 因为data被传递而不是user.

于 2013-10-23T09:17:05.983 回答
0

在我看来,当前代码根本不会记录用户 ID,因为该user变量从未更新。它开始于null并且仅设置在 中(...) if(user) { user = data },永远无法到达。我认为该行应该} else if (data) {改为:

var url = 'https://molovo-comments.firebaseio.com/';
var myDataRef = new Firebase(url);

var dataStore = (function(){
  var user = null;

  var auth = new FirebaseSimpleLogin(myDataRef, function(error, data) {
    if (error) {
      // an error occurred while attempting login
      console.log(error);
    } else if (data) {
      // user authenticated with Firebase
      user = data;          
      console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
    } else {
      // user is logged out
      console.log('Not logged in.');
      user = {id: null}; // for example.
    }
  });

  return {
    getAuth : function() {
      if (auth) return auth;
        // else show some error that it isn't loaded yet;
    },
    getUser : function() {
      if (user) return user;
        // else show some error that it isn't loaded yet;
    }
  };
})();

编辑:正如 Benjamin Warren 所指出的,这可能是由于将函数参数的名称从user更改为data以避免与先前定义的user变量发生冲突。我假设日志功能在将参数名称从更改为之后停止user工作data

于 2013-10-23T09:26:01.700 回答