Skip to content

Commit f222cfb

Browse files
committed
feat(client:auth): implement router auth via router.decorator
Changes: * move remaining auth code from app.js to auth module * the router param `authenticate` now accepts a string as a role requirement * admin route now uses role based auth
1 parent 533f20e commit f222cfb

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

app/templates/client/app/admin(auth)/admin(js).js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ angular.module('<%= scriptAppName %>')
55
$routeProvider
66
.when('/admin', {
77
templateUrl: 'app/admin/admin.html',
8-
controller: 'AdminCtrl'
8+
controller: 'AdminCtrl',
9+
authenticate: 'admin'
910
});
1011
});<% } %><% if (filters.uirouter) { %>.config(function($stateProvider) {
1112
$stateProvider
1213
.state('admin', {
1314
url: '/admin',
1415
templateUrl: 'app/admin/admin.html',
15-
controller: 'AdminCtrl'
16+
controller: 'AdminCtrl',
17+
authenticate: 'admin'
1618
});
1719
});<% } %>

app/templates/client/app/app(js).js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,4 @@ angular.module('<%= scriptAppName %>', [<%- angularModules %>])
1010
.otherwise('/');<% } %>
1111

1212
$locationProvider.html5Mode(true);
13-
})<% if (filters.auth) { %>
14-
15-
.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
16-
// Redirect to login if route requires auth and the user is not logged in
17-
$rootScope.$on(<% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) {
18-
if (next.authenticate) {
19-
Auth.isLoggedIn(function(loggedIn) {
20-
if (!loggedIn) {
21-
event.preventDefault();
22-
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>$state.go('login');<% } %>
23-
}
24-
});
25-
}
26-
});
27-
})<% } %>;
13+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
(function() {
4+
5+
function routerDecorator(<%= filters.uirouter ? '$stateProvider' : '$provide' %>) {
6+
var authDecorator = function(<%= filters.uirouter ? 'state' : 'route' %>) {
7+
var auth = <%= filters.uirouter ? 'state' : 'route' %>.authenticate;
8+
if (auth) {
9+
<%= filters.uirouter ? 'state' : 'route' %>.resolve = <%= filters.uirouter ? 'state' : 'route' %>.resolve || {};
10+
<%= filters.uirouter ? 'state' : 'route' %>.resolve.user = function(<%= filters.uirouter ? '$state' : '$location' %>, $q, Auth) {
11+
return Auth.getCurrentUser(true)
12+
.then(function(user) {
13+
if ((typeof auth !== 'string' && user._id) ||
14+
(typeof auth === 'string' && Auth.hasRole(auth))) {
15+
return user;
16+
}<% if (filters.ngroute) { %>
17+
$location.path((user._id) ? '/' : '/login');<% } if (filters.uirouter) { %>
18+
$state.go((user._id) ? 'main' : 'login');<% } %>
19+
return $q.reject('not authorized');
20+
});
21+
};
22+
}
23+
};<% if (filters.ngroute) { %>
24+
25+
$provide.decorator('$route', function($delegate) {
26+
for (var r in $delegate.routes) {
27+
authDecorator($delegate.routes[r]);
28+
}
29+
return $delegate;
30+
});<% } if (filters.uirouter) { %>
31+
32+
$stateProvider.decorator('authenticate', function(state) {
33+
authDecorator(state);
34+
return state.authenticate;
35+
});<% } %>
36+
}
37+
38+
angular.module('<%= scriptAppName %>.auth')
39+
.config(routerDecorator);
40+
41+
})();

test/test-file-creation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ describe('angular-fullstack generator', function () {
270270
'client/components/auth/auth.module.' + script,
271271
'client/components/auth/auth.service.' + script,
272272
'client/components/auth/interceptor.service.' + script,
273+
'client/components/auth/router.decorator.' + script,
273274
'client/components/auth/user.service.' + script,
274275
'client/components/mongoose-error/mongoose-error.directive.' + script,
275276
'server/api/user/index.js',

0 commit comments

Comments
 (0)