diff --git a/src/stores/auth.ts b/src/stores/auth.ts new file mode 100644 index 0000000..38a560a --- /dev/null +++ b/src/stores/auth.ts @@ -0,0 +1,31 @@ +import { defineStore } from 'pinia'; +import { ID, account, client } from 'boot/appwrite'; +import type { Models } from 'appwrite'; +import { ref } from 'vue'; + +export const useAuthStore = defineStore('auth', () => { + const currentUser = ref | null>(null); + + async function init() { + try { + currentUser.value = await account.get(); + } catch { + return (currentUser.value = null); + } + } + + async function register(email: string, password: string) { + await account.create(ID.unique(), email, password); + return await login(email, password); + } + async function login(email: string, password: string) { + await account.createEmailSession(email, password); + currentUser.value = await account.get(); + } + + function logout() { + return account.deleteSession('current').then((currentUser.value = null)); + } + + return { currentUser, register, login, logout, init }; +});