44 lines
2.3 KiB
JavaScript
44 lines
2.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Created by igor.samulenko on 5/28/2014.
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
angular.module('securityModule').config(['$httpProvider', function ($httpProvider) {
|
|
$httpProvider.interceptors.push([
|
|
'$injector', function ($injector) {
|
|
return $injector.get('authInterceptor');
|
|
}
|
|
]);
|
|
}]);
|
|
angular.module('securityModule').factory('authInterceptor', ['$rootScope', '$q', 'AUTH_EVENTS', '$injector', 'session', '$filter',
|
|
function ($rootScope, $q, AUTH_EVENTS, $injector, session, $filter) {
|
|
return {
|
|
responseError: function (response) {
|
|
if ((response.status !== 500) && !(session.expired && response.status === 403)) {
|
|
var $modalStack = $injector.get('$modalStack'); //for some reason Angular cannot inject $modalStack on factory Init
|
|
$modalStack.dismissAll(); //close modal/action blade on session expires
|
|
}
|
|
if (response.status === 401) {
|
|
$rootScope.$broadcast(AUTH_EVENTS.NOT_AUTHORIZED, response);
|
|
}
|
|
if (response.data && response.data.ARConnectionProblem) {
|
|
response.data.error = response.data.additionalMessage;
|
|
$rootScope.$broadcast(AUTH_EVENTS.AR_CONNECTION_FAILED, response);
|
|
}
|
|
if (!session.expired && response.status === 403 && response.data && (response.data.error === 'MOBILITY_ERROR_SESSION_EXPIRED' || response.data.indexOf('MOBILITY_ERROR_SESSION_EXPIRED') !== -1)) {
|
|
if (!response.config.url.match(/users\/sessions/)) { // ignore it, this is logout request
|
|
session.expired = true;
|
|
if (typeof response.data === 'string') {
|
|
response.data = {};
|
|
}
|
|
response.data.error = $filter('i18n')('error.MOBILITY_ERROR_SESSION_EXPIRED');
|
|
$rootScope.$broadcast(AUTH_EVENTS.SESSION_EXPIRED, response);
|
|
}
|
|
}
|
|
return $q.reject(response);
|
|
}
|
|
};
|
|
}]);
|
|
})();
|