Skip to content

Commit eca6d2d

Browse files
committed
Merge pull request #1309 from kingcody/feature/auth-module
Feature: auth module
2 parents cb0e4ab + abcb51a commit eca6d2d

13 files changed

+378
-231
lines changed

Diff for: app/generator.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ export default class Generator extends Base {
380380
if(this.filters.socketio) angModules.push("'btford.socket-io'");
381381
if(this.filters.uirouter) angModules.push("'ui.router'");
382382
if(this.filters.uibootstrap) angModules.push("'ui.bootstrap'");
383-
if(this.filters.auth) angModules.push("'validation.match'");
383+
if(this.filters.auth) {
384+
angModules.unshift(`'${this.scriptAppName}.auth'`);
385+
angModules.push("'validation.match'");
386+
}
384387

385388
this.angularModules = '\n ' + angModules.join(',\n ') +'\n';
386389
}

Diff for: app/templates/Gruntfile.js

+7
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ module.exports = function (grunt) {
658658
filePath = filePath.replace('/.tmp/', '');
659659
return '<script src="' + filePath + '"></script>';
660660
},
661+
sort: function(a, b) {
662+
var module = /\.module\.js$/;
663+
var aMod = module.test(a);
664+
var bMod = module.test(b);
665+
// inject *.module.js first
666+
return (aMod === bMod) ? 0 : (aMod ? -1 : 1);
667+
},
661668
starttag: '<!-- injector:js -->',
662669
endtag: '<!-- endinjector -->'
663670
},

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

+4-2
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
});<% } %>

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

+5-51
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
11
'use strict';
22

33
angular.module('<%= scriptAppName %>', [<%- angularModules %>])
4-
<% if (filters.ngroute) { %>.config(function($routeProvider, $locationProvider<% if (filters.auth) { %>, $httpProvider<% } %>) {
4+
.config(function(<% if (filters.ngroute) { %>$routeProvider<% } if (filters.uirouter) { %>$urlRouterProvider<% } %>, $locationProvider) {<% if (filters.ngroute) { %>
55
$routeProvider
66
.otherwise({
77
redirectTo: '/'
8-
});
9-
10-
$locationProvider.html5Mode(true);<% if (filters.auth) { %>
11-
$httpProvider.interceptors.push('authInterceptor');<% } %>
12-
})<% } if (filters.uirouter) { %>.config(function($stateProvider, $urlRouterProvider, $locationProvider<% if (filters.auth) { %>, $httpProvider<% } %>) {
8+
});<% } if (filters.uirouter) { %>
139
$urlRouterProvider
14-
.otherwise('/');
15-
16-
$locationProvider.html5Mode(true);<% if (filters.auth) { %>
17-
$httpProvider.interceptors.push('authInterceptor');<% } %>
18-
})<% } if (filters.auth) { %>
19-
20-
.factory('authInterceptor', function($rootScope, $q, $cookies<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $injector<% } %>) {
21-
<% if (filters.uirouter) { %>var state;
22-
<% } %>return {
23-
// Add authorization token to headers
24-
request: function(config) {
25-
config.headers = config.headers || {};
26-
if ($cookies.get('token')) {
27-
config.headers.Authorization = 'Bearer ' + $cookies.get('token');
28-
}
29-
return config;
30-
},
31-
32-
// Intercept 401s and redirect you to login
33-
responseError: function(response) {
34-
if (response.status === 401) {
35-
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>(state || (state = $injector.get('$state'))).go('login');<% } %>
36-
// remove any stale tokens
37-
$cookies.remove('token');
38-
return $q.reject(response);
39-
}
40-
else {
41-
return $q.reject(response);
42-
}
43-
}
44-
};
45-
})
10+
.otherwise('/');<% } %>
4611

47-
.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
48-
// Redirect to login if route requires auth and the user is not logged in
49-
$rootScope.$on(<% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) {
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-
}
58-
});
59-
})<% } %>;
12+
$locationProvider.html5Mode(true);
13+
});

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
23
(function() {
34

45
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
@@ -8,8 +9,8 @@ function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %
89
$http.get('/api/things').then(function(response) {
910
self.awesomeThings = response.data;<% if (filters.socketio) { %>
1011
socket.syncUpdates('thing', self.awesomeThings);<% } %>
11-
});
12-
<% if (filters.models) { %>
12+
});<% if (filters.models) { %>
13+
1314
this.addThing = function() {
1415
if (self.newThing === '') {
1516
return;
@@ -20,7 +21,7 @@ function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %
2021

2122
this.deleteThing = function(thing) {
2223
$http.delete('/api/things/' + thing._id);
23-
};<% } %><% if (filters.socketio) { %>
24+
};<% } if (filters.socketio) { %>
2425

2526
$scope.$on('$destroy', function() {
2627
socket.unsyncUpdates('thing');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
angular.module('<%= scriptAppName %>.auth', [
4+
'<%= scriptAppName %>.constants',
5+
'<%= scriptAppName %>.util',
6+
'ngCookies'<% if (filters.ngroute) { %>,
7+
'ngRoute'<% } if (filters.uirouter) { %>,
8+
'ui.router'<% } %>
9+
])
10+
.config(function($httpProvider) {
11+
$httpProvider.interceptors.push('authInterceptor');
12+
});

0 commit comments

Comments
 (0)