(function () { var app = angular.module('helloApp', ['ui.router','ngHello','angular-storage']); app.config( function($stateProvider, $urlRouterProvider, helloProvider) { helloProvider.init({ facebook: 'YOUR_FACEBOOK_APP_ID' }, {redirect_uri: 'redirect.html'}); $urlRouterProvider.otherwise("/"); $stateProvider.state('login', { url: "/", templateUrl: "home.html", controller: "LoginController" }).state('test', { url: "/test", templateUrl: "test.html", controller: "TestController" }).state('home', { url: "/login", template: "home.html" }); }); app.run(function($rootScope, store, hello, $timeout) { $rootScope.user = null; $rootScope.$on('$locationChangeStart', function() { var user = store.get('user'); if (user) { $rootScope.user = user; } else { self.location.href = "#/"; } }); }); app.controller('TestController', function ($scope, $rootScope, hello, $timeout) { $scope.whoami = ""; if( $rootScope.user === null ){ self.location.href = "#/"; }else{ $scope.whoami = "Hey " + $rootScope.user.name; } }); app.controller('LoginController', function ($scope, $rootScope, hello, store, $timeout) { $scope.whoami = ""; if( $rootScope.user !== null ){ $scope.whoami = "Hey " + $rootScope.user.name; } $scope.login = function () { hello('facebook').login(); }; $scope.logout = function () { hello('facebook').logout().then(function() { $timeout(function() { store.remove('user'); $scope.whoami = ""; $rootScope.user = {}; }); }, function(e) { alert('Signed out error: ' + e.error.message); }); }; hello.on("auth.login", function (auth) { hello(auth.network).api('/me').then(function( user ) { var displayName = user.name.split(' '); user.first_name = displayName[0].toLowerCase(); user.last_name = displayName[displayName.length - 1].toLowerCase(); user.fbid = user.id; $scope.user = user; $timeout(function() { $rootScope.user = user; store.set('user', user ); $scope.whoami = "Hey " + $rootScope.user.name; }); }); }); }); })();