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) => { |
Matteo Scandolo | c37c5ac | 2017-01-09 13:43:31 -0800 | [diff] [blame^] | 15 | if (res.status === 401 || res.status === -1) { |
Matteo Scandolo | f1e32dc | 2016-12-23 17:26:39 -0800 | [diff] [blame] | 16 | $state.go('login'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 17 | } |
| 18 | return res; |
| 19 | }; |
| 20 | |
| 21 | return { |
| 22 | response: checkLogin, |
| 23 | responseError: checkLogin |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 28 | return { |
| 29 | request: (req) => { |
| 30 | if (!$cookies.get('xoscsrftoken') || !$cookies.get('xossessionid')) { |
| 31 | return req; |
| 32 | } |
Matteo Scandolo | 6e5da16 | 2016-12-23 09:11:04 -0800 | [diff] [blame] | 33 | // req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 34 | req.headers['x-csrftoken'] = $cookies.get('xoscsrftoken'); |
| 35 | req.headers['x-sessionid'] = $cookies.get('xossessionid'); |
| 36 | return req; |
| 37 | } |
| 38 | }; |
| 39 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 40 | |
| 41 | export function NoHyperlinksInterceptor() { |
| 42 | return { |
| 43 | request: (req) => { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 44 | if ( |
| 45 | req.url.indexOf('.html') === -1 |
| 46 | // && req.url.indexOf('login') === -1 |
| 47 | // && req.url.indexOf('logout') === -1 |
| 48 | ) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 49 | // NOTE this may fail if there are already query params |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | req.url += '?no_hyperlinks=1'; |
| 51 | } |
| 52 | return req; |
| 53 | } |
| 54 | }; |
| 55 | } |