mirror of
https://github.com/navidrome/navidrome.git
synced 2025-08-10 00:52:20 +00:00
* Bump jwt-decode from 3.1.2 to 4.0.0 in /ui Bumps [jwt-decode](https://github.com/auth0/jwt-decode) from 3.1.2 to 4.0.0. - [Release notes](https://github.com/auth0/jwt-decode/releases) - [Changelog](https://github.com/auth0/jwt-decode/blob/main/CHANGELOG.md) - [Commits](https://github.com/auth0/jwt-decode/compare/v3.1.2...v4.0.0) --- updated-dependencies: - dependency-name: jwt-decode dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Make jwt-decode a named import. --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Caio Cotts <caio@cotts.com.br>
35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
import { fetchUtils } from 'react-admin'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { baseUrl } from '../utils'
|
|
import config from '../config'
|
|
import { jwtDecode } from 'jwt-decode'
|
|
|
|
const customAuthorizationHeader = 'X-ND-Authorization'
|
|
const clientUniqueIdHeader = 'X-ND-Client-Unique-Id'
|
|
const clientUniqueId = uuidv4()
|
|
|
|
const httpClient = (url, options = {}) => {
|
|
url = baseUrl(url)
|
|
if (!options.headers) {
|
|
options.headers = new Headers({ Accept: 'application/json' })
|
|
}
|
|
options.headers.set(clientUniqueIdHeader, clientUniqueId)
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
options.headers.set(customAuthorizationHeader, `Bearer ${token}`)
|
|
}
|
|
return fetchUtils.fetchJson(url, options).then((response) => {
|
|
const token = response.headers.get(customAuthorizationHeader)
|
|
if (token) {
|
|
const decoded = jwtDecode(token)
|
|
localStorage.setItem('token', token)
|
|
localStorage.setItem('userId', decoded.uid)
|
|
// Avoid going to create admin dialog after logout/login without a refresh
|
|
config.firstTime = false
|
|
}
|
|
return response
|
|
})
|
|
}
|
|
|
|
export default httpClient
|