-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TIMOB-5994 - Facebook #722
Changes from 10 commits
7a9748c
1170111
b81ab59
fbdd6de
c1cdd80
59e8079
3b64152
ec249d7
f91735c
443c61e
c4bc042
516b395
f72b309
b0b7236
1d7348c
2a510f8
c6374ed
bdd9c1c
b34efd2
f587c7d
57356e3
0f715e6
fbe3379
2d3cce4
a28bc55
4fcd118
c5ca33e
6c194a0
41ec287
ecd76a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,12 @@ | |
set: function(val){return _expirationDate = val;} | ||
}); | ||
|
||
var _forceDialogAuth = null; | ||
Object.defineProperty(api, 'forceDialogAuth', { | ||
get: function(){return _forceDialogAuth;}, | ||
set: function(val){return _forceDialogAuth = val;} | ||
get: function(){return true;}, | ||
set: function(val){return true;} | ||
}); | ||
|
||
var _loggedIn = null; | ||
var _loggedIn = false; | ||
Object.defineProperty(api, 'loggedIn', { | ||
get: function(){return _loggedIn;}, | ||
set: function(val){return _loggedIn = val;} | ||
|
@@ -44,32 +43,160 @@ | |
get: function(){return _uid;}, | ||
set: function(val){return _uid = val;} | ||
}); | ||
|
||
var _initSession = function(response) { | ||
var ar = response.authResponse | ||
if (ar) { | ||
// Set the various status members | ||
_loggedIn = true; | ||
_uid = response.authResponse.userID; | ||
_expirationDate = new Date((new Date()).getTime() + response.authResponse.expiresIn * 1000); | ||
|
||
// Set a timeout to match when the token expires | ||
response.authResponse.expiresIn && setTimeout(function(){ | ||
api.logout(); | ||
}, response.authResponse.expiresIn * 1000); | ||
|
||
// Fire the login event | ||
var undef; | ||
api.fireEvent('login', { | ||
cancelled : false, | ||
data : response, | ||
error : undef, | ||
source : api, | ||
success : true, | ||
type : undef, | ||
uid : _uid | ||
}); | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
// Setup the Facebook initialization callback | ||
var _facebookInitialized = false; | ||
var _authAfterInitialized = false; | ||
window.fbAsyncInit = function() { | ||
FB.init({ | ||
appId : _appid, // App ID | ||
status : true, // check login status | ||
cookie : true, // enable cookies to allow the server to access the session | ||
oauth : true, // enable OAuth 2.0 | ||
xfbml : true // parse XFBML | ||
}); | ||
FB.getLoginStatus(function(response){ | ||
// Calculate connected outside of the if statement to ensure that _initSession runs and isn't optimized out if _authAfterInitialized is false | ||
var connected = (response.status == "connected") && (_initSession(response)); | ||
if (!connected && _authAfterInitialized) { | ||
api.authorize(); | ||
} | ||
_facebookInitialized = true; | ||
}); | ||
}; | ||
|
||
// Create the div required by Facebook | ||
fbDiv = document.createElement('div'); | ||
fbDiv.id = 'fb-root'; | ||
document.getElementsByTagName('body')[0].appendChild(fbDiv); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to reliably do document.body.appendChild(fbDiv); |
||
|
||
// Load the Facebook SDK Asynchronously. | ||
|
||
var fbScriptTag, id = 'facebook-jssdk'; | ||
if (!document.getElementById(id)) { | ||
fbScriptTag = document.createElement('script'); | ||
fbScriptTag.id = id; | ||
fbScriptTag.async = true; | ||
fbScriptTag.src = "//connect.facebook.net/en_US/all.js"; | ||
head = document.getElementsByTagName ("head")[0] || document.documentElement; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are declaring a global variable. No good. Need to put a "var" here. Do we need the "|| document.documentElement"? Can't we guarantee there will always be a head tag? After all, we control the html template file. |
||
head.insertBefore(fbScriptTag, head.firstChild); | ||
} | ||
|
||
// Methods | ||
api.authorize = function(){ | ||
console.debug('Method "Titanium.Facebook.authorize" is not implemented yet.'); | ||
|
||
// Sanity check | ||
if (_appid == null) { | ||
throw new Error('App ID not set. Facebook authorization cancelled.'); | ||
} | ||
|
||
// Check if facebook is still initializing, and if so queue the auth request | ||
if (!_facebookInitialized) { | ||
_authAfterInitialized = true; | ||
return; | ||
} | ||
|
||
// Authorize | ||
FB.login(function(response) { | ||
if (!_initSession(response)) { | ||
var undef; | ||
api.fireEvent('login', { | ||
cancelled : true, | ||
data : response, | ||
error : "The user cancelled or an internal error occured.", | ||
source : api, | ||
success : false, | ||
type : undef, | ||
uid : response.id | ||
}); | ||
} | ||
}, {'scope':_permissions.join()}); | ||
}; | ||
api.createLoginButton = function(){ | ||
console.debug('Method "Titanium.Facebook.createLoginButton" is not implemented yet.'); | ||
api.createLoginButton = function(parameters){ | ||
throw new Error('Method "Titanium.Facebook.createLoginButton" is not implemented yet.'); | ||
}; | ||
api.dialog = function(){ | ||
console.debug('Method "Titanium.Facebook.dialog" is not implemented yet.'); | ||
api.dialog = function(action,params,callback){ | ||
if (!_loggedIn) return; | ||
params.method = action; | ||
FB.ui(params,function(response){ | ||
if (!response) { | ||
var undef; | ||
callback({'success':false,'error':undef,'action':action,'source':api}); | ||
} else if (response.error) { | ||
callback({'success':false,'error':response.error,'action':action,'source':api}); | ||
} else { | ||
callback({'success':true,'result':response,'action':action,'source':api}); | ||
} | ||
}); | ||
}; | ||
api.logout = function(){ | ||
console.debug('Method "Titanium.Facebook.logout" is not implemented yet.'); | ||
if (!_loggedIn) return; | ||
FB.logout(function(response) { | ||
_loggedIn = false; | ||
var undef; | ||
api.fireEvent('logout', { | ||
source : api, | ||
type : undef | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You do not need to pass in a type here and you can get rid of undef. fireEvent() will set the type for you. In this case it will automatically set type to "logout". Even if you set type to "hello", it gets clobbered. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is true of all events being passed through fireEvent then, correct? I think there are a few other areas where we can get rid of this. |
||
}); | ||
}); | ||
}; | ||
api.request = function(){ | ||
console.debug('Method "Titanium.Facebook.request" is not implemented yet.'); | ||
api.request = function(method,params,callback){ | ||
if (!_loggedIn) return; | ||
params.method = method; | ||
params.urls = 'facebook.com,developers.facebook.com'; | ||
FB.api(params,function(response){ | ||
if (!response) { | ||
var undef; | ||
callback({'success':false,'error':undef,'method':method,'source':api}); | ||
} else if (response.error) { | ||
callback({'success':false,'error':response.error,'method':method,'source':api}); | ||
} else { | ||
callback({'success':true,'result':JSON.stringify(response),'method':method,'source':api}); | ||
} | ||
}); | ||
}; | ||
api.requestWithGraphPath = function(){ | ||
console.debug('Method "Titanium.Facebook.requestWithGraphPath" is not implemented yet.'); | ||
api.requestWithGraphPath = function(path,params,httpMethod,callback){ | ||
if (!_loggedIn) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If not logged in, there's no way to know if the request failed. You should fire callback with success false. |
||
FB.api(path,httpMethod,params,function(response){ | ||
if (!response) { | ||
var undef; | ||
callback({'success':false,'error':undef,'path':path,'source':api}); | ||
} else if (response.error) { | ||
callback({'success':false,'error':response.error,'path':path,'source':api}); | ||
} else { | ||
callback({'success':true,'result':JSON.stringify(response),'path':path,'source':api}); | ||
} | ||
}); | ||
}; | ||
|
||
// Events | ||
api.addEventListener('login', function(){ | ||
console.debug('Event "login" is not implemented yet.'); | ||
}); | ||
api.addEventListener('logout', function(){ | ||
console.debug('Event "logout" is not implemented yet.'); | ||
}); | ||
})(Ti._5.createClass('Titanium.Facebook')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to initialize this variable correctly. That's fine to default to false, but then later after Facebook's API is loaded, query it to see if you're logged in. If I log in, then reload, I can't query because it thinks I'm not logged in. However, I still have a valid session and I'm able to post status updates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read my new comment for line 76 below...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it doesn't work that way. "Logged in" is something of a misnomer, and a bad choice of words IMO on the part of Appcelerator. It's not that the user is logged in, but rather that the application is authorized. This must be done every time because the application must have an access token to make queries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but updating a status message does not require a token? We need things to work across reloads.