fix: login persistence, username login, realtime refresh, group name uniqueness
- Remove loadFromCookie that overwrites valid localStorage auth data - Set user status to idle on first login (was empty string) - Default empty status to idle instead of away Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -5,13 +5,7 @@ const pbUrl = import.meta.env.VITE_PB_URL || window.location.origin
|
|||||||
|
|
||||||
export const pb = new PocketBase(pbUrl)
|
export const pb = new PocketBase(pbUrl)
|
||||||
|
|
||||||
// 认证状态持久化
|
// SDK v0.21+ 自动使用 localStorage 持久化,无需手动 cookie 操作
|
||||||
pb.authStore.loadFromCookie(document.cookie)
|
|
||||||
|
|
||||||
// 保存认证状态到 cookie
|
|
||||||
pb.authStore.onChange(() => {
|
|
||||||
document.cookie = pb.authStore.exportToCookie({ httpOnly: false, path: '/' })
|
|
||||||
})
|
|
||||||
|
|
||||||
// 获取当前用户
|
// 获取当前用户
|
||||||
export function getCurrentUser() {
|
export function getCurrentUser() {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
|
|
||||||
// 计算属性
|
// 计算属性
|
||||||
const isLoggedIn = computed(() => isAuthenticated() && user.value !== null)
|
const isLoggedIn = computed(() => isAuthenticated() && user.value !== null)
|
||||||
const userStatus = computed(() => user.value?.status || 'away')
|
const userStatus = computed(() => user.value?.status || 'idle')
|
||||||
const userId = computed(() => user.value?.id || '')
|
const userId = computed(() => user.value?.id || '')
|
||||||
|
|
||||||
// 初始化用户信息
|
// 初始化用户信息
|
||||||
@@ -41,6 +41,10 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
await pb.collection('users').authWithPassword(email, password)
|
await pb.collection('users').authWithPassword(email, password)
|
||||||
await initUser()
|
await initUser()
|
||||||
|
// 首次登录设置默认状态为空闲
|
||||||
|
if (user.value && !user.value.status) {
|
||||||
|
await setStatus('idle')
|
||||||
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error('邮箱或密码错误')
|
throw new Error('邮箱或密码错误')
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user