Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | /// <reference path="../typings/index.d.ts" /> |
| 2 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 3 | export function interceptorConfig($httpProvider: angular.IHttpProvider, $resourceProvider: angular.resource.IResourceServiceProvider) { |
| 4 | $httpProvider.interceptors.push('UserStatusInterceptor'); |
| 5 | $httpProvider.interceptors.push('CredentialsInterceptor'); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 6 | $httpProvider.interceptors.push('NoHyperlinksInterceptor'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 7 | } |
| 8 | |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 9 | export function userStatusInterceptor($state: angular.ui.IStateService, $cookies: ng.cookies.ICookiesService, $q: ng.IQService) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | const checkLogin = (res) => { |
Matteo Scandolo | c37c5ac | 2017-01-09 13:43:31 -0800 | [diff] [blame] | 11 | if (res.status === 401 || res.status === -1) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 12 | $cookies.remove('sessionid', {path: '/'}); |
Matteo Scandolo | f1e32dc | 2016-12-23 17:26:39 -0800 | [diff] [blame] | 13 | $state.go('login'); |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 14 | return $q.reject(res); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | } |
| 16 | return res; |
| 17 | }; |
| 18 | |
| 19 | return { |
| 20 | response: checkLogin, |
| 21 | responseError: checkLogin |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | export function CredentialsInterceptor($cookies: angular.cookies.ICookiesService) { |
| 26 | return { |
| 27 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 28 | if (!$cookies.get('sessionid')) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 29 | return req; |
| 30 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 31 | req.headers['x-sessionid'] = $cookies.get('sessionid'); |
| 32 | req.headers['x-xossession'] = $cookies.get('sessionid'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 33 | return req; |
| 34 | } |
| 35 | }; |
| 36 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 37 | |
| 38 | export function NoHyperlinksInterceptor() { |
| 39 | return { |
| 40 | request: (req) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 41 | if (req.url.indexOf('.html') === -1) { |
| 42 | // NOTE force content type to be JSON |
| 43 | req.headers['Content-Type'] = 'application/json'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 44 | } |
| 45 | return req; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 46 | }, |
| 47 | response: (res) => { |
| 48 | try { |
| 49 | // NOTE convert res.data from string to JSON |
| 50 | res.data = JSON.parse(res.data); |
| 51 | try { |
| 52 | // NOTE chameleon return everything inside an "items" field |
| 53 | res.data = res.data.items; |
| 54 | } catch (_e) { |
| 55 | res.data = res.data; |
| 56 | } |
| 57 | } catch (e) { |
| 58 | res.data = res.data; |
| 59 | } |
| 60 | return res; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | } |