Skip to content

Commit f350412

Browse files
committed
refactor(client:auth): move authInterceptor to auth module
1 parent d3d0f56 commit f350412

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

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

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,16 @@
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) { %>
10+
.otherwise('/');<% } %>
1911

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-
})
12+
$locationProvider.html5Mode(true);
13+
})<% if (filters.auth) { %>
4614

4715
.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
4816
// Redirect to login if route requires auth and the user is not logged in
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

33
angular.module('<%= scriptAppName %>.auth', [
4-
'ngCookies'
5-
]);
4+
'ngCookies'<% if (filters.ngroute) { %>,
5+
'ngRoute'<% } if (filters.uirouter) { %>,
6+
'ui.router'<% } %>
7+
])
8+
.config(function($httpProvider) {
9+
$httpProvider.interceptors.push('authInterceptor');
10+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
(function() {
4+
5+
function authInterceptor($rootScope, $q, $cookies<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $injector<% } %>) {
6+
<% if (filters.uirouter) { %>var state;
7+
<% } %>return {
8+
// Add authorization token to headers
9+
request: function(config) {
10+
config.headers = config.headers || {};
11+
if ($cookies.get('token')) {
12+
config.headers.Authorization = 'Bearer ' + $cookies.get('token');
13+
}
14+
return config;
15+
},
16+
17+
// Intercept 401s and redirect you to login
18+
responseError: function(response) {
19+
if (response.status === 401) {
20+
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>(state || (state = $injector.get('$state'))).go('login');<% } %>
21+
// remove any stale tokens
22+
$cookies.remove('token');
23+
return $q.reject(response);
24+
}
25+
else {
26+
return $q.reject(response);
27+
}
28+
}
29+
};
30+
}
31+
32+
angular.module('<%= scriptAppName %>.auth')
33+
.factory('authInterceptor', authInterceptor);
34+
35+
})();

test/test-file-creation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ describe('angular-fullstack generator', function () {
269269
'client/app/admin/admin.controller.' + script,
270270
'client/components/auth/auth.module.' + script,
271271
'client/components/auth/auth.service.' + script,
272+
'client/components/auth/interceptor.service.' + script,
272273
'client/components/auth/user.service.' + script,
273274
'client/components/mongoose-error/mongoose-error.directive.' + script,
274275
'server/api/user/index.js',

0 commit comments

Comments
 (0)