fix(auth): 修复登录状态检查和退出登录功能
- 优化登录状态检查逻辑,未登录时重定向到登录页 - 添加原目标页面作为登录后返回参数 - 退出登录时清除缓存,确保安全退出 - 移除文档页面的权限校验中间件
This commit is contained in:
parent
d989b0e7cb
commit
2da6933e00
@ -8,20 +8,18 @@ export default defineClientConfig({
|
|||||||
// 仅在客户端执行
|
// 仅在客户端执行
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
// 检查是否是首页
|
// 检查是否从首页访问其他页面
|
||||||
if (from.fullPath === '/' && to.fullPath !== '/') {
|
if (from.fullPath === '/' && to.fullPath !== '/') {
|
||||||
try {
|
try {
|
||||||
// 复用 UserInfo 组件中相同的接口检查登录状态
|
|
||||||
const response = await fetch('/api/user/info')
|
const response = await fetch('/api/user/info')
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// 未登录状态,使用非SPA的路由跳转方式
|
// 未登录时重定向到登录页,并带上原目标页面作为参数
|
||||||
window.location.href = to.fullPath
|
window.location.href = `/login?return_url=${encodeURIComponent(to.fullPath)}`
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('检查登录状态失败:', error)
|
console.error('检查登录状态失败:', error)
|
||||||
// 发生错误时同上
|
window.location.href = `/login?return_url=${encodeURIComponent(to.fullPath)}`
|
||||||
window.location.href = to.fullPath
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -139,6 +139,11 @@ func Logout(c *gin.Context) {
|
|||||||
// 清除session数据
|
// 清除session数据
|
||||||
session.Clear()
|
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 {
|
if err := session.Save(); err != nil {
|
||||||
utils.Logger.Errorf("退出登录失败: %v", err)
|
utils.Logger.Errorf("退出登录失败: %v", err)
|
||||||
|
|||||||
@ -62,9 +62,6 @@ func main() {
|
|||||||
// 文档页面路由
|
// 文档页面路由
|
||||||
r.GET("/", handlers.ServeIndex(db))
|
r.GET("/", handlers.ServeIndex(db))
|
||||||
|
|
||||||
// 权限校验中间件
|
|
||||||
//r.Use(middleware.AuthRequired())
|
|
||||||
|
|
||||||
// 添加路由认证中间件
|
// 添加路由认证中间件
|
||||||
r.Use(middleware.RouteAuthMiddleware())
|
r.Use(middleware.RouteAuthMiddleware())
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user