fix(auth): 修复登录状态检查和退出登录功能

- 优化登录状态检查逻辑,未登录时重定向到登录页
- 添加原目标页面作为登录后返回参数
- 退出登录时清除缓存,确保安全退出
- 移除文档页面的权限校验中间件
This commit is contained in:
高手 2025-02-16 22:51:25 +08:00
parent d989b0e7cb
commit 2da6933e00
3 changed files with 9 additions and 9 deletions

View File

@ -8,20 +8,18 @@ export default defineClientConfig({
// 仅在客户端执行
if (typeof window !== 'undefined') {
router.beforeEach(async (to, from, next) => {
// 检查是否是首页
// 检查是否从首页访问其他页面
if (from.fullPath === '/' && to.fullPath !== '/') {
try {
// 复用 UserInfo 组件中相同的接口检查登录状态
const response = await fetch('/api/user/info')
if (!response.ok) {
// 未登录状态使用非SPA的路由跳转方式
window.location.href = to.fullPath
// 未登录时重定向到登录页,并带上原目标页面作为参数
window.location.href = `/login?return_url=${encodeURIComponent(to.fullPath)}`
return
}
} catch (error) {
console.error('检查登录状态失败:', error)
// 发生错误时同上
window.location.href = to.fullPath
window.location.href = `/login?return_url=${encodeURIComponent(to.fullPath)}`
return
}
}

View File

@ -139,6 +139,11 @@ func Logout(c *gin.Context) {
// 清除session数据
session.Clear()
// 添加清除缓存的 HTTP 头
c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
c.Header("Pragma", "no-cache")
c.Header("Expires", "0")
// 保存更改
if err := session.Save(); err != nil {
utils.Logger.Errorf("退出登录失败: %v", err)

View File

@ -62,9 +62,6 @@ func main() {
// 文档页面路由
r.GET("/", handlers.ServeIndex(db))
// 权限校验中间件
//r.Use(middleware.AuthRequired())
// 添加路由认证中间件
r.Use(middleware.RouteAuthMiddleware())