| .. | .. |
|---|
| 1 | 1 | (function() { |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | | - |
|---|
| 3 | + |
|---|
| 4 | 4 | var m = angular.module('securis', [ 'ngRoute', 'ngResource', 'toaster', 'localytics.directives', 'catalogs', 'i18n' ]); |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | m.service('$store', function() { |
|---|
| .. | .. |
|---|
| 20 | 20 | return store.getAll(); |
|---|
| 21 | 21 | } |
|---|
| 22 | 22 | }); |
|---|
| 23 | | - |
|---|
| 23 | + |
|---|
| 24 | 24 | m.factory('securisHttpInterceptor', function($q, $location, $store, toaster) { |
|---|
| 25 | 25 | var isUnauthorizedAccess = function(rejection) { |
|---|
| 26 | 26 | return rejection.status === 401 /* Unauthorized */; |
|---|
| 27 | 27 | } |
|---|
| 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 | + }); |
|---|
| 62 | 63 | |
|---|
| 63 | 64 | m.config(function($routeProvider, $locationProvider, $httpProvider) { |
|---|
| 64 | 65 | 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' |
|---|
| 81 | 69 | }); |
|---|
| 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 | + |
|---|
| 83 | 84 | m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store', |
|---|
| 84 | | - function($scope, $http, $location, $L, $store) { |
|---|
| 85 | | - |
|---|
| 85 | + function($scope, $http, $location, $L, $store) { |
|---|
| 86 | + |
|---|
| 86 | 87 | $scope.currentRoute = null; |
|---|
| 87 | 88 | console.log('Current location: ' + $location); |
|---|
| 88 | 89 | console.log($location); |
|---|
| 89 | 90 | $location.path('/login'); |
|---|
| 90 | 91 | if ($store.get('token') != null) { |
|---|
| 91 | | - |
|---|
| 92 | + |
|---|
| 92 | 93 | $http.get('/check', { |
|---|
| 93 | 94 | headers: { |
|---|
| 94 | 95 | 'X-SECURIS-TOKEN': $store.get('token') |
|---|
| .. | .. |
|---|
| 98 | 99 | $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token'); |
|---|
| 99 | 100 | var location = $store.get('location') || '/licenses'; |
|---|
| 100 | 101 | |
|---|
| 101 | | - $location.path(location); |
|---|
| 102 | + $location.path(location); |
|---|
| 102 | 103 | $store.set('user', data.user); |
|---|
| 103 | 104 | } |
|---|
| 104 | 105 | }); |
|---|
| 105 | 106 | } |
|---|
| 106 | | - |
|---|
| 107 | + |
|---|
| 107 | 108 | $scope.logout = function() { |
|---|
| 108 | 109 | $store.remove('user'); |
|---|
| 109 | 110 | $store.remove('token'); |
|---|
| 110 | 111 | $location.path('/login'); |
|---|
| 111 | 112 | } |
|---|
| 112 | | - |
|---|
| 113 | | - }]); |
|---|
| 114 | | - |
|---|
| 113 | + |
|---|
| 114 | + }]); |
|---|
| 115 | + |
|---|
| 115 | 116 | })(); |
|---|