import {Client, Account, ID, Avatars, Databases, Query } from 'react-native-appwrite'; export const appwriteConfig = { endpoint: 'https://cloud.appwrite.io/v1', platform: 'com.rsrg.Resurgence', projectId: '66f420ab002eadba56c9', databaseId: '66f4274a002a4d44a4de', userCollectionId: '66f4283c0000cfea13a5', videoCollectionId: '66f42898002f1ede07a7', storageId: '66f42a32000fa3b655de', } const client = new Client(); client .setEndpoint(appwriteConfig.endpoint) .setProject(appwriteConfig.projectId) .setPlatform(appwriteConfig.platform); const account = new Account(client); const avatars = new Avatars(client); const databases = new Databases(client); export async function createUser(email, password, username) { try { const newAccount = await account.create( ID.unique(), email, password, username ); if (!newAccount) throw Error; const avatarUrl = avatars.getInitials(username); await signIn(email, password); const newUser = await databases.createDocument( appwriteConfig.databaseId, appwriteConfig.userCollectionId, ID.unique(), { accountId: newAccount.$id, email: email, username: username, avatar: avatarUrl, } ); return newUser; } catch (error) { throw new Error(error); } } // Sign In export const signIn = async (email, password) => { try { const session = await account.createEmailPasswordSession(email, password); return session; } catch (error) { throw new Error(error); } } export const getCurrentUser = async () => { try { const currentAccount = await account.get(); if (!currentAccount) throw Error; const currentUser = await databases.listDocuments( appwriteConfig.databaseId, appwriteConfig.userCollectionId, [Query.equal('accountId', currentAccount.$id)] ) if (!currentUser) throw Error; return currentUser[0]; } catch (error) { console.log(error); } }; export async function getAccount() { try { const currentAccount = await account.get(); return currentAccount; } catch (error) { throw new Error(error); } } export const getAllPosts = async () => { try { const posts = await databases.listDocuments( databaseId, videoCollectionId, ) return posts.documents; } catch (error) { throw new Error(error); } }; export async function signOut() { try { const session = await account.deleteSession("current"); return session; } catch (error) { throw new Error(error); } }