[FEAT] Auth. and user projects
This commit is contained in:
39
src/stores/useAuthStore.ts
Normal file
39
src/stores/useAuthStore.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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);
|
||||
|
||||
// Sync user state on change
|
||||
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,
|
||||
});
|
||||
// Auto login after register
|
||||
await login(email, password);
|
||||
}
|
||||
|
||||
function logout() {
|
||||
pb.authStore.clear();
|
||||
}
|
||||
|
||||
return {
|
||||
pb,
|
||||
user,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user