Files
skt-vuetify-templates/src/services/session.ts
T
skytek_xinliang 71683482e1 refactor: ky
2026-05-07 11:17:30 +08:00

27 lines
792 B
TypeScript

// 全域 Session 事件(Playground 專用)
//
// 目的:
// - 避免 HTTP hooks 直接 import Pinia store / router 造成循環依賴
// - 由 App.vue 在 UI 層統一處理登出流程(清狀態、導頁、提示訊息)
export const SESSION_FORCE_LOGOUT_EVENT = 'sk-playground:session-force-logout'
type ForceLogoutDetail = {
message?: string
}
let forceLogoutEmitted = false
export function emitForceLogout(detail: ForceLogoutDetail) {
// 避免同一波大量 401 觸發多次登出流程
if (forceLogoutEmitted) return
forceLogoutEmitted = true
window.dispatchEvent(new CustomEvent(SESSION_FORCE_LOGOUT_EVENT, { detail }))
// 下一個 event loop 再允許觸發(避免把 guard 永久鎖住)
setTimeout(() => {
forceLogoutEmitted = false
}, 0)
}