import { defineStore } from "pinia"; import { ref, computed } from "vue"; import { createAuthClient } from "better-auth/vue"; const authClient = createAuthClient(); export const useAuthStore = defineStore("useAuthStore", () => { const session = ref> | null>(null); const lastError = ref(undefined); async function init() { const data = await authClient.useSession(useFetch); session.value = data; lastError.value = undefined; } const user = computed(() => session.value?.data?.user); const loading = computed(() => session.value?.isPending); async function signIn(email: string, password: string) { const { error } = await authClient.signIn.email({ email, password, callbackURL: "/", }); if (error) { lastError.value = error.message; } } async function signOut() { await authClient.signOut({}); navigateTo("/"); } return { init, lastError, loading, signIn, signOut, user, }; });