blob: 3ac672dffcf16ab12762980256f2315a36e49e05 [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 Scandolo45a02602017-01-06 15:01:08 -080015 // NOTE why are we returning 403 in place of 401??
16 if (res.status === 403 || res.status === -1) {
Matteo Scandolof1e32dc2016-12-23 17:26:39 -080017 $state.go('login');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080018 }
19 return res;
20 };
21
22 return {
23 response: checkLogin,
24 responseError: checkLogin
25 };
26}
27
28export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) {
29 return {
30 request: (req) => {
31 if (!$cookies.get('xoscsrftoken') || !$cookies.get('xossessionid')) {
32 return req;
33 }
Matteo Scandolo6e5da162016-12-23 09:11:04 -080034 // req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035 req.headers['x-csrftoken'] = $cookies.get('xoscsrftoken');
36 req.headers['x-sessionid'] = $cookies.get('xossessionid');
37 return req;
38 }
39 };
40}
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080041
42export function NoHyperlinksInterceptor() {
43 return {
44 request: (req) => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080045 if (
46 req.url.indexOf('.html') === -1
47 // && req.url.indexOf('login') === -1
48 // && req.url.indexOf('logout') === -1
49 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080050 // NOTE this may fail if there are already query params
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080051 req.url += '?no_hyperlinks=1';
52 }
53 return req;
54 }
55 };
56}