|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | 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) { %> |
5 | 5 | $routeProvider |
6 | 6 | .otherwise({ |
7 | 7 | 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) { %> |
13 | 9 | $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('/');<% } %> |
46 | 11 |
|
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 | + }); |
0 commit comments