Skip to content

Instantly share code, notes, and snippets.

@bob-sims
Last active December 28, 2015 11:28
Show Gist options
  • Save bob-sims/7493241 to your computer and use it in GitHub Desktop.
Save bob-sims/7493241 to your computer and use it in GitHub Desktop.
Authenticate to Azure Mobile BaaS with Titanium Mobile's native Facebook module (Alloy framework)
// The contents of this file will be executed before any of
// your view controllers are ever executed, including the index.
// You have access to all functionality on the `Alloy` namespace.
//
// This is a great place to do any initialization for your app
// or create any global variables/functions that you'd like to
// make available throughout your app. You can easily make things
// accessible globally by attaching them to the `Alloy.Globals`
// object. For example:
//
// Alloy.Globals.someGlobalFunction = function(){};
// Loads the Facebook module, which can be referenced by Alloy.Globals.Facebook
Alloy.Globals.Facebook = require('facebook');
// Use the Alloy.Globals.Facebook namespace to make Facebook module API calls
var facebook = Alloy.Globals.Facebook;
// substitute your own Azure mobile service URL
var appURL = "https://tiazure.azure-mobile.net/";
// set your Facebook app ID in tiapp.xml
// <property name="ti.facebook.appid">FACEBOOK_APP_ID</property>
// declare Facebook modules in tiapp.xml
// <modules>
// <!-- Add the appropriate line(s) to your modules section -->
// <module platform="android">facebook</module>
// <module platform="iphone">facebook</module>
// </modules>
// retrieve Facebook app ID as set in tiapp.xml
facebook.appid = Ti.App.Properties.getString('ti.facebook.appid');
// set Facebook permissions
facebook.permissions = ['publish_actions'];
// set FB login listener
facebook.addEventListener('login', function(e) {
if (e.success) {
console.info(JSON.stringify(e));
// FB returns accessToken
console.info("token: "+facebook.accessToken);
// pass FB accessToken to Azure authentication method
authAzure({"token":facebook.accessToken});
}
else if (e.error) {
console.info(JSON.stringify(e));
alert(e.error);
}
else if (e.cancelled) {
console.info(JSON.stringify(e));
alert("Canceled");
}
});
// method to retrieve Azure
function authAzure(_opts) {
var endpoint = appURL + "login/facebook";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
// celebration! We've successfully authenticated to Azure
// parse Azure authentication token
var authToken = JSON.parse(this.responseText).authenticationToken;
// display Azure authentication token to console
console.info("Azure auth token: " + authToken);
alert('success');
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug('error: ' + e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
// Prepare the connection.
client.open("POST", endpoint);
// Send the request.
client.send({"access_token":_opts.token});
};
// set Facebook button style
$.fbButton.style = facebook.BUTTON_STYLE_WIDE;
// open window
$.index.open();
<Alloy>
<Window class="container">
<LoginButton id="fbButton" ns="Alloy.Globals.Facebook"/>
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment