{"action":"created","issue":{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1285","labels_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1285/labels{/name}","comments_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1285/comments","events_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1285/events","html_url":"https://github.com/DaftMonk/generator-angular-fullstack/issues/1285","id":105826727,"number":1285,"title":"move authentication check from app.js","user":{"login":"honkskillet","id":2345665,"avatar_url":"https://avatars.githubusercontent.com/u/2345665?v=3","gravatar_id":"","url":"https://api.github.com/users/honkskillet","html_url":"https://github.com/honkskillet","followers_url":"https://api.github.com/users/honkskillet/followers","following_url":"https://api.github.com/users/honkskillet/following{/other_user}","gists_url":"https://api.github.com/users/honkskillet/gists{/gist_id}","starred_url":"https://api.github.com/users/honkskillet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/honkskillet/subscriptions","organizations_url":"https://api.github.com/users/honkskillet/orgs","repos_url":"https://api.github.com/users/honkskillet/repos","events_url":"https://api.github.com/users/honkskillet/events{/privacy}","received_events_url":"https://api.github.com/users/honkskillet/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-10T14:51:52Z","updated_at":"2015-09-10T20:10:54Z","closed_at":null,"body":"I think (and correct me if I'm wrong here) that the client side check of user authentication should be moved out of client/app/app.js .  If you look in that file, currently you will see\r\n```javascript\r\n.run(function ($rootScope, $location, Auth) {\r\n    // Redirect to login if route requires auth and you're not logged in\r\n    $rootScope.$on('$routeChangeStart', function (event, next) {\r\n      Auth.isLoggedInAsync(function(loggedIn) {\r\n        if (next.authenticate && !loggedIn) {\r\n          $location.path('/login');\r\n        }\r\n      });\r\n    });\r\n  });\r\n```\r\nAuth.isLoggedInAsync is, as the name suggests, asynchronous.  Under the hood it calls User.get(), an http request.  So there is a very real chance that you can have the new route loaded before this is resolved.  This could cause two bad outcomes,\r\n1) The route loads before it is confirmed that the user isn't authenticated.  This might result in the route briefly flickering into view before being redirect to /login .\r\n2) The route loads before the user is authenticated.  This mean that if you call Auth.getCurrentUser() while setting up your controller, the current user object will just be a promise.\r\n\r\nMight it not be better to move authentication into the route's $routeProvider.config function?  Currently .config for a route might look like.\r\n```javascript\r\n.config(function ($routeProvider) {\r\n    $routeProvider\r\n      .when('/settings', {\r\n        templateUrl: 'app/account/settings/settings.html',\r\n        controller: 'SettingsCtrl',\r\n        authenticate: true\r\n      });\r\n```\r\nInstead, use `resolve` to make sure the user is authenticated prior to actually loading the route. (Double check this as I am coding it in the browser right now.)\r\n```javascript\r\n.config(function ($routeProvider, Auth) {\r\n    $routeProvider\r\n      .when('/settings', {\r\n        /*...*/\r\n        resolve: {\r\n          authenticatedUser: function($q, $location){\r\n            var deferred =$q.defer();\r\n            Auth.isLoggedInAsync(function(loggedIn) {\r\n              if(loggedIn){\r\n                deferred.resolve(Auth.getCurrentUser() );\r\n              } else  {  \r\n                 deferred.reject(); \r\n                 $location.path('/login');\r\n              }\r\n            });\r\n            return deferred.promise;\r\n          },\r\n        },\r\n      });\r\n```\r\nNow, the route won't load until the user's authentication status has actually been determined.  Better yet would be to add `function($q,$location){...}` to the Auth service, making it about as simple to check authentication this way.  Say, call it Auth.authenticateUserAsync().  Then .config would look something like,\r\n```javascript\r\n.config(function ($routeProvider, Auth) {\r\n    $routeProvider\r\n      .when('/settings', {\r\n        templateUrl: 'app/account/settings/settings.html',\r\n        controller: 'SettingsCtrl',\r\n        resolve: {\r\n          authenticatedUser: function(Auth){\r\n            return Auth.getAuthenticatedUserAsync();\r\n          },\r\n        },\r\n      });\r\n```\r\nSome added bonuses from this approach (besides fixing the issues above),\r\n1) authenticatedUser would be injected into the route controller ready to use.\r\n2) Under the old approach, when you open a config and see `authenticate:true` you have zero idea where this value is actually being used.  I had to hunt through the entire client side code to find that it was being used in app.js.  With the new approach, the logic flow is right there plain to see.\r\n\r\nThoughts?"},"comment":{"url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/comments/139366297","html_url":"https://github.com/DaftMonk/generator-angular-fullstack/issues/1285#issuecomment-139366297","issue_url":"https://api.github.com/repos/DaftMonk/generator-angular-fullstack/issues/1285","id":139366297,"user":{"login":"honkskillet","id":2345665,"avatar_url":"https://avatars.githubusercontent.com/u/2345665?v=3","gravatar_id":"","url":"https://api.github.com/users/honkskillet","html_url":"https://github.com/honkskillet","followers_url":"https://api.github.com/users/honkskillet/followers","following_url":"https://api.github.com/users/honkskillet/following{/other_user}","gists_url":"https://api.github.com/users/honkskillet/gists{/gist_id}","starred_url":"https://api.github.com/users/honkskillet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/honkskillet/subscriptions","organizations_url":"https://api.github.com/users/honkskillet/orgs","repos_url":"https://api.github.com/users/honkskillet/repos","events_url":"https://api.github.com/users/honkskillet/events{/privacy}","received_events_url":"https://api.github.com/users/honkskillet/received_events","type":"User","site_admin":false},"created_at":"2015-09-10T20:10:54Z","updated_at":"2015-09-10T20:10:54Z","body":"I went ahead and made these changes in an active app.  I added a new function to the Auth factory.\r\n```javascropt\r\n     getAuthenticatedUserAsync: function(){\r\n        var deferred =$q.defer();\r\n        console.log(deferred)\r\n        this.isLoggedInAsync(function(loggedIn) {\r\n          if(loggedIn){\r\n            deferred.resolve(currentUser );\r\n          } else  {  \r\n             deferred.reject(); \r\n             $location.path('/login');\r\n          }\r\n        });\r\n        return deferred.promise;\r\n      },\r\n```"}}