angular.module('homepage').
config(function($httpProvider) {
var interceptor = function($q, $location, $localStorage) {
function success(response) {
response.headers = response.headers || {};
if ($localStorage.token) {
response.headers.Authorization = 'Bearer ' + $localStorage.token;
}
return response;
}
function error(response) {
if (response.status === 401 || response.status === 403) {
$location.path('/#/login');
}
return $q.reject(response);
}
return function(promise) {
return promise.then(success, error);
};
};
$httpProvider.interceptors.push(interceptor);
}).
run(function($http, $localStorage, $rootScope, Authentication, MobileType, Server) {
function setMobileType() {
$rootScope.isMobile = MobileType.any();
}
// TODO load local storage templates on load
function initializeLocalStorage() {
if(!($localStorage.templates && $localStorage.templates.length)) {
$localStorage.templates = [];
}
else {
Server.ibsubmit({ 'templates': $localStorage.templates})
.success(function (result) {
console.log(result);
if(result.success) {
$localStorage.templates = [];
}
});
}
}
function initializeDefaults() {
// Set the token as header for your requests!
$http.defaults.headers.common['X-Auth-Token'] = $localStorage.token;
}
function initializeAuthentication() {
$rootScope.userDtls = Authentication.getUser();
$rootScope.isAuthenticated = Authentication.isAuthenticated();
}
function initialize() {
setMobileType();
initializeDefaults();
initializeLocalStorage();
initializeAuthentication();
}
initialize();
window.addEventListener("orientationchange", setMobileType, false);
window.addEventListener("resize", setMobileType, false);
});