Skip to content

Commit

Permalink
fix(app): wait for currentUser to resolve before checking if logged i…
Browse files Browse the repository at this point in the history
…n on route changes

fixes #306, #294
  • Loading branch information
DaftMonk committed Jul 13, 2014
1 parent 64f33f0 commit 6d6090d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/templates/client/app/app(coffee).coffee
Expand Up @@ -35,5 +35,6 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])
.run (($rootScope, $location, Auth) ->
# Redirect to login if route requires auth and you're not logged in
$rootScope.$on <% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, (event, next) ->
$location.path '/login' if next.authenticate and not Auth.isLoggedIn()
Auth.isLoggedInAsync (loggedIn) ->
$location.path "/login" if next.authenticate and not loggedIn
)<% } %>
8 changes: 5 additions & 3 deletions app/templates/client/app/app(js).js
Expand Up @@ -46,8 +46,10 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])
.run(function ($rootScope, $location, Auth) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on(<% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, function (event, next) {
if (next.authenticate && !Auth.isLoggedIn()) {
$location.path('/login');
}
Auth.isLoggedInAsync(function(loggedIn) {
if (next.authenticate && !loggedIn) {
$location.path('/login');
}
});
});
})<% } %>;
Expand Up @@ -92,14 +92,31 @@ angular.module('<%= scriptAppName %>').factory 'Auth', ($location, $rootScope, $


###
Check if a user is logged in
Check if a user is logged in synchronously
@return {Boolean}
###
isLoggedIn: ->
currentUser.hasOwnProperty 'role'


###
Waits for currentUser to resolve before checking if user is logged in
###
isLoggedInAsync: (cb) ->
if currentUser.hasOwnProperty('$promise')
currentUser.$promise.then(->
cb true
return
).catch ->
cb false
return

else if currentUser.hasOwnProperty('role')
cb true
else
cb false

###
Check if a user is an admin
Expand Down
17 changes: 17 additions & 0 deletions app/templates/client/components/auth(auth)/auth.service(js).js
Expand Up @@ -110,6 +110,23 @@ angular.module('<%= scriptAppName %>')
return currentUser.hasOwnProperty('role');
},

/**
* Waits for currentUser to resolve before checking if user is logged in
*/
isLoggedInAsync: function(cb) {
if(currentUser.hasOwnProperty('$promise')) {
currentUser.$promise.then(function() {
cb(true);
}).catch(function() {
cb(false);
});
} else if(currentUser.hasOwnProperty('role')) {
cb(true);
} else {
cb(false);
}
},

/**
* Check if a user is an admin
*
Expand Down

1 comment on commit 6d6090d

@f2net
Copy link

@f2net f2net commented on 6d6090d Jul 14, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you: this way the promise is correctly resolved before the checking.
I think that it would be useful to have also a "role" property on the route. Otherwise a logged in user (not admin) can see the admin interface (even if he cannot see any data from the API). This is because the API in this case return a 403 error instead of a 401.

Thank you,
Matteo

Please sign in to comment.