38 lines
827 B
TypeScript
38 lines
827 B
TypeScript
import { defineStore } from 'pinia';
|
|
import PocketBase from 'pocketbase';
|
|
import { ref } from 'vue';
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
const pb = new PocketBase(import.meta.env.VITE_POCKETBASE_URL);
|
|
const user = ref(pb.authStore.model);
|
|
|
|
pb.authStore.onChange(() => {
|
|
user.value = pb.authStore.model;
|
|
});
|
|
|
|
async function login(email: string, password: string) {
|
|
await pb.collection('users').authWithPassword(email, password);
|
|
}
|
|
|
|
async function register(email: string, password: string, passwordConfirm: string) {
|
|
await pb.collection('users').create({
|
|
email,
|
|
password,
|
|
passwordConfirm,
|
|
});
|
|
await login(email, password);
|
|
}
|
|
|
|
function logout() {
|
|
pb.authStore.clear();
|
|
}
|
|
|
|
return {
|
|
pb,
|
|
user,
|
|
login,
|
|
register,
|
|
logout,
|
|
};
|
|
});
|