feat: 使用localStorage保存登录状态,防止刷新后退出登录
This commit is contained in:
parent
374e699949
commit
1749d13987
@ -8,6 +8,8 @@ export interface User {
|
|||||||
user_type: string
|
user_type: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'auth_user'
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
const user = ref<User | null>(null)
|
const user = ref<User | null>(null)
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
@ -23,16 +25,49 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
error?.message?.includes('Cookie已过期')
|
error?.message?.includes('Cookie已过期')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置用户状态(统一的状态更新方法,不保存到localStorage)
|
// 从 localStorage 加载用户信息
|
||||||
|
const loadUserFromStorage = (): User | null => {
|
||||||
|
try {
|
||||||
|
const stored = localStorage.getItem(STORAGE_KEY)
|
||||||
|
if (stored) {
|
||||||
|
return JSON.parse(stored)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('从 localStorage 加载用户信息失败:', error)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存用户信息到 localStorage
|
||||||
|
const saveUserToStorage = (userInfo: User) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(userInfo))
|
||||||
|
} catch (error) {
|
||||||
|
console.error('保存用户信息到 localStorage 失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清除 localStorage 中的用户信息
|
||||||
|
const clearUserFromStorage = () => {
|
||||||
|
try {
|
||||||
|
localStorage.removeItem(STORAGE_KEY)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('清除 localStorage 用户信息失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置用户状态(统一的状态更新方法,保存到localStorage)
|
||||||
const setUserState = (userInfo: User) => {
|
const setUserState = (userInfo: User) => {
|
||||||
user.value = userInfo
|
user.value = userInfo
|
||||||
isAuthenticated.value = true
|
isAuthenticated.value = true
|
||||||
|
saveUserToStorage(userInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清除用户状态
|
// 清除用户状态
|
||||||
const clearUserState = () => {
|
const clearUserState = () => {
|
||||||
user.value = null
|
user.value = null
|
||||||
isAuthenticated.value = false
|
isAuthenticated.value = false
|
||||||
|
clearUserFromStorage()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证并更新用户信息
|
// 验证并更新用户信息
|
||||||
@ -88,17 +123,28 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
const hasSessionCookie = !isCookieExpired()
|
const hasSessionCookie = !isCookieExpired()
|
||||||
const hasCookie = userId || hasSessionCookie
|
const hasCookie = userId || hasSessionCookie
|
||||||
|
|
||||||
// 如果没有cookie,清除认证状态
|
// 如果有cookie,尝试验证并获取用户信息
|
||||||
if (!hasCookie) {
|
if (hasCookie) {
|
||||||
|
const success = await validateAndUpdateUser()
|
||||||
|
if (success) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果cookie验证失败或没有cookie,尝试从localStorage恢复
|
||||||
|
const storedUser = loadUserFromStorage()
|
||||||
|
if (storedUser) {
|
||||||
|
user.value = storedUser
|
||||||
|
isAuthenticated.value = true
|
||||||
|
// 尝试在后台验证用户信息,失败也不影响当前状态
|
||||||
|
validateAndUpdateUser().catch(() => {
|
||||||
|
// 静默失败,保持localStorage中的状态
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// 既没有cookie也没有localStorage,清除认证状态
|
||||||
if (isAuthenticated.value) {
|
if (isAuthenticated.value) {
|
||||||
clearUserState()
|
clearUserState()
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果有cookie,尝试验证并获取用户信息
|
|
||||||
if (hasCookie) {
|
|
||||||
await validateAndUpdateUser()
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setInitializingAuth(false)
|
setInitializingAuth(false)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user