mirror of
https://github.com/sudoxnym/audiobookshelf-atv.git
synced 2026-04-14 11:36:27 +00:00
Handle re-authenticating socket
This commit is contained in:
parent
d35dd2df1a
commit
4534ffaead
2 changed files with 22 additions and 4 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { CapacitorHttp } from '@capacitor/core'
|
||||
|
||||
export default function ({ store, $db }, inject) {
|
||||
export default function ({ store, $db, $socket }, inject) {
|
||||
const nativeHttp = {
|
||||
async request(method, _url, data, options = {}) {
|
||||
// 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
|
||||
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) {
|
||||
store.commit('user/setServerConnectionConfig', savedConfig)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ class ServerSocket extends EventEmitter {
|
|||
this.socket = null
|
||||
this.connected = false
|
||||
this.serverAddress = null
|
||||
this.token = null
|
||||
this.isAuthenticated = false
|
||||
|
||||
this.lastReconnectAttemptTime = 0
|
||||
}
|
||||
|
|
@ -26,7 +26,6 @@ class ServerSocket extends EventEmitter {
|
|||
|
||||
connect(serverAddress, token) {
|
||||
this.serverAddress = serverAddress
|
||||
this.token = token
|
||||
|
||||
const serverUrl = new URL(serverAddress)
|
||||
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('disconnect', this.onDisconnect.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_item_progress_updated', this.onUserItemProgressUpdated.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))
|
||||
}
|
||||
|
||||
sendAuthenticate() {
|
||||
// Required to connect a socket to a user
|
||||
this.socket.emit('auth', this.$store.getters['user/getToken'])
|
||||
}
|
||||
|
||||
removeListeners() {
|
||||
if (!this.socket) return
|
||||
this.socket.removeAllListeners()
|
||||
|
|
@ -74,7 +79,7 @@ class ServerSocket extends EventEmitter {
|
|||
this.connected = true
|
||||
this.$store.commit('setSocketConnected', true)
|
||||
this.emit('connection-update', true)
|
||||
this.socket.emit('auth', this.token) // Required to connect a user with their socket
|
||||
this.sendAuthenticate()
|
||||
}
|
||||
|
||||
onReconnectAttempt(attemptNumber) {
|
||||
|
|
@ -101,6 +106,12 @@ class ServerSocket extends EventEmitter {
|
|||
onInit(data) {
|
||||
console.log('[SOCKET] Initial socket data received', data)
|
||||
this.emit('initialized', true)
|
||||
this.isAuthenticated = true
|
||||
}
|
||||
|
||||
onAuthFailed(data) {
|
||||
console.log('[SOCKET] Auth failed', data)
|
||||
this.isAuthenticated = false
|
||||
}
|
||||
|
||||
onUserUpdated(data) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue