Skip to content

Commit

Permalink
feat(backend): create client-side appwrite utils
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVinck committed Oct 26, 2023
1 parent fb388be commit 9d465d4
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
"@astrojs/sitemap": "^3.0.0",
"@astrojs/solid-js": "^3.0.1",
"@astrojs/tailwind": "^5.0.0",
"@nanostores/solid": "^0.4.2",
"appwrite": "^13.0.0",
"astro": "^3.2.0",
"fuse.js": "^6.6.2",
"nanostores": "^0.9.4",
"node-appwrite": "^11.0.0",
"solid-js": "^1.7.3",
"tailwindcss": "^3.3.1"
Expand Down
143 changes: 143 additions & 0 deletions src/lib/appwrite.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
Account,
AppwriteException,
Client,
Databases,
Storage,
ID,
type Models,
Teams,
Query,
} from "appwrite";
import { action, atom, type WritableAtom } from "nanostores";

// Adapted the code here from https://github.com/appwrite/demos-for-astro/blob/main/example-blog/src/lib/appwrite.ts

/** Setup */
export const appwriteClient = new Client()
.setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT) // Your API Endpoint
.setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID); // Your project ID

export const appwriteDatabases = new Databases(appwriteClient);
export const appwriteStorage = new Storage(appwriteClient);
export const appwriteTeams = new Teams(appwriteClient);

/** Account */
export const $isLoggedIn: WritableAtom<undefined | Models.Session> =
atom(undefined);
export const setIsLoggedIn = action(
$isLoggedIn,
"setIsLoggedIn",
(store, session: Models.Session | undefined) => {
store.set(session);
}
);

export const $user: WritableAtom<undefined | Models.User<Models.Preferences>> =
atom(undefined);
export const setUser = action(
$user,
"setUser",
(store, session: Models.User<Models.Preferences>) => {
store.set(session);
}
);

export const $isAuthor: WritableAtom<boolean> = atom(false);
export const setIsAuthor = action(
$isAuthor,
"setIsAuthor",
(store, userIsAnAuthor: boolean) => {
store.set(userIsAnAuthor);
}
);

/** Database */
export interface BlogPost extends Models.Document {
title: string;
description: string;
content: string;
slug: string;
imageUrl: string;
}
export interface BlogPostList extends Models.DocumentList<BlogPost> {}

export interface BlogComment extends Models.Document {
postId: string;
content: string;
}
export interface BlogCommentList extends Models.DocumentList<BlogComment> {}

/** Account */
export const appwriteAccount = new Account(appwriteClient);

// Check for session
appwriteAccount.getSession("current").then(
function (response) {
setIsLoggedIn(response);
},
function (_error) {
setIsLoggedIn(undefined);
}
);

$isLoggedIn.subscribe(async (session) => {
if (session?.userId) {
$user.set(await account());
}
});

$isLoggedIn.subscribe(async (session) => {
if (!session?.userId) return;
const authorsMemberships = await appwriteTeams.listMemberships("authors", [
Query.equal("userId", session.userId),
]);
$isAuthor.set(authorsMemberships.total > 0);
});

export const login = async (email: string, password: string) => {
try {
const session = await appwriteAccount.createEmailSession(email, password);
setIsLoggedIn(session);
window.location.href = "/account";
} catch (error) {
const appwriteError = error as AppwriteException;
alert(appwriteError.message);
}
};

export const logout = async () => {
try {
const session = $isLoggedIn.get();
if (session?.$id) {
await appwriteAccount.deleteSession(session?.$id);
setIsLoggedIn(undefined);
window.location.href = "/";
}
} catch (error) {
const appwriteError = error as AppwriteException;
alert(appwriteError.message);
}
};

export const register = async (email: string, password: string) => {
try {
await appwriteAccount.create(ID.unique(), email, password);
const session = await appwriteAccount.createEmailSession(email, password);
setIsLoggedIn(session);
window.location.href = "/account";
} catch (error) {
const appwriteError = error as AppwriteException;
alert(appwriteError.message);
}
};

export const account = async () => {
try {
return appwriteAccount.get();
} catch (error) {
const appwriteError = error as AppwriteException;
alert(appwriteError.message);
return;
}
};

0 comments on commit 9d465d4

Please sign in to comment.