mateclaw/mateclaw-ui/src/utils/auth.ts
matevip 579d60125b Initial commit: MateClaw — Java + Vue 3 AI Assistant System
Full-stack AI assistant built on Spring AI Alibaba.
Features: ReAct Agent, Plan-and-Execute, MCP Protocol, Multi-Model, Multi-Channel.

Apache-2.0 License
2026-04-04 19:03:49 +08:00

40 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 认证工具函数
* 统一处理 token 失效跳转和自动续期
*/
let isRedirecting = false
/**
* 处理认证失败:清除 token 并跳转登录页
* 使用 isRedirecting 标记防止多个并发请求同时触发跳转
*/
export function handleAuthFailure() {
localStorage.removeItem('token')
localStorage.removeItem('username')
localStorage.removeItem('role')
// 已经在登录页则不再跳转,避免死循环
if (window.location.pathname === '/login') {
return
}
if (!isRedirecting) {
isRedirecting = true
window.location.href = '/login'
}
}
/**
* 从响应头中提取新 token 并更新 localStorage
* 支持 fetch Headers 和 Axios headers对象格式
*/
export function updateTokenFromHeader(headers: any) {
if (!headers) return
const newToken =
typeof headers.get === 'function'
? headers.get('x-new-token')
: headers['x-new-token']
if (newToken && typeof newToken === 'string') {
localStorage.setItem('token', newToken)
}
}