mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
* feat: create vite project * feat: it's alive! * feat: `make dev` working! * feat: replace custom serviceWorker with vite plugin * test: replace Jest with Vitest * fix: run prettier * fix: skip eslint for now. * chore: remove ui.old folder * refactor: replace lodash.pick with simple destructuring * fix: eslint errors (wip) * fix: eslint errors (wip) * fix: display-name eslint errors (wip) * fix: no-console eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: react-refresh/only-export-components eslint errors (wip) * fix: build * fix: pwa manifest * refactor: pwa manifest * refactor: simplify PORT configuration * refactor: rename simple JS files * test: cover playlistUtils * fix: react-image-lightbox * feat(ui): add sourcemaps to help debug issues
111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
import { jwtDecode } from 'jwt-decode'
|
|
import { baseUrl } from './utils'
|
|
import config from './config'
|
|
|
|
// config sent from server may contain authentication info, for example when the user is authenticated
|
|
// by a reverse proxy request header
|
|
if (config.auth) {
|
|
try {
|
|
storeAuthenticationInfo(config.auth)
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.log(e)
|
|
}
|
|
}
|
|
|
|
function storeAuthenticationInfo(authInfo) {
|
|
authInfo.token && localStorage.setItem('token', authInfo.token)
|
|
localStorage.setItem('userId', authInfo.id)
|
|
localStorage.setItem('name', authInfo.name)
|
|
localStorage.setItem('username', authInfo.username)
|
|
authInfo.avatar && localStorage.setItem('avatar', authInfo.avatar)
|
|
localStorage.setItem('role', authInfo.isAdmin ? 'admin' : 'regular')
|
|
localStorage.setItem('subsonic-salt', authInfo.subsonicSalt)
|
|
localStorage.setItem('subsonic-token', authInfo.subsonicToken)
|
|
localStorage.setItem('lastfm-apikey', authInfo.lastFMApiKey)
|
|
localStorage.setItem('is-authenticated', 'true')
|
|
}
|
|
|
|
const authProvider = {
|
|
login: ({ username, password }) => {
|
|
let url = baseUrl('/auth/login')
|
|
if (config.firstTime) {
|
|
url = baseUrl('/auth/createAdmin')
|
|
}
|
|
const request = new Request(url, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ username, password }),
|
|
headers: new Headers({ 'Content-Type': 'application/json' }),
|
|
})
|
|
return fetch(request)
|
|
.then((response) => {
|
|
if (response.status < 200 || response.status >= 300) {
|
|
throw new Error(response.statusText)
|
|
}
|
|
return response.json()
|
|
})
|
|
.then((response) => {
|
|
jwtDecode(response.token) // Validate token
|
|
storeAuthenticationInfo(response)
|
|
// Avoid "going to create admin" dialog after logout/login without a refresh
|
|
config.firstTime = false
|
|
return response
|
|
})
|
|
.catch((error) => {
|
|
if (
|
|
error.message === 'Failed to fetch' ||
|
|
error.stack === 'TypeError: Failed to fetch'
|
|
) {
|
|
throw new Error('errors.network_error')
|
|
}
|
|
|
|
throw new Error(error)
|
|
})
|
|
},
|
|
|
|
logout: () => {
|
|
removeItems()
|
|
return Promise.resolve()
|
|
},
|
|
|
|
checkAuth: () =>
|
|
localStorage.getItem('is-authenticated')
|
|
? Promise.resolve()
|
|
: Promise.reject(),
|
|
|
|
checkError: ({ status }) => {
|
|
if (status === 401) {
|
|
removeItems()
|
|
return Promise.reject()
|
|
}
|
|
return Promise.resolve()
|
|
},
|
|
|
|
getPermissions: () => {
|
|
const role = localStorage.getItem('role')
|
|
return role ? Promise.resolve(role) : Promise.reject()
|
|
},
|
|
|
|
getIdentity: () => {
|
|
return Promise.resolve({
|
|
id: localStorage.getItem('username'),
|
|
fullName: localStorage.getItem('name'),
|
|
avatar: localStorage.getItem('avatar'),
|
|
})
|
|
},
|
|
}
|
|
|
|
const removeItems = () => {
|
|
localStorage.removeItem('token')
|
|
localStorage.removeItem('userId')
|
|
localStorage.removeItem('name')
|
|
localStorage.removeItem('username')
|
|
localStorage.removeItem('avatar')
|
|
localStorage.removeItem('role')
|
|
localStorage.removeItem('subsonic-salt')
|
|
localStorage.removeItem('subsonic-token')
|
|
localStorage.removeItem('lastfm-apikey')
|
|
localStorage.removeItem('is-authenticated')
|
|
}
|
|
|
|
export default authProvider
|