mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-06 20:08:48 +02:00
Update auth to handle refresh tokens
This commit is contained in:
+10
-16
@@ -99,21 +99,6 @@ export default {
|
||||
|
||||
await this.$store.dispatch('user/loadUserSettings')
|
||||
},
|
||||
async postRequest(url, data, headers, connectTimeout = 30000) {
|
||||
const options = {
|
||||
url,
|
||||
headers,
|
||||
data,
|
||||
connectTimeout
|
||||
}
|
||||
const response = await CapacitorHttp.post(options)
|
||||
console.log('[default] POST request response', response)
|
||||
if (response.status >= 400) {
|
||||
throw new Error(response.data)
|
||||
} else {
|
||||
return response.data
|
||||
}
|
||||
},
|
||||
async attemptConnection() {
|
||||
console.warn('[default] attemptConnection')
|
||||
if (!this.networkConnected) {
|
||||
@@ -145,10 +130,18 @@ export default {
|
||||
|
||||
AbsLogger.info({ tag: 'default', message: `attemptConnection: Got server config, attempt authorize (${serverConfig.name})` })
|
||||
|
||||
const authRes = await this.postRequest(`${serverConfig.address}/api/authorize`, null, { Authorization: `Bearer ${serverConfig.token}` }, 6000).catch((error) => {
|
||||
const nativeHttpOptions = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${serverConfig.token}`
|
||||
},
|
||||
connectTimeout: 6000,
|
||||
serverConnectionConfig: serverConfig
|
||||
}
|
||||
const authRes = await this.$nativeHttp.post(`${serverConfig.address}/api/authorize`, null, nativeHttpOptions).catch((error) => {
|
||||
AbsLogger.error({ tag: 'default', message: `attemptConnection: Server auth failed (${serverConfig.name})` })
|
||||
return false
|
||||
})
|
||||
|
||||
if (!authRes) {
|
||||
this.attemptingConnection = false
|
||||
return
|
||||
@@ -168,6 +161,7 @@ export default {
|
||||
const serverConnectionConfig = await this.$db.setServerConnectionConfig(serverConfig)
|
||||
|
||||
this.$store.commit('user/setUser', user)
|
||||
this.$store.commit('user/setAccessToken', serverConnectionConfig.token)
|
||||
this.$store.commit('user/setServerConnectionConfig', serverConnectionConfig)
|
||||
|
||||
this.$socket.connect(serverConnectionConfig.address, serverConnectionConfig.token)
|
||||
|
||||
Reference in New Issue
Block a user