Skip to content

Commit d636ba2

Browse files
committed
Merge pull request #1171 from kingcody/fix/route-auth-logic
fix(app): only check loggedIn when required
2 parents 29875d6 + 6a07a81 commit d636ba2

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

Diff for: app/templates/client/app/app(coffee).coffee

+6-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ angular.module '<%= scriptAppName %>', [<%= angularModules %>]
3333
$q.reject response
3434

3535
.run ($rootScope<% if (filters.ngroute) { %>, $location<% } %><% if (filters.uirouter) { %>, $state<% } %>, Auth) ->
36-
# Redirect to login if route requires auth and you're not logged in
36+
# Redirect to login if route requires auth and the user is not logged in
3737
$rootScope.$on <% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, (event, next) ->
38-
Auth.isLoggedIn (loggedIn) ->
39-
event.preventDefault()
40-
<% if (filters.ngroute) { %>$location.path '/login'<% } %><% if (filters.uirouter) { %>$state.go 'login'<% } %> if next.authenticate and not loggedIn
41-
<% } %>
38+
if next.authenticate
39+
Auth.isLoggedIn (loggedIn) ->
40+
if !loggedIn
41+
event.preventDefault()
42+
<% if (filters.ngroute) { %>$location.path '/login'<% } %><% if (filters.uirouter) { %>$state.go 'login'<% }} %>

Diff for: app/templates/client/app/app(js).js

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])
4545
})
4646

4747
.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
48-
// Redirect to login if route requires auth and you're not logged in
48+
// Redirect to login if route requires auth and the user is not logged in
4949
$rootScope.$on(<% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) {
50-
Auth.isLoggedIn(function(loggedIn) {
51-
if (next.authenticate && !loggedIn) {
52-
event.preventDefault();
53-
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>$state.go('login');<% } %>
54-
}
55-
});
50+
if (next.authenticate) {
51+
Auth.isLoggedIn(function(loggedIn) {
52+
if (!loggedIn) {
53+
event.preventDefault();
54+
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>$state.go('login');<% } %>
55+
}
56+
});
57+
}
5658
});
5759
})<% } %>;

0 commit comments

Comments
 (0)