Files
bab-app/src/boot/appwrite.ts

102 lines
2.6 KiB
TypeScript

import { boot } from 'quasar/wrappers';
import { Client, Account, Databases, ID } from 'appwrite';
import { useAuthStore } from 'src/stores/auth';
import { Dialog, Notify } from 'quasar';
import type { Router } from 'vue-router';
const client = new Client();
// appwrite.io SaaS
// client
// .setEndpoint('https://api.bab.toal.ca/v1')
// .setProject('653ef6f76baf06d68034');
// const appDatabaseId = '654ac5044d1c446feb71';
// Private self-hosted appwrite
if (process.env.APPWRITE_API_ENDPOINT && process.env.APPWRITE_API_PROJECT)
client
.setEndpoint(process.env.APPWRITE_API_ENDPOINT)
.setProject(process.env.APPWRITE_API_PROJECT);
//TODO move this to config file
const AppwriteIds = {
databaseId: '65ee1cbf9c2493faf15f',
collection: {
task: '65ee1cd5b550023fae4f',
taskTags: '65ee21d72d5c8007c34c',
skillTags: '66072582a74d94a4bd01',
boat: '66341910003e287cd71c',
timeBlock: '66361869002883fb4c4b',
timeBlockTemplate: '66361f480007fdd639af',
},
};
const account = new Account(client);
const databases = new Databases(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' });
});
}
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();
authStore
.login(email, password)
.then(() => {
notification({
type: 'positive',
message: 'Logged in!',
timeout: 2000,
spinner: false,
icon: 'check_circle',
});
console.log('Redirecting to index page');
appRouter.replace({ name: 'index' });
})
.catch(function (reason: Error) {
notification({
type: 'negative',
message: 'Login failed.',
timeout: 1,
});
Dialog.create({
title: 'Login Error!',
message: reason.message,
persistent: true,
});
});
}
export { client, account, databases, ID, AppwriteIds, login, logout };