114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import {Account, Avatars, Client, Databases, OAuthProvider, Query} from "react-native-appwrite";
|
|
import * as Linking from "expo-linking"
|
|
import {openAuthSessionAsync} from "expo-web-browser";
|
|
import {attribute} from "postcss-selector-parser";
|
|
import limit from "ajv-formats/src/limit";
|
|
|
|
export const config = {
|
|
platform: 'com.placebomk.mobilemk',
|
|
endpoint: process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT,
|
|
projectId: process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID,
|
|
databaseId: process.env.EXPO_PUBLIC_APPWRITE_DATABASE_ID,
|
|
oglasuvacId: process.env.EXPO_PUBLIC_APPWRITE_OGLASUVAC_COLLECTION_ID,
|
|
galleryId: process.env.EXPO_PUBLIC_APPWRITE_GALERIES_COLLECTION_ID,
|
|
reviewId: process.env.EXPO_PUBLIC_APPWRITE_REVIEWS_COLLECTION_ID,
|
|
vehicleId: process.env.EXPO_PUBLIC_APPWRITE_VEHICLE_COLLECTION_ID,
|
|
}
|
|
|
|
export const client = new Client();
|
|
|
|
client
|
|
.setEndpoint(config.endpoint!)
|
|
.setProject(config.projectId!)
|
|
.setPlatform(config.platform!)
|
|
|
|
export const avatar = new Avatars(client);
|
|
export const account = new Account(client);
|
|
export const databases = new Databases(client);
|
|
|
|
export async function login (){
|
|
// Your code here to authenticate the user and return their JWT token.
|
|
try {
|
|
const redirectUri = Linking.createURL('/');
|
|
const response = await account.createOAuth2Token(OAuthProvider.Google, redirectUri);
|
|
|
|
if(!response) throw new Error('failed to login');
|
|
|
|
const browserResult = await openAuthSessionAsync(
|
|
response.toString(),
|
|
redirectUri
|
|
)
|
|
|
|
if (!browserResult.type || browserResult.type !== 'success') {
|
|
throw new Error('failed to log in');
|
|
}
|
|
|
|
const url = new URL(browserResult.url);
|
|
|
|
const secret = url.searchParams.get('secret')?.toString();
|
|
const userId = url.searchParams.get('userId')?.toString();
|
|
|
|
if (!secret ||!userId) {
|
|
throw new Error('failed to authenticate user');
|
|
}
|
|
|
|
const session = await account.createSession(userId, secret);
|
|
if (!session) throw new Error('failed to create session');
|
|
return true;
|
|
}
|
|
catch (error) {
|
|
console.error("Failed to authenticate user:", error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function getCurrentUser() {
|
|
try {
|
|
const response = await account.get();
|
|
if(response.$id){
|
|
const userAvatar = avatar.getInitials(response.name)
|
|
return {
|
|
...response,
|
|
avatar: userAvatar.toString(),
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(error)
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function logOut(): Promise<boolean> {
|
|
try {
|
|
const result = await account.deleteSession('current');
|
|
return result
|
|
} catch (error) {
|
|
console.error("Failed to log out user:", error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function getData() {
|
|
try {
|
|
const result =await databases.listDocuments(
|
|
config.databaseId!,
|
|
config.vehicleId!,
|
|
[Query.orderAsc(`$createdAt`), Query.limit(5)],
|
|
|
|
)
|
|
console.log(result.documents)
|
|
return result.documents;
|
|
}
|
|
catch (error) {
|
|
console.error("Failed to fetch data:", error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getVehicles({filter, query, limit}: {
|
|
filter?: string;
|
|
query?: string;
|
|
limit?: number;
|
|
}){
|
|
|
|
} |