Gracefully handle login failures.
authorCameron Ball <cameron@getapproved.com.au>
Tue, 30 Dec 2014 02:33:31 +0000 (10:33 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Tue, 30 Dec 2014 02:33:31 +0000 (10:33 +0800)
app/components/hello/hello-service.js
app/components/userMenu/userMenu-directive.js

index e9bc504..5fd5f39 100644 (file)
@@ -19,7 +19,7 @@ factory("HelloService", ['rockEndpoint', '$http', '$location', '$q', function(ro
     {
         var deferred = $q.defer();
         
-        if(! this.isLoggedIn())
+        if(!this.isLoggedIn())
         {
             deferred.reject();
             $location.path('/');
@@ -46,30 +46,40 @@ factory("HelloService", ['rockEndpoint', '$http', '$location', '$q', function(ro
     hello.facebookLogin = function()
     {
         // get the short term token
-        hello('facebook').login( {scope: "hometown"} );
+        hello('facebook').login( {scope: "hometown"} ).then(function() {
+            var facebookObj = hello.utils.store('facebook');
+
+            // send to 
+            $http({
+                url: rockEndpoint + "user/auth",
+                method: "GET",
+                params: {token: facebookObj.access_token}
+            }).
+            success(function (data)
+            {
+                /*
+                 * It is no good to use auth.login because that comes back almost
+                 * instantly due to facebook. We rely on rock.de for things like
+                 * the user name, so we need to wait for it to be ready, which happens
+                 * here.
+                 */
+                hello.emit('auth.login.userReady');
+                hello.setAccessToken(data.token, data.expires);
+            })
+            .error(function(data)
+            {
+                hello.emit('auth.login.fail');
+            });
+        },
+        function()
+        {
+            hello.emit('auth.login.fail');
+        });
     };
     
     hello.on('auth.login', function()
     {
-        var facebookObj = hello.utils.store('facebook');
 
-        // send to 
-        $http({
-            url: rockEndpoint + "user/auth",
-            method: "GET",
-            params: {token: facebookObj.access_token}
-        }).
-        success(function (data)
-        {
-            /*
-             * It is no good to use auth.login because that comes back almost
-             * instantly due to facebook. We rely on rock.de for things like
-             * the user name, so we need to wait for it to be ready, which happens
-             * here.
-             */
-            hello.emit('auth.login.userReady');
-            hello.setAccessToken(data.token, data.expires);
-        });
     });
         
     hello.getFacebookId = function()
index 138c1fc..a5dffb2 100644 (file)
@@ -33,6 +33,12 @@ directive('userMenu', ['HelloService', 'UserService', '$modal', function(HelloSe
                 });
             });
             
+            HelloService.on('auth.login.fail', function()
+            {
+                scope.menuReady = true;
+                scope.loggedIn = false;
+            });
+            
             HelloService.on('auth.logout', function()
             {
                 UserService.flushCache();