[FEAT] Auth. and user projects

This commit is contained in:
2026-01-01 18:23:42 +01:00
parent 8e1b5fa77c
commit 7e51896d00
11 changed files with 590 additions and 16 deletions

View 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,
};
});

View File

@@ -0,0 +1,130 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useAuthStore } from './useAuthStore';
export interface Project {
id: string;
name: string;
data: any; // Store the JSON export of the project here
created: string;
updated: string;
}
export const useProjectStore = defineStore('project', () => {
const authStore = useAuthStore();
const projects = ref<Project[]>([]);
const currentProject = ref<Project | null>(null);
const isLoading = ref(false);
async function fetchProjects() {
if (!authStore.user) return;
isLoading.value = true;
try {
const records = await authStore.pb.collection('projects').getList(1, 50, {
sort: '-updated',
});
projects.value = records.items.map((r: any) => ({
id: r.id,
name: r.name,
data: r.data,
created: r.created,
updated: r.updated,
}));
} catch (error) {
console.error('Failed to fetch projects:', error);
} finally {
isLoading.value = false;
}
}
async function createProject(name: string, data: any) {
if (!authStore.user) return;
isLoading.value = true;
try {
const record = await authStore.pb.collection('projects').create({
name,
data,
user: authStore.user.id,
});
currentProject.value = {
id: record.id,
name: record.name,
data: record.data,
created: record.created,
updated: record.updated,
};
await fetchProjects();
} catch (error) {
console.error('Failed to create project:', error);
throw error;
} finally {
isLoading.value = false;
}
}
async function updateProject(id: string, data: any) {
if (!authStore.user) return;
isLoading.value = true;
try {
const record = await authStore.pb.collection('projects').update(id, {
data,
});
currentProject.value = { ...currentProject.value!, data: record.data, updated: record.updated };
await fetchProjects();
} catch (error) {
console.error('Failed to update project:', error);
throw error;
} finally {
isLoading.value = false;
}
}
async function loadProject(id: string) {
if (!authStore.user) return;
isLoading.value = true;
try {
const record = await authStore.pb.collection('projects').getOne(id);
currentProject.value = {
id: record.id,
name: record.name,
data: record.data,
created: record.created,
updated: record.updated
};
} catch (error) {
console.error("Failed to load project", error);
throw error;
} finally {
isLoading.value = false;
}
}
async function deleteProject(id: string) {
if (!authStore.user) return;
if (!confirm('Are you sure you want to delete this project?')) return;
isLoading.value = true;
try {
await authStore.pb.collection('projects').delete(id);
if (currentProject.value?.id === id) {
currentProject.value = null;
}
await fetchProjects();
} catch (error) {
console.error('Failed to delete project:', error);
} finally {
isLoading.value = false;
}
}
return {
projects,
currentProject,
isLoading,
fetchProjects,
createProject,
updateProject,
loadProject,
deleteProject
};
});