import { boot } from 'quasar/wrappers'; import { Client, Account, Databases, Functions, ID, AppwriteException, Teams, } from 'appwrite'; import { useAuthStore } from 'src/stores/auth'; import { Dialog, Notify } from 'quasar'; import type { Router } from 'vue-router'; const client = new Client(); let APPWRITE_API_ENDPOINT, APPWRITE_API_PROJECT; // Private self-hosted appwrite if (process.env.APPWRITE_API_ENDPOINT && process.env.APPWRITE_API_PROJECT) { APPWRITE_API_ENDPOINT = process.env.APPWRITE_API_ENDPOINT; APPWRITE_API_PROJECT = process.env.APPWRITE_API_PROJECT; } else if (process.env.DEV) { APPWRITE_API_ENDPOINT = 'http://localhost:4000/api/v1'; APPWRITE_API_PROJECT = '65ede55a213134f2b688'; } else { APPWRITE_API_ENDPOINT = 'https://appwrite.oys.undock.ca/v1'; APPWRITE_API_PROJECT = 'bab'; } client.setEndpoint(APPWRITE_API_ENDPOINT).setProject(APPWRITE_API_PROJECT); const pwresetUrl = process.env.DEV ? 'http://localhost:4000/pwreset' : 'https://oys.undock.ca/pwreset'; const AppwriteIds = process.env.DEV ? { databaseId: '65ee1cbf9c2493faf15f', collection: { boat: 'boat', reservation: 'reservation', skillTags: 'skillTags', task: 'task', taskTags: 'taskTags', interval: 'interval', intervalTemplate: 'intervalTemplate', }, function: { userinfo: 'userinfo', }, } : { databaseId: 'bab_prod', collection: { boat: 'boat', reservation: 'reservation', skillTags: 'skillTags', task: 'task', taskTags: 'taskTags', interval: 'interval', intervalTemplate: 'intervalTemplate', }, function: { userinfo: '664038294b5473ef0c8d', }, }; const account = new Account(client); const databases = new Databases(client); const functions = new Functions(client); const teams = new Teams(client); let appRouter: Router; export default boot(async ({ router }) => { // Initialize store const authStore = useAuthStore(); await authStore.init(); appRouter = router; }); async function logout() { Dialog.create({ title: 'Logout', message: 'Are you sure?', cancel: true, persistent: true, }).onOk(async () => { const authStore = useAuthStore(); await authStore.logout(); Notify.create({ message: 'Logged out!', type: 'warning', position: 'top', timeout: 2000, group: false, }); appRouter.replace({ name: 'login' }); }); } async function login(email: string, password: string) { const notification = Notify.create({ type: 'primary', position: 'top', spinner: true, message: 'Logging you in...', timeout: 8000, group: false, }); const authStore = useAuthStore(); try { await authStore.login(email, password); notification({ type: 'positive', message: 'Logged in!', timeout: 2000, spinner: false, icon: 'check_circle', }); appRouter.replace({ name: 'index' }); } catch (error: unknown) { if (error instanceof AppwriteException) { if (error.type === 'user_session_already_exists') { appRouter.replace({ name: 'index' }); notification({ type: 'positive', message: 'Already Logged in!', timeout: 2000, spinner: false, icon: 'check_circle', }); return; } Dialog.create({ title: 'Login Error!', message: error.message, persistent: true, }); } notification({ type: 'negative', message: 'Login failed.', timeout: 2000, }); } } async function resetPassword(email: string) { await account.createRecovery(email, pwresetUrl); } export { client, account, teams, databases, functions, ID, AppwriteIds, login, logout, resetPassword, };