-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapp(js).js
More file actions
57 lines (51 loc) · 1.99 KB
/
app(js).js
File metadata and controls
57 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
angular.module('<%= scriptAppName %>', [<%= angularModules %>])
<% if(filters.ngroute) { %>.config(function ($routeProvider, $locationProvider<% if(filters.auth) { %>, $httpProvider<% } %>) {
$routeProvider
.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);<% if(filters.auth) { %>
$httpProvider.interceptors.push('authInterceptor');<% } %>
})<% } %><% if(filters.uirouter) { %>.config(function ($stateProvider, $urlRouterProvider, $locationProvider<% if(filters.auth) { %>, $httpProvider<% } %>) {
$urlRouterProvider
.otherwise('/');
$locationProvider.html5Mode(true);<% if(filters.auth) { %>
$httpProvider.interceptors.push('authInterceptor');<% } %>
})<% } %><% if(filters.auth) { %>
.factory('authInterceptor', function ($rootScope, $q, $localStorage, $cookies, $location) {
return {
// Add authorization token to headers
request: function (config) {
config.headers = config.headers || {};
var token = $localStorage.token||$cookies.token;
if (token) {
config.headers.Authorization = 'Bearer ' + token;
}
return config;
},
// Intercept 401s and redirect you to login
responseError: function(response) {
if(response.status === 401) {
$location.path('/login');
// remove any stale tokens
delete $localStorage.token;
delete $cookies.token;
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
};
})
.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) {
Auth.isLoggedInAsync(function(loggedIn) {
if (next.authenticate && !loggedIn) {
$location.path('/login');
}
});
});
})<% } %>;