我为我的 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;
});
初始化函数
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 正确显示响应(客户端),但它不会创建 fbsr-cookie。有人可以告诉我为什么吗?我该如何处理这个问题?
打招呼