blob: 9e3ba65b127be992a75557def6e42a0e69a2e844 [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../typings/index.d.ts" />
2
Matteo Scandolo9f87f302016-12-13 18:11:10 -08003// TODO handle backend failure
4
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) {
6 $httpProvider.interceptors.push('UserStatusInterceptor');
7 $httpProvider.interceptors.push('CredentialsInterceptor');
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08008 $httpProvider.interceptors.push('NoHyperlinksInterceptor');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08009 $resourceProvider.defaults.stripTrailingSlashes = false;
10}
11
Matteo Scandolo0e363772017-01-13 11:41:29 -080012export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService) {
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080013
14 const checkLogin = (res) => {
Matteo Scandoloc37c5ac2017-01-09 13:43:31 -080015 if (res.status === 401 || res.status === -1) {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080016 $cookies.remove('xoscsrftoken', {path: '/'});
17 $cookies.remove('xossessionid', {path: '/'});
18 $cookies.remove('xosuser', {path: '/'});
Matteo Scandolof1e32dc2016-12-23 17:26:39 -080019 $state.go('login');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080020 }
21 return res;
22 };
23
24 return {
25 response: checkLogin,
26 responseError: checkLogin
27 };
28}
29
30export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
31 return {
32 request: (req) => {
33 if (!$cookies.get('xoscsrftoken') || !$cookies.get('xossessionid')) {
34 return req;
35 }
Matteo Scandolo6e5da162016-12-23 09:11:04 -080036 // req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080037 req.headers['x-csrftoken'] = $cookies.get('xoscsrftoken');
38 req.headers['x-sessionid'] = $cookies.get('xossessionid');
39 return req;
40 }
41 };
42}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043
44export function NoHyperlinksInterceptor() {
45 return {
46 request: (req) => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080047 if (
48 req.url.indexOf('.html') === -1
49 // && req.url.indexOf('login') === -1
50 // && req.url.indexOf('logout') === -1
51 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080052 // NOTE this may fail if there are already query params
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080053 req.url += '?no_hyperlinks=1';
54 }
55 return req;
56 }
57 };
58}