Facebook Javascript SDK Cookie

时间:2013-03-27 22:08:32

标签: javascript facebook backbone.js sdk

我为我的Node.js Applikation写了一个模型(骨干),用Facebook Javascript SDK实现了身份验证。

我的模型代码在这里:

define(['jquery','underscore','backbone'], function($,_,Backbone) {
 var facebookUserModel = Backbone.Model.extend({
  initialize: function() {

    console.log('FacebbokUserModel initialized');

    // bind statusChange to this context
    _.bindAll(this, 'statusChange');

    // be notified when the session changes        
    FB.Event.subscribe('auth.authResponseChange', this.statusChange);
  }

  //saves actual status
  ,_loginStatus : null

  //return true/false if user is logged in
  ,isLoggedIn : function() {
    return this._loginStatus === 'connected';
  }

  //check whether user is logged in or not
  //used for firsttime invoke
  ,getLoginStatus : function() {
    _this = this;
    FB.getLoginStatus(function(response) {
      _this.statusChange(response);
    });
  }     

  //log user in
  ,login: function() {
    FB.login(function() {});
  }

  //log user out
  ,logout: function() {
    FB.logout();
  }

  //treat changes on status
  ,statusChange: function(response) {
    console.log('statuschange');
    if(this._loginStatus === response.status) return false;

    var event;

    if(response.status === 'not_authorized') {
      event = 'fb:unauthorized';
    }else if(response.status === 'connected') {
      event = 'fb:connected';     
    }else {
      event = 'fb:disconnected';
    }

    console.log(event);
    // RESPONSE IS SHOWN CORRECTLY EVEN IN FIREFOX
    console.log(response);
    this._loginStatus = response.status;
    this.trigger(event, this, response);  
  }

  ,sync: function(method, model, options) {      
    if(method !== 'read') throw new Error('FacebookUser is a readonly model, cannot perform ' + method);

    var callback = function(response) {
      if(response.error) {
        options.error(response);
      } else {
        options.success(model, response, options);
      }
      return true;
    };

    FB.api('/me', callback);
  },      

});

return facebookUserModel;
});

Initfunction

FB.init({
  appId      : 'myappId', // App ID
  channelUrl : 'channelurl', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true,  // parse XFBML
  oauth      : true
});

当我执行此代码时,它在Chrome和IE中运行良好。在服务器端,我可以很容易地验证这个cookie但我有一个Firefox的问题。 Firefox正确显示响应(clientside),但它不会创建fbsr-cookie。有人可以给我一个提示吗?我该如何处理这个问题?

招呼

0 个答案:

没有答案