Add missing file
This commit is contained in:
31
src/stores/auth.ts
Normal file
31
src/stores/auth.ts
Normal file
@@ -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<Models.User<Models.Preferences> | 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 };
|
||||
});
|
||||
Reference in New Issue
Block a user