Roberto Sánchez
2014-01-17 441c660af706fd3c6d0e06b36b8f25a808fcdf5f
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(function() {
   'use strict';
   
   var m = angular.module('securis', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs', 'i18n' ]);
   m.service('$store', function() {
       this.get = function(key, defaultValue) {
           return store.get(key) || defaultValue;
       }
       this.set = this.put = function(key, value) {
           store.set(key, value);
       }
       this.remove = this.delete = function(key) {
           return store.remove(key);
       }
       this.clear = this.clearAll = function() {
           store.clear();
       }
       this.getAll = function() {
           return store.getAll();
       }
   });
   
   m.factory('securisHttpInterceptor', function($q, $location, $store) {
       var isUnauthorizedAccess = function(rejection) {
           console.log('rejection -----------------------');
           console.log(rejection);
           return rejection.status === 401 /* Unauthorized */;
       } 
         return {
          'responseError': function(rejection) {
             // do something on error
             if (isUnauthorizedAccess(rejection)) {
               if ($location.path() !== '/login') {
                   $store.clear();
                   $location.path('/login');
                   console.error('There was an unathorized access to url {0}, method: {1}'.$i18n(rejection.config.url, rejection.config.method));
               } else {
                   // console.log('Error on login ...')
               }
             }
             return $q.reject(rejection);
           }
         };
       });
   m.config(function($routeProvider, $locationProvider, $httpProvider) {
       console.log('Configuring routes...');
           $routeProvider.when('/login', {
             templateUrl: 'login.html',
             controller: 'LoginCtrl'
           });
           $routeProvider.when('/licenses', {
                 templateUrl: 'licenses.html',
                 controller: 'LicensesCtrl'
           });
           $routeProvider.when('/admin', {
                 templateUrl: 'admin.html',
                 controller: 'AdminCtrl'
           });
        
           // configure html5 to get links working on jsfiddle
           $locationProvider.html5Mode(true);
           $httpProvider.interceptors.push('securisHttpInterceptor');
       });    
   m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store',
                                function($scope, $http, $location, $L, $store) {
       
       $location.path('/login');
       if ($store.get('token') != null) {
           
           $http.get('/check', {
               headers: {
                   'X-SECURIS-TOKEN': $store.get('token')
               }
           }).success(function(data) {
               if (data.valid) {
                   $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token'); 
                   $location.path('/licenses');
                   $store.set('user', data.user);
               }
           });
       }
       
       $scope.logout = function() {
           $store.remove('user');
           $store.remove('token');
           $location.path('/login');
       }
       
       }]);    
   
})();