web / stores /user.ts
hanbinsite's picture
feat: implement all web pages with player, search, favorites, auth, useApi composable
dfa6264
Raw History Blame Contribute Delete
716 Bytes
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 }
})