mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-28 13:58:23 +02:00
Update auth to handle refresh tokens
This commit is contained in:
parent
67bab72783
commit
d8cdb7073e
13 changed files with 828 additions and 141 deletions
|
@ -1,4 +1,4 @@
|
|||
import { registerPlugin, Capacitor, WebPlugin } from '@capacitor/core';
|
||||
import { registerPlugin, Capacitor, WebPlugin } from '@capacitor/core'
|
||||
|
||||
class AbsDatabaseWeb extends WebPlugin {
|
||||
constructor() {
|
||||
|
@ -22,7 +22,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
async setCurrentServerConnectionConfig(serverConnectionConfig) {
|
||||
var deviceData = await this.getDeviceData()
|
||||
|
||||
var ssc = deviceData.serverConnectionConfigs.find(_ssc => _ssc.id == serverConnectionConfig.id)
|
||||
var ssc = deviceData.serverConnectionConfigs.find((_ssc) => _ssc.id == serverConnectionConfig.id)
|
||||
if (ssc) {
|
||||
deviceData.lastServerConnectionConfigId = ssc.id
|
||||
ssc.name = `${ssc.address} (${serverConnectionConfig.username})`
|
||||
|
@ -30,6 +30,13 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
ssc.userId = serverConnectionConfig.userId
|
||||
ssc.username = serverConnectionConfig.username
|
||||
ssc.customHeaders = serverConnectionConfig.customHeaders || {}
|
||||
|
||||
if (serverConnectionConfig.refreshToken) {
|
||||
console.log('[AbsDatabase] Updating refresh token...', serverConnectionConfig.refreshToken)
|
||||
// Only using local storage for web version that is only used for testing
|
||||
localStorage.setItem(`refresh_token_${ssc.id}`, serverConnectionConfig.refreshToken)
|
||||
}
|
||||
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
} else {
|
||||
ssc = {
|
||||
|
@ -42,6 +49,13 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
token: serverConnectionConfig.token,
|
||||
customHeaders: serverConnectionConfig.customHeaders || {}
|
||||
}
|
||||
|
||||
if (serverConnectionConfig.refreshToken) {
|
||||
console.log('[AbsDatabase] Setting refresh token...', serverConnectionConfig.refreshToken)
|
||||
// Only using local storage for web version that is only used for testing
|
||||
localStorage.setItem(`refresh_token_${ssc.id}`, serverConnectionConfig.refreshToken)
|
||||
}
|
||||
|
||||
deviceData.serverConnectionConfigs.push(ssc)
|
||||
deviceData.lastServerConnectionConfigId = ssc.id
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
|
@ -49,10 +63,16 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
return ssc
|
||||
}
|
||||
|
||||
async getRefreshToken({ serverConnectionConfigId }) {
|
||||
console.log('[AbsDatabase] Getting refresh token...', serverConnectionConfigId)
|
||||
const refreshToken = localStorage.getItem(`refresh_token_${serverConnectionConfigId}`)
|
||||
return refreshToken ? { refreshToken } : null
|
||||
}
|
||||
|
||||
async removeServerConnectionConfig(serverConnectionConfigCallObject) {
|
||||
var serverConnectionConfigId = serverConnectionConfigCallObject.serverConnectionConfigId
|
||||
var deviceData = await this.getDeviceData()
|
||||
deviceData.serverConnectionConfigs = deviceData.serverConnectionConfigs.filter(ssc => ssc.id != serverConnectionConfigId)
|
||||
deviceData.serverConnectionConfigs = deviceData.serverConnectionConfigs.filter((ssc) => ssc.id != serverConnectionConfigId)
|
||||
localStorage.setItem('device', JSON.stringify(deviceData))
|
||||
}
|
||||
|
||||
|
@ -85,79 +105,81 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
}
|
||||
async getLocalLibraryItems(payload) {
|
||||
return {
|
||||
value: [{
|
||||
id: 'local_test',
|
||||
libraryItemId: 'test34',
|
||||
serverAddress: 'https://abs.test.com',
|
||||
serverUserId: 'test56',
|
||||
folderId: 'test1',
|
||||
absolutePath: 'a',
|
||||
contentUrl: 'c',
|
||||
isInvalid: false,
|
||||
mediaType: 'book',
|
||||
media: {
|
||||
metadata: {
|
||||
title: 'Test Book',
|
||||
authorName: 'Test Author Name'
|
||||
value: [
|
||||
{
|
||||
id: 'local_test',
|
||||
libraryItemId: 'test34',
|
||||
serverAddress: 'https://abs.test.com',
|
||||
serverUserId: 'test56',
|
||||
folderId: 'test1',
|
||||
absolutePath: 'a',
|
||||
contentUrl: 'c',
|
||||
isInvalid: false,
|
||||
mediaType: 'book',
|
||||
media: {
|
||||
metadata: {
|
||||
title: 'Test Book',
|
||||
authorName: 'Test Author Name'
|
||||
},
|
||||
coverPath: null,
|
||||
tags: [],
|
||||
audioFiles: [],
|
||||
chapters: [],
|
||||
tracks: [
|
||||
{
|
||||
index: 1,
|
||||
startOffset: 0,
|
||||
duration: 10000,
|
||||
title: 'Track Title 1',
|
||||
contentUrl: 'test',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf1',
|
||||
audioProbeResult: {}
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
startOffset: 0,
|
||||
duration: 15000,
|
||||
title: 'Track Title 2',
|
||||
contentUrl: 'test2',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf2',
|
||||
audioProbeResult: {}
|
||||
},
|
||||
{
|
||||
index: 3,
|
||||
startOffset: 0,
|
||||
duration: 20000,
|
||||
title: 'Track Title 3',
|
||||
contentUrl: 'test3',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf3',
|
||||
audioProbeResult: {}
|
||||
}
|
||||
]
|
||||
},
|
||||
coverPath: null,
|
||||
tags: [],
|
||||
audioFiles: [],
|
||||
chapters: [],
|
||||
tracks: [
|
||||
localFiles: [
|
||||
{
|
||||
index: 1,
|
||||
startOffset: 0,
|
||||
duration: 10000,
|
||||
title: 'Track Title 1',
|
||||
id: 'lf1',
|
||||
filename: 'lf1.mp3',
|
||||
contentUrl: 'test',
|
||||
absolutePath: 'test',
|
||||
simplePath: 'test',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf1',
|
||||
audioProbeResult: {}
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
startOffset: 0,
|
||||
duration: 15000,
|
||||
title: 'Track Title 2',
|
||||
contentUrl: 'test2',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf2',
|
||||
audioProbeResult: {}
|
||||
},
|
||||
{
|
||||
index: 3,
|
||||
startOffset: 0,
|
||||
duration: 20000,
|
||||
title: 'Track Title 3',
|
||||
contentUrl: 'test3',
|
||||
mimeType: 'audio/mpeg',
|
||||
metadata: null,
|
||||
isLocal: true,
|
||||
localFileId: 'lf3',
|
||||
audioProbeResult: {}
|
||||
size: 39048290
|
||||
}
|
||||
]
|
||||
},
|
||||
localFiles: [
|
||||
{
|
||||
id: 'lf1',
|
||||
filename: 'lf1.mp3',
|
||||
contentUrl: 'test',
|
||||
absolutePath: 'test',
|
||||
simplePath: 'test',
|
||||
mimeType: 'audio/mpeg',
|
||||
size: 39048290
|
||||
}
|
||||
],
|
||||
coverContentUrl: null,
|
||||
coverAbsolutePath: null,
|
||||
isLocal: true
|
||||
}]
|
||||
],
|
||||
coverContentUrl: null,
|
||||
coverAbsolutePath: null,
|
||||
isLocal: true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
async getLocalLibraryItemsInFolder({ folderId }) {
|
||||
|
@ -167,7 +189,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
return this.getLocalLibraryItems().then((data) => data.value[0])
|
||||
}
|
||||
async getLocalLibraryItemByLId({ libraryItemId }) {
|
||||
return this.getLocalLibraryItems().then((data) => data.value.find(lli => lli.libraryItemId == libraryItemId))
|
||||
return this.getLocalLibraryItems().then((data) => data.value.find((lli) => lli.libraryItemId == libraryItemId))
|
||||
}
|
||||
async getAllLocalMediaProgress() {
|
||||
return {
|
||||
|
@ -182,7 +204,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
isFinished: false,
|
||||
lastUpdate: 394089090,
|
||||
startedAt: 239048209,
|
||||
finishedAt: null,
|
||||
finishedAt: null
|
||||
// For local lib items from server to support server sync
|
||||
// var serverConnectionConfigId:String?,
|
||||
// var serverAddress:String?,
|
||||
|
@ -240,7 +262,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 22) + 13000 // 22 mins ago + 13s
|
||||
timestamp: Date.now() - 1000 * 60 * 22 + 13000 // 22 mins ago + 13s
|
||||
},
|
||||
{
|
||||
name: 'Play',
|
||||
|
@ -250,7 +272,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: false,
|
||||
serverSyncSuccess: null,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 22) // 22 mins ago
|
||||
timestamp: Date.now() - 1000 * 60 * 22 // 22 mins ago
|
||||
},
|
||||
{
|
||||
name: 'Pause',
|
||||
|
@ -260,7 +282,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: false,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60) + (58000) // 1 hour ago + 58s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 + 58000 // 1 hour ago + 58s
|
||||
},
|
||||
{
|
||||
name: 'Save',
|
||||
|
@ -270,7 +292,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60) + (45000) // 1 hour ago + 45s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 + 45000 // 1 hour ago + 45s
|
||||
},
|
||||
{
|
||||
name: 'Save',
|
||||
|
@ -280,7 +302,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60) + (30000) // 1 hour ago + 30s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 + 30000 // 1 hour ago + 30s
|
||||
},
|
||||
{
|
||||
name: 'Save',
|
||||
|
@ -290,7 +312,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60) + (15000) // 1 hour ago + 15s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 + 15000 // 1 hour ago + 15s
|
||||
},
|
||||
{
|
||||
name: 'Play',
|
||||
|
@ -300,7 +322,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: false,
|
||||
serverSyncSuccess: null,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60) // 1 hour ago
|
||||
timestamp: Date.now() - 1000 * 60 * 60 // 1 hour ago
|
||||
},
|
||||
{
|
||||
name: 'Stop',
|
||||
|
@ -310,7 +332,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60 * 25) + 10000 // 25 hours ago + 10s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 * 25 + 10000 // 25 hours ago + 10s
|
||||
},
|
||||
{
|
||||
name: 'Seek',
|
||||
|
@ -320,7 +342,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: true,
|
||||
serverSyncSuccess: true,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60 * 25) + 2000 // 25 hours ago + 2s
|
||||
timestamp: Date.now() - 1000 * 60 * 60 * 25 + 2000 // 25 hours ago + 2s
|
||||
},
|
||||
{
|
||||
name: 'Play',
|
||||
|
@ -330,7 +352,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: false,
|
||||
serverSyncSuccess: null,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60 * 25) // 25 hours ago
|
||||
timestamp: Date.now() - 1000 * 60 * 60 * 25 // 25 hours ago
|
||||
},
|
||||
{
|
||||
name: 'Play',
|
||||
|
@ -340,7 +362,7 @@ class AbsDatabaseWeb extends WebPlugin {
|
|||
serverSyncAttempted: false,
|
||||
serverSyncSuccess: null,
|
||||
serverSyncMessage: null,
|
||||
timestamp: Date.now() - (1000 * 60 * 60 * 50) // 50 hours ago
|
||||
timestamp: Date.now() - 1000 * 60 * 60 * 50 // 50 hours ago
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -351,4 +373,4 @@ const AbsDatabase = registerPlugin('AbsDatabase', {
|
|||
web: () => new AbsDatabaseWeb()
|
||||
})
|
||||
|
||||
export { AbsDatabase }
|
||||
export { AbsDatabase }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue