Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../typings/index.d.ts" /> |
| 2 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 3 | // TODO handle backend failure |
| 4 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) { |
| 6 | $httpProvider.interceptors.push('UserStatusInterceptor'); |
| 7 | $httpProvider.interceptors.push('CredentialsInterceptor'); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 8 | $httpProvider.interceptors.push('NoHyperlinksInterceptor'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 9 | $resourceProvider.defaults.stripTrailingSlashes = false; |
| 10 | } |
| 11 | |
| 12 | export 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 | |
| 30 | export 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 43 | |
| 44 | export function NoHyperlinksInterceptor() { |
| 45 | return { |
| 46 | request: (req) => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 47 | if (req.url.indexOf('.html') === -1 && req.url.indexOf('login') !== -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 48 | // NOTE this may fail if there are already query params |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 49 | req.url += '?no_hyperlinks=1'; |
| 50 | } |
| 51 | return req; |
| 52 | } |
| 53 | }; |
| 54 | } |