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 | |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 12 | export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 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 | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 16 | $cookies.remove('xoscsrftoken', {path: '/'}); |
| 17 | $cookies.remove('xossessionid', {path: '/'}); |
| 18 | $cookies.remove('xosuser', {path: '/'}); |
Matteo Scandolo | f1e32dc | 2016-12-23 17:26:39 -0800 | [diff] [blame] | 19 | $state.go('login'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 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 | } |
Matteo Scandolo | 6e5da16 | 2016-12-23 09:11:04 -0800 | [diff] [blame] | 36 | // req.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 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 | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 47 | if ( |
| 48 | req.url.indexOf('.html') === -1 |
| 49 | // && req.url.indexOf('login') === -1 |
| 50 | // && req.url.indexOf('logout') === -1 |
| 51 | ) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 52 | // NOTE this may fail if there are already query params |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 53 | req.url += '?no_hyperlinks=1'; |
| 54 | } |
| 55 | return req; |
| 56 | } |
| 57 | }; |
| 58 | } |