fix: create a subsonic token on login, to use for subsonic API calls

This commit is contained in:
Deluan
2020-02-06 20:37:31 -05:00
parent 8673533cd4
commit 99361c0d9f
4 changed files with 42 additions and 10 deletions
+13
View File
@@ -1,4 +1,5 @@
import jwtDecode from 'jwt-decode'
import md5 from 'md5-hex'
const authProvider = {
login: ({ username, password }) => {
@@ -27,6 +28,12 @@ const authProvider = {
localStorage.setItem('name', response.name)
localStorage.setItem('username', response.username)
localStorage.setItem('role', response.isAdmin ? 'admin' : 'regular')
const salt = new Date().getTime().toString()
localStorage.setItem('subsonic-salt', salt)
localStorage.setItem(
'subsonic-token',
generateSubsonicToken(password, salt)
)
return response
})
.catch((error) => {
@@ -73,6 +80,12 @@ const removeItems = () => {
localStorage.removeItem('username')
localStorage.removeItem('role')
localStorage.removeItem('version')
localStorage.removeItem('subsonic-salt')
localStorage.removeItem('subsonic-token')
}
const generateSubsonicToken = (password, salt) => {
return md5(password + salt)
}
export default authProvider
+15 -10
View File
@@ -5,20 +5,25 @@ const PLAYER_SET_TRACK = 'PLAYER_SET_TRACK'
const PLAYER_SYNC_QUEUE = 'PLAYER_SYNC_QUEUE'
const PLAYER_SCROBBLE = 'PLAYER_SCROBBLE'
const subsonicUrl = (command, id, options) => {
const username = localStorage.getItem('username')
const token = localStorage.getItem('subsonic-token')
const salt = localStorage.getItem('subsonic-salt')
const timeStamp = new Date().getTime()
const url = `rest/${command}?u=${username}&f=json&v=1.8.0&c=NavidromeUI&t=${token}&s=${salt}&id=${id}&_=${timeStamp}`
if (options) {
return url + '&' + options
}
return url
}
const mapToAudioLists = (item) => ({
id: item.id,
name: item.title,
singer: item.artist,
cover: `/rest/getCoverArt?u=admin&f=json&v=1.8.0&c=NavidromeUI&size=300&id=${
item.id
}&jwt=${localStorage.getItem('token')}`,
musicSrc: `/rest/stream?u=admin&f=json&v=1.8.0&c=NavidromeUI&jwt=${localStorage.getItem(
'token'
)}&id=${item.id}&_=${new Date().getTime()}`,
scrobble: (submit) =>
`/rest/scrobble?u=admin&jwt=${localStorage.getItem(
'token'
)}&f=json&v=1.8.0&c=NavidromeUI&id=${item.id}&submission=${submit}`
cover: subsonicUrl('getCoverArt', item.id),
musicSrc: subsonicUrl('stream', item.id),
scrobble: (submit) => subsonicUrl('scrobble', item.id, `submission=${submit}`)
})
const addTrack = (data) => ({