Handle re-authenticating socket

This commit is contained in:
advplyr
2025-07-06 11:32:21 -05:00
parent d35dd2df1a
commit 4534ffaead
2 changed files with 22 additions and 4 deletions
+14 -3
View File
@@ -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) {