quintodrome/ui/src/dataProvider/httpClient.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
947 B
JavaScript
Raw Normal View History

2020-01-20 05:54:29 -09:00
import { fetchUtils } from 'react-admin'
import { baseUrl } from '../utils'
2020-05-11 17:27:00 -08:00
import config from '../config'
import jwtDecode from 'jwt-decode'
2020-01-20 05:54:29 -09:00
const customAuthorizationHeader = 'X-ND-Authorization'
2020-01-20 05:54:29 -09:00
const httpClient = (url, options = {}) => {
2020-04-03 13:50:42 -08:00
url = baseUrl(url)
2020-01-20 05:54:29 -09:00
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' })
}
const token = localStorage.getItem('token')
if (token) {
options.headers.set(customAuthorizationHeader, `Bearer ${token}`)
2020-01-20 05:54:29 -09:00
}
return fetchUtils.fetchJson(url, options).then((response) => {
const token = response.headers.get(customAuthorizationHeader)
2020-01-20 05:54:29 -09:00
if (token) {
const decoded = jwtDecode(token)
2020-01-20 05:54:29 -09:00
localStorage.setItem('token', token)
localStorage.setItem('userId', decoded.uid)
2020-04-08 09:20:02 -08:00
// Avoid going to create admin dialog after logout/login without a refresh
config.firstTime = false
2020-01-20 05:54:29 -09:00
}
return response
})
}
2020-05-11 17:27:00 -08:00
export default httpClient