feat: 添加认证守卫工具函数

- 创建authGuard.ts工具模块
- 实现requireAuth函数用于主动检查用户登录状态
- 未登录时显示友好提示并自动跳转到登录页
- 实现isAuthenticated函数用于静默检查登录状态
This commit is contained in:
Leo 2025-10-13 21:34:57 +08:00
parent b8fe29d5ba
commit f1c12a974c

38
src/utils/authGuard.ts Normal file
View File

@ -0,0 +1,38 @@
/**
*
*
*/
import { message } from 'antd'
import { getToken } from './request'
/**
*
* ,
* @param actionName ,("点赞""收藏")
* @returns (true=,false=)
*/
export const requireAuth = (actionName: string = '此操作'): boolean => {
const token = getToken()
if (!token) {
message.warning(`${actionName}需要登录,请先登录`)
// 延迟跳转,让用户看到提示信息
setTimeout(() => {
window.location.href = '/login'
}, 800)
return false
}
return true
}
/**
* (,)
* @returns
*/
export const isAuthenticated = (): boolean => {
return !!getToken()
}