File size: 716 Bytes
d003a1c
dfa6264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d003a1c
 
dfa6264
 
d003a1c
 
dfa6264
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export const useUserStore = defineStore("user", () => {
  const token = useCookie<string | null>("binv_token", { maxAge: 60 * 60 * 24 * 7 })
  const userInfo = ref<{
    id: number
    username: string | null
    nickname: string | null
    avatar: string | null
    email: string | null
  } | null>(null)
  const isLoggedIn = computed(() => !!token.value)

  async function fetchProfile() {
    if (!token.value) return
    try {
      const { get } = useApi()
      userInfo.value = await get<any>("/user/auth/profile")
    } catch {
      userInfo.value = null
    }
  }

  function logout() {
    token.value = null
    userInfo.value = null
  }

  return { token, userInfo, isLoggedIn, fetchProfile, logout }
})