Project import from github
This commit is contained in:
commit
0add58254d
179 changed files with 23756 additions and 0 deletions
45
app/stores/auth.ts
Normal file
45
app/stores/auth.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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,
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue