mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-28 06:14:00 +02:00
Handle re-authenticating socket
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { CapacitorHttp } from '@capacitor/core'
|
import { CapacitorHttp } from '@capacitor/core'
|
||||||
|
|
||||||
export default function ({ store, $db }, inject) {
|
export default function ({ store, $db, $socket }, inject) {
|
||||||
const nativeHttp = {
|
const nativeHttp = {
|
||||||
async request(method, _url, data, options = {}) {
|
async request(method, _url, data, options = {}) {
|
||||||
// When authorizing before a config is set, server config gets passed in as an option
|
// When authorizing before a config is set, server config gets passed in as an option
|
||||||
@@ -184,6 +184,13 @@ export default function ({ store, $db }, inject) {
|
|||||||
// Update the store
|
// Update the store
|
||||||
store.commit('user/setAccessToken', tokens.accessToken)
|
store.commit('user/setAccessToken', tokens.accessToken)
|
||||||
|
|
||||||
|
// Re-authenticate socket if necessary
|
||||||
|
if ($socket?.connected && !$socket.isAuthenticated) {
|
||||||
|
$socket.sendAuthenticate()
|
||||||
|
} else if (!$socket) {
|
||||||
|
console.warn('[nativeHttp] Socket not available, cannot re-authenticate')
|
||||||
|
}
|
||||||
|
|
||||||
if (savedConfig) {
|
if (savedConfig) {
|
||||||
store.commit('user/setServerConnectionConfig', savedConfig)
|
store.commit('user/setServerConnectionConfig', savedConfig)
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-3
@@ -9,7 +9,7 @@ class ServerSocket extends EventEmitter {
|
|||||||
this.socket = null
|
this.socket = null
|
||||||
this.connected = false
|
this.connected = false
|
||||||
this.serverAddress = null
|
this.serverAddress = null
|
||||||
this.token = null
|
this.isAuthenticated = false
|
||||||
|
|
||||||
this.lastReconnectAttemptTime = 0
|
this.lastReconnectAttemptTime = 0
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,6 @@ class ServerSocket extends EventEmitter {
|
|||||||
|
|
||||||
connect(serverAddress, token) {
|
connect(serverAddress, token) {
|
||||||
this.serverAddress = serverAddress
|
this.serverAddress = serverAddress
|
||||||
this.token = token
|
|
||||||
|
|
||||||
const serverUrl = new URL(serverAddress)
|
const serverUrl = new URL(serverAddress)
|
||||||
const serverHost = `${serverUrl.protocol}//${serverUrl.host}`
|
const serverHost = `${serverUrl.protocol}//${serverUrl.host}`
|
||||||
@@ -53,6 +52,7 @@ class ServerSocket extends EventEmitter {
|
|||||||
this.socket.on('connect', this.onConnect.bind(this))
|
this.socket.on('connect', this.onConnect.bind(this))
|
||||||
this.socket.on('disconnect', this.onDisconnect.bind(this))
|
this.socket.on('disconnect', this.onDisconnect.bind(this))
|
||||||
this.socket.on('init', this.onInit.bind(this))
|
this.socket.on('init', this.onInit.bind(this))
|
||||||
|
this.socket.on('auth_failed', this.onAuthFailed.bind(this))
|
||||||
this.socket.on('user_updated', this.onUserUpdated.bind(this))
|
this.socket.on('user_updated', this.onUserUpdated.bind(this))
|
||||||
this.socket.on('user_item_progress_updated', this.onUserItemProgressUpdated.bind(this))
|
this.socket.on('user_item_progress_updated', this.onUserItemProgressUpdated.bind(this))
|
||||||
this.socket.on('playlist_added', this.onPlaylistAdded.bind(this))
|
this.socket.on('playlist_added', this.onPlaylistAdded.bind(this))
|
||||||
@@ -61,6 +61,11 @@ class ServerSocket extends EventEmitter {
|
|||||||
this.socket.io.on('reconnect_failed', this.onReconnectFailed.bind(this))
|
this.socket.io.on('reconnect_failed', this.onReconnectFailed.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendAuthenticate() {
|
||||||
|
// Required to connect a socket to a user
|
||||||
|
this.socket.emit('auth', this.$store.getters['user/getToken'])
|
||||||
|
}
|
||||||
|
|
||||||
removeListeners() {
|
removeListeners() {
|
||||||
if (!this.socket) return
|
if (!this.socket) return
|
||||||
this.socket.removeAllListeners()
|
this.socket.removeAllListeners()
|
||||||
@@ -74,7 +79,7 @@ class ServerSocket extends EventEmitter {
|
|||||||
this.connected = true
|
this.connected = true
|
||||||
this.$store.commit('setSocketConnected', true)
|
this.$store.commit('setSocketConnected', true)
|
||||||
this.emit('connection-update', true)
|
this.emit('connection-update', true)
|
||||||
this.socket.emit('auth', this.token) // Required to connect a user with their socket
|
this.sendAuthenticate()
|
||||||
}
|
}
|
||||||
|
|
||||||
onReconnectAttempt(attemptNumber) {
|
onReconnectAttempt(attemptNumber) {
|
||||||
@@ -101,6 +106,12 @@ class ServerSocket extends EventEmitter {
|
|||||||
onInit(data) {
|
onInit(data) {
|
||||||
console.log('[SOCKET] Initial socket data received', data)
|
console.log('[SOCKET] Initial socket data received', data)
|
||||||
this.emit('initialized', true)
|
this.emit('initialized', true)
|
||||||
|
this.isAuthenticated = true
|
||||||
|
}
|
||||||
|
|
||||||
|
onAuthFailed(data) {
|
||||||
|
console.log('[SOCKET] Auth failed', data)
|
||||||
|
this.isAuthenticated = false
|
||||||
}
|
}
|
||||||
|
|
||||||
onUserUpdated(data) {
|
onUserUpdated(data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user