fix: auto logout when cookie expires (user_id becomes Guest)
This commit is contained in:
parent
67a117f65f
commit
09d312d076
@ -1,6 +1,6 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { loginApi, getUserInfoApi, logoutApi, getSessionUser, isCookieExpired } from '../api/auth'
|
||||
import { loginApi, getUserInfoApi, logoutApi, isCookieExpired } from '../api/auth'
|
||||
|
||||
export interface User {
|
||||
id: string
|
||||
@ -59,6 +59,17 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
}
|
||||
|
||||
const initAuth = async () => {
|
||||
// 检查user_id是否为Guest(cookie过期时后端会设置为Guest)
|
||||
const cookies = new URLSearchParams(document.cookie.split('; ').join('&'))
|
||||
const userId = cookies.get('user_id')
|
||||
if (userId === 'Guest') {
|
||||
// user_id是Guest,说明cookie已过期,清除本地状态
|
||||
if (isAuthenticated.value) {
|
||||
await logout()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 首先检查session cookie是否存在
|
||||
if (!isCookieExpired()) {
|
||||
try {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user