blob: 7707b0bcee2bc5daa67cea2fa277b8e16ed28349 [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) => {
15 if (res.status === 403) {
16 $state.go('login');
17 }
18 else if (res.data.status === 403) {
19 $state.go('login');
20 }
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 }
36 req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
37 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 Scandolo266907e2016-12-20 13:41:42 -080047 if (req.url.indexOf('.html') === -1 && req.url.indexOf('login') !== -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080048 // NOTE this may fail if there are already query params
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080049 req.url += '?no_hyperlinks=1';
50 }
51 return req;
52 }
53 };
54}