Skip to content

Instantly share code, notes, and snippets.

@grantges
Created May 1, 2014 01:51
Show Gist options
  • Save grantges/eb85c53be5dc6b9c13b4 to your computer and use it in GitHub Desktop.
Save grantges/eb85c53be5dc6b9c13b4 to your computer and use it in GitHub Desktop.
Titanium - Login with Facebook and Create a User In ACS
/**
* Create an instance of the facebook module (need to include this in the tiapp.xml)
*/
var fb = require('facebook');
/*
* Initiate the facebook module with appropriate APP ID and PERMISSIONS ARRAY (developer.facebook.com)
*/
fb.appid = FACEBOOK_APP_ID;
fb.permissions = ['publish_stream']; // Permissions your app needs
fb.forceDialogAuth = true;
/**
* Setup the `login` event listener on the Facebook Module
* NOTE: The assumption here is that the user has setup their phone with Facebook Credentials, otherwise
* it will try to open the browser and get permissions from Facebook via the web - if you go this route, you need
* to setup your facebook app settings according to the documentation (http://docs.appcelerator.com/titanium/latest/#!/api/Modules.Facebook)
*/
fb.addEventListener('login', function(e) {
/**
* You did something right - success!
*/
if (e.success) {
alert('Logged In');
/**
* Social Integrations with ACS a brief tutorial:
* This currently only works with Facebook, Twitter, and LinkedIin
*
* By passing the received oAuth token, this API will access the associated service to retrieve the user information
* create a new ACS User Object for you. Repeaet logins will leverage the same user.
*/
Cloud.SocialIntegrations.externalAccountLogin({
type: 'facebook',
token: fb.accessToken // <--- The access token is stored on the facebook module
}, function (e) {
if (e.success) {
var user = e.users[0];
alert('Success:\n' +
'id: ' + user.id + '\n' +
'first name: ' + user.first_name + '\n' +
'last name: ' + user.last_name);
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
} else if (e.error) {
alert(e.error);
} else if (e.cancelled) {
alert("Canceled");
}
});
/**
* Everything is all set to capture the Facebook login event and handle the cloud service - so lets go ahead and request
* authorization from Facebook
*/
fb.authorize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment