Roberto Sánchez
2014-01-17 04afd774aecc70dca37559fdd8b9a716829c18cd
securis/src/main/resources/static/js/main.js
....@@ -2,35 +2,84 @@
22 'use strict';
33
44 var m = angular.module('securis', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs', 'i18n' ]);
5
+
6
+ m.service('$store', function() {
7
+ this.get = function(key, defaultValue) {
8
+ return store.get(key) || defaultValue;
9
+ }
10
+ this.set = this.put = function(key, value) {
11
+ store.set(key, value);
12
+ }
13
+ this.remove = this.delete = function(key) {
14
+ return store.remove(key);
15
+ }
16
+ this.clear = this.clearAll = function() {
17
+ store.clear();
18
+ }
19
+ this.getAll = function() {
20
+ return store.getAll();
21
+ }
22
+ });
523
6
- m.config(function($routeProvider, $locationProvider) {
24
+ m.factory('securisHttpInterceptor', function($q, $location, $store) {
25
+ var isUnauthorizedAccess = function(rejection) {
26
+ console.log('rejection -----------------------');
27
+ console.log(rejection);
28
+ return rejection.status === 401 /* Unauthorized */;
29
+ }
30
+ return {
31
+
32
+ 'responseError': function(rejection) {
33
+ // do something on error
34
+ if (isUnauthorizedAccess(rejection)) {
35
+ if ($location.path() !== '/login') {
36
+ $store.clear();
37
+ $location.path('/login');
38
+ console.error('There was an unathorized access to url {0}, method: {1}'.$i18n(rejection.config.url, rejection.config.method));
39
+ } else {
40
+ // console.log('Error on login ...')
41
+ }
42
+ }
43
+ return $q.reject(rejection);
44
+ }
45
+ };
46
+ });
47
+
48
+ m.config(function($routeProvider, $locationProvider, $httpProvider) {
749 console.log('Configuring routes...');
850 $routeProvider.when('/login', {
951 templateUrl: 'login.html',
10
- controller: 'LoginCtrl',
11
- controllerAs: 'login'
52
+ controller: 'LoginCtrl'
1253 });
1354 $routeProvider.when('/licenses', {
1455 templateUrl: 'licenses.html',
15
- controller: 'LicensesCtrl',
16
- controllerAs: 'licenses'
56
+ controller: 'LicensesCtrl'
1757 });
1858 $routeProvider.when('/admin', {
1959 templateUrl: 'admin.html',
20
- controller: 'AdminCtrl',
21
- controllerAs: 'admin'
60
+ controller: 'AdminCtrl'
2261 });
2362
2463 // configure html5 to get links working on jsfiddle
2564 $locationProvider.html5Mode(true);
65
+ $httpProvider.interceptors.push('securisHttpInterceptor');
2666 });
27
- m.controller('MainCtrl', ['$scope', '$location', '$L',
28
- function($scope, $location, $L) {
29
- console.log('Moving to login...');
30
- console.log('Test 1 lang: ' + 'Hello {0}!! this is {1}'.$i18n('World', 'cool'));
31
- console.log('Test 2 lang: ' + $L.get('Hello Pepe!!'));
32
- $location.path('/login');
33
- }]);
67
+ m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store',
68
+ function($scope, $http, $location, $L, $store) {
3469
70
+ if ($store.get('token') != null) {
71
+ $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token');
72
+ $location.path('/licenses');
73
+ } else {
74
+ $location.path('/login');
75
+ }
76
+
77
+ $scope.logout = function() {
78
+ $store.remove('user');
79
+ $store.remove('token');
80
+ $location.path('/login');
81
+ }
82
+
83
+ }]);
3584
3685 })();