I was able to figure it out. I created a new service to handle all the code for mobile services. Since the methods from the client are asynchronous, I use callbacks in the methods. I also use cookie storage to save the user credential object (currentUser) and pull it out again when necessary. currentUser seems to lose the user credential object between calls, so I save it in a cookie and click on it when it lost it.
'use strict'; angular.module('{appName}') .factory('AzureMobileClient', ['$cookieStore', function ($cookieStore) { var azureMobileClient = {}; azureMobileClient.isLoggedIn = false; azureMobileClient.azureError = ""; azureMobileClient.azureMSC = new WindowsAzure.MobileServiceClient('{app URL from Azure}', '{app key from Azure}'); azureMobileClient.login = function(callback, socialMediaService) { azureMobileClient.azureMSC.login(socialMediaService).then(function(user) { azureMobileClient.isLoggedIn = user != null; $cookieStore.put("azureUser", user); callback(azureMobileClient.isLoggedIn); } , function(error){ alert(error); azureMobileClient.azureError = error; }); }; azureMobileClient.logout = function() { azureMobileClient.getUser(); azureMobileClient.azureMSC.logout(); $cookieStore.remove("azureUser"); }; azureMobileClient.getStuff = function(callback) { azureMobileClient.getUser(); var stuffTable = azureMobileClient.azureMSC.getTable("stuff"); stuffTable.read().then(function(items) { callback(items); }); }; azureMobileClient.addStuff = function(scope) { azureMobileClient.getUser(); var stuffTable = azureMobileClient.azureMSC.getTable("stuff"); stuffTable.insert({ stuffname: scope.stuffname }); }; azureMobileClient.getUser = function() { if (azureMobileClient.azureMSC.currentUser === null) { azureMobileClient.azureMSC.currentUser = $cookieStore.get("azureUser"); } }; return azureMobileClient; }]);
In the controller that does the login and logout, I do this:
'use strict'; angular.module('{appName}') .controller('MainCtrl', function ($scope, $window, AzureMobileClient) { $scope.authenticate = function (socialService) { AzureMobileClient.login(function(isLoggedIn) { if (isLoggedIn) { $window.location.href = "/#/play"; } }, socialService); }; $scope.signOut = function() { AzureMobileClient.logout(); } });
The view of the I / O controller is as follows:
<button ng-click="signOut()">Sign Out</button> <div class="span4"> <img src="images/facebooklogin.png" ng-click="authenticate('Facebook')" /> <img src="images/twitterlogin.png" ng-click="authenticate('Twitter')" /> <img src="images/googlelogin.png" ng-click="authenticate('Google')" /> </div>
And finally, in the controller that receives the data, I do this:
'use strict'; angular.module('{appName}') .controller('StuffCtrl', ['$scope', '$window', 'AzureMobileClient', function ($scope, $window, AzureMobileClient) { AzureMobileClient.getStuff(function(items) { if (items.length == 0) { $window.location.href = "/#/stuff/new"; } else { $scope.$apply($scope.items = items); } }); }]);