blob: 80be1308b9645420969c7903c0eb141ebd1370ad [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
12export function userStatusInterceptor($state: angular.ui.IStateService) {
13
14 const checkLogin = (res) => {
Matteo Scandoloc37c5ac2017-01-09 13:43:31 -080015 if (res.status === 401 || res.status === -1) {
Matteo Scandolof1e32dc2016-12-23 17:26:39 -080016 $state.go('login');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080017 }
18 return res;
19 };
20
21 return {
22 response: checkLogin,
23 responseError: checkLogin
24 };
25}
26
27export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
28 return {
29 request: (req) => {
30 if (!$cookies.get('xoscsrftoken') || !$cookies.get('xossessionid')) {
31 return req;
32 }
Matteo Scandolo6e5da162016-12-23 09:11:04 -080033 // req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080034 req.headers['x-csrftoken'] = $cookies.get('xoscsrftoken');
35 req.headers['x-sessionid'] = $cookies.get('xossessionid');
36 return req;
37 }
38 };
39}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080040
41export function NoHyperlinksInterceptor() {
42 return {
43 request: (req) => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080044 if (
45 req.url.indexOf('.html') === -1
46 // && req.url.indexOf('login') === -1
47 // && req.url.indexOf('logout') === -1
48 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080049 // NOTE this may fail if there are already query params
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050 req.url += '?no_hyperlinks=1';
51 }
52 return req;
53 }
54 };
55}