Files
CFDivePlatform/frontend/src/stores/auth.js
T
a620906209 1ba4ca893a fix: 修正即時聊天室 presence channel 間歇性失效
三個根本原因:
1. updateEchoToken() 呼叫 disconnect/connect 會中斷已訂閱的
   presence channel,改為只更新 auth header(不重連)。
   同時將 setAuth() 的呼叫順序改為先 updateEchoToken() 再
   startRealtime(),確保訂閱時使用最新 token。
2. BookingPresenceChannel::join() 未對 schedule null 做防守,
   載入後若 schedule 不存在會 500,導致 auth 靜默失敗。
3. WebSocket 重連後 presence channel 不會自動恢復,
   在 BookingChat 加入 reconnected 事件處理,重連後重訂閱。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-28 17:46:15 +08:00

51 lines
1.4 KiB
JavaScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import api from '../api/axios'
import { useNotificationStore } from './notifications'
import { updateEchoToken } from '../plugins/echo'
export const useAuthStore = defineStore('auth', () => {
const user = ref(null)
const token = ref(null)
const isLoggedIn = computed(() => !!token.value)
function init() {
const saved = localStorage.getItem('token')
const savedUser = localStorage.getItem('user')
if (saved) {
token.value = saved
user.value = savedUser ? JSON.parse(savedUser) : null
const ns = useNotificationStore()
ns.startPolling()
ns.startRealtime(user.value?.id)
}
}
function setAuth(userData, tokenValue) {
user.value = userData
token.value = tokenValue
localStorage.setItem('token', tokenValue)
localStorage.setItem('user', JSON.stringify(userData))
const ns = useNotificationStore()
ns.startPolling()
updateEchoToken()
ns.startRealtime(userData.id)
}
async function logout() {
try {
await api.post('/member/logout')
} catch {}
const ns = useNotificationStore()
ns.stopRealtime()
ns.stopPolling()
user.value = null
token.value = null
localStorage.removeItem('token')
localStorage.removeItem('user')
}
return { user, token, isLoggedIn, init, setAuth, logout }
})