rsanchez
2014-10-30 e2aa9b0177cdfeb9e3f26c511f541c37a03134aa
securis/src/main/resources/static/js/main.js
....@@ -1,6 +1,6 @@
11 (function() {
22 'use strict';
3
-
3
+
44 var m = angular.module('securis', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs', 'i18n' ]);
55
66 m.service('$store', function() {
....@@ -20,75 +20,76 @@
2020 return store.getAll();
2121 }
2222 });
23
-
23
+
2424 m.factory('securisHttpInterceptor', function($q, $location, $store, toaster) {
2525 var isUnauthorizedAccess = function(rejection) {
2626 return rejection.status === 401 /* Unauthorized */;
2727 }
28
- return {
29
- 'request': function(config) {
30
- var token = $store.get('token');
31
- if (token) {
32
- var la = $store.get('last_access');
33
- var now = new Date().getTime();
34
- if (la !== null) {
35
- if (now > (la + 1800000)) { // Session timeout is 1/2 hour
36
- $store.clear();
37
- $location.path('/login');
38
- toaster.pop('warning', 'Session has expired', null, 4000);
39
- } else {
40
- console.debug('Last access recent');
41
- }
42
- }
43
- $store.set('last_access', now);
44
- }
45
- return config || $q.when(config);
46
- },
47
- 'responseError': function(rejection) {
48
- // do something on error
49
- if (isUnauthorizedAccess(rejection)) {
50
- if ($location.path() !== '/login') {
51
- $store.clear();
52
- $location.path('/login');
53
- console.error('There was an unathorized access to url {0}, method: {1}'.$i18n(rejection.config.url, rejection.config.method));
54
- } else {
55
- // console.log('Error on login ...')
56
- }
57
- }
58
- return $q.reject(rejection);
59
- }
60
- };
61
- });
28
+ return {
29
+ 'request': function(config) {
30
+ var token = $store.get('token');
31
+ if (token) {
32
+ var la = $store.get('last_access');
33
+ var now = new Date().getTime();
34
+ if (la !== null) {
35
+ if (now > (la + 1800000)) { // Session timeout is 1/2
36
+ // hour
37
+ $store.clear();
38
+ $location.path('/login');
39
+ toaster.pop('warning', 'Session has expired', null, 4000);
40
+ } else {
41
+ console.debug('Last access recent');
42
+ }
43
+ }
44
+ $store.set('last_access', now);
45
+ }
46
+ return config || $q.when(config);
47
+ },
48
+ 'responseError': function(rejection) {
49
+ // do something on error
50
+ if (isUnauthorizedAccess(rejection)) {
51
+ if ($location.path() !== '/login') {
52
+ $store.clear();
53
+ $location.path('/login');
54
+ console.error('There was an unathorized access to url {0}, method: {1}'.$i18n(rejection.config.url, rejection.config.method));
55
+ } else {
56
+ // console.log('Error on login ...')
57
+ }
58
+ }
59
+ return $q.reject(rejection);
60
+ }
61
+ };
62
+ });
6263
6364 m.config(function($routeProvider, $locationProvider, $httpProvider) {
6465 console.debug('Configuring routes...');
65
- $routeProvider.when('/login', {
66
- templateUrl: 'login.html',
67
- controller: 'LoginCtrl'
68
- });
69
- $routeProvider.when('/licenses', {
70
- templateUrl: 'licenses.html',
71
- controller: 'PackAndLicensesCtrl'
72
- });
73
- $routeProvider.when('/admin', {
74
- templateUrl: 'admin.html',
75
- controller: 'AdminCtrl'
76
- });
77
-
78
- // configure html5 to get links working on jsfiddle
79
- $locationProvider.html5Mode(true);
80
- $httpProvider.interceptors.push('securisHttpInterceptor');
66
+ $routeProvider.when('/login', {
67
+ templateUrl: 'login.html',
68
+ controller: 'LoginCtrl'
8169 });
82
-
70
+ $routeProvider.when('/licenses', {
71
+ templateUrl: 'licenses.html',
72
+ controller: 'PackAndLicensesCtrl'
73
+ });
74
+ $routeProvider.when('/admin', {
75
+ templateUrl: 'admin.html',
76
+ controller: 'AdminCtrl'
77
+ });
78
+
79
+ // configure html5 to get links working on jsfiddle
80
+ $locationProvider.html5Mode(true);
81
+ $httpProvider.interceptors.push('securisHttpInterceptor');
82
+ });
83
+
8384 m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store',
84
- function($scope, $http, $location, $L, $store) {
85
-
85
+ function($scope, $http, $location, $L, $store) {
86
+
8687 $scope.currentRoute = null;
8788 console.log('Current location: ' + $location);
8889 console.log($location);
8990 $location.path('/login');
9091 if ($store.get('token') != null) {
91
-
92
+
9293 $http.get('/check', {
9394 headers: {
9495 'X-SECURIS-TOKEN': $store.get('token')
....@@ -98,18 +99,18 @@
9899 $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token');
99100 var location = $store.get('location') || '/licenses';
100101
101
- $location.path(location);
102
+ $location.path(location);
102103 $store.set('user', data.user);
103104 }
104105 });
105106 }
106
-
107
+
107108 $scope.logout = function() {
108109 $store.remove('user');
109110 $store.remove('token');
110111 $location.path('/login');
111112 }
112
-
113
- }]);
114
-
113
+
114
+ }]);
115
+
115116 })();