| .. | .. |
|---|
| 2 | 2 | 'use strict'; |
|---|
| 3 | 3 | |
|---|
| 4 | 4 | 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 | + }); |
|---|
| 5 | 23 | |
|---|
| 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) { |
|---|
| 7 | 49 | console.log('Configuring routes...'); |
|---|
| 8 | 50 | $routeProvider.when('/login', { |
|---|
| 9 | 51 | templateUrl: 'login.html', |
|---|
| 10 | | - controller: 'LoginCtrl', |
|---|
| 11 | | - controllerAs: 'login' |
|---|
| 52 | + controller: 'LoginCtrl' |
|---|
| 12 | 53 | }); |
|---|
| 13 | 54 | $routeProvider.when('/licenses', { |
|---|
| 14 | 55 | templateUrl: 'licenses.html', |
|---|
| 15 | | - controller: 'LicensesCtrl', |
|---|
| 16 | | - controllerAs: 'licenses' |
|---|
| 56 | + controller: 'LicensesCtrl' |
|---|
| 17 | 57 | }); |
|---|
| 18 | 58 | $routeProvider.when('/admin', { |
|---|
| 19 | 59 | templateUrl: 'admin.html', |
|---|
| 20 | | - controller: 'AdminCtrl', |
|---|
| 21 | | - controllerAs: 'admin' |
|---|
| 60 | + controller: 'AdminCtrl' |
|---|
| 22 | 61 | }); |
|---|
| 23 | 62 | |
|---|
| 24 | 63 | // configure html5 to get links working on jsfiddle |
|---|
| 25 | 64 | $locationProvider.html5Mode(true); |
|---|
| 65 | + $httpProvider.interceptors.push('securisHttpInterceptor'); |
|---|
| 26 | 66 | }); |
|---|
| 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) { |
|---|
| 34 | 69 | |
|---|
| 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 | + }]); |
|---|
| 35 | 84 | |
|---|
| 36 | 85 | })(); |
|---|