From 4534ffaead1721f28d0d210d0ea68594e8a126b4 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 6 Jul 2025 11:32:21 -0500 Subject: [PATCH] Handle re-authenticating socket --- plugins/nativeHttp.js | 9 ++++++++- plugins/server.js | 17 ++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/plugins/nativeHttp.js b/plugins/nativeHttp.js index 815fb219..f4089ebb 100644 --- a/plugins/nativeHttp.js +++ b/plugins/nativeHttp.js @@ -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) } diff --git a/plugins/server.js b/plugins/server.js index 409ebe2d..90f6f715 100644 --- a/plugins/server.js +++ b/plugins/server.js @@ -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) {