From 04afd774aecc70dca37559fdd8b9a716829c18cd Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 17 Jan 2014 12:27:16 +0000
Subject: [PATCH] #396 feature - Added LocalStorage support and http interceptor for unauthorized access
---
securis/src/main/resources/static/js/main.js | 77 +++++++++++++++++++++++++++++++-------
1 files changed, 63 insertions(+), 14 deletions(-)
diff --git a/securis/src/main/resources/static/js/main.js b/securis/src/main/resources/static/js/main.js
index 8428bc3..14521b6 100644
--- a/securis/src/main/resources/static/js/main.js
+++ b/securis/src/main/resources/static/js/main.js
@@ -2,35 +2,84 @@
'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.config(function($routeProvider, $locationProvider) {
+ 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',
- controllerAs: 'login'
+ controller: 'LoginCtrl'
});
$routeProvider.when('/licenses', {
templateUrl: 'licenses.html',
- controller: 'LicensesCtrl',
- controllerAs: 'licenses'
+ controller: 'LicensesCtrl'
});
$routeProvider.when('/admin', {
templateUrl: 'admin.html',
- controller: 'AdminCtrl',
- controllerAs: 'admin'
+ controller: 'AdminCtrl'
});
// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
+ $httpProvider.interceptors.push('securisHttpInterceptor');
});
- m.controller('MainCtrl', ['$scope', '$location', '$L',
- function($scope, $location, $L) {
- console.log('Moving to login...');
- console.log('Test 1 lang: ' + 'Hello {0}!! this is {1}'.$i18n('World', 'cool'));
- console.log('Test 2 lang: ' + $L.get('Hello Pepe!!'));
- $location.path('/login');
- }]);
+ m.controller('MainCtrl', ['$scope', '$http', '$location', '$L', '$store',
+ function($scope, $http, $location, $L, $store) {
+ if ($store.get('token') != null) {
+ $http.defaults.headers.common['X-SECURIS-TOKEN'] = $store.get('token');
+ $location.path('/licenses');
+ } else {
+ $location.path('/login');
+ }
+
+ $scope.logout = function() {
+ $store.remove('user');
+ $store.remove('token');
+ $location.path('/login');
+ }
+
+ }]);
})();
\ No newline at end of file
--
Gitblit v1.3.2