LLM-translator/frontend/src/stores/auth.ts

30 lines
733 B
TypeScript

import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { me as apiMe, logout as apiLogout } from '../api'
export const useAuthStore = defineStore('auth', () => {
const user_id = ref<string | null>(null)
const loaded = ref(false)
const isLoggedIn = computed(() => !!user_id.value && loaded.value)
async function check() {
if (loaded.value) return
try {
const data = await apiMe()
user_id.value = data.user_id
} catch {
user_id.value = null
} finally {
loaded.value = true
}
}
async function logout() {
await apiLogout()
user_id.value = null
window.location.hash = '#/login'
}
return { user_id, loaded, isLoggedIn, check, logout }
})