Some checks failed
Production PR / QA Tests (pull_request) Failing after 13s
45 lines
1 KiB
TypeScript
45 lines
1 KiB
TypeScript
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<Awaited<ReturnType<typeof authClient.useSession>> | null>(null);
|
|
const lastError = ref<string | undefined>(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,
|
|
};
|
|
});
|