loged in users saved to db
This commit is contained in:
1 parent
91e5838b00
commit
0af7a5060e
4 files changed
+199
-29
No files matched your search
+76
-27
@@ -4,15 +4,26 @@ import {openAuthSessionAsync} from "expo-web-browser";
|
||||
import {attribute} from "postcss-selector-parser";
|
||||
import limit from "ajv-formats/src/limit";
|
||||
|
||||
// Add this debug log
|
||||
// console.log('Environment Variables:', {
|
||||
// endpoint: process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT,
|
||||
// projectId: process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID,
|
||||
// });
|
||||
|
||||
if (!process.env.EXPO_PUBLIC_APPWRITE_ENDPOINT || !process.env.EXPO_PUBLIC_APPWRITE_PROJECT_ID) {
|
||||
throw new Error('Missing required Appwrite environment variables');
|
||||
}
|
||||
|
||||
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,
|
||||
usersId: process.env.EXPO_PUBLIC_APPWRITE_USERS_COLLECTION_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();
|
||||
@@ -80,35 +91,73 @@ export async function getCurrentUser() {
|
||||
|
||||
export async function logOut(): Promise<boolean> {
|
||||
try {
|
||||
const result = await account.deleteSession('current');
|
||||
return result
|
||||
await account.deleteSession('current');
|
||||
return true;
|
||||
} 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)],
|
||||
// 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 [];
|
||||
// )
|
||||
// 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;
|
||||
// }){
|
||||
|
||||
// }
|
||||
|
||||
export async function saveUserToDatabase(userData: {
|
||||
userId: string;
|
||||
name: string;
|
||||
email: string;
|
||||
avatar: string;
|
||||
}) {
|
||||
try {
|
||||
// First check if user already exists
|
||||
try {
|
||||
await databases.getDocument(
|
||||
config.databaseId!,
|
||||
config.usersId!,
|
||||
userData.userId
|
||||
);
|
||||
console.log('User already exists in database');
|
||||
return null;
|
||||
} catch (error) {
|
||||
// User doesn't exist, proceed with creation
|
||||
const result = await databases.createDocument(
|
||||
config.databaseId!,
|
||||
config.usersId!,
|
||||
userData.userId,
|
||||
{
|
||||
name: userData.name,
|
||||
email: userData.email,
|
||||
avatar: userData.avatar,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
);
|
||||
console.log('User created successfully');
|
||||
return result;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to save user data:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getVehicles({filter, query, limit}: {
|
||||
filter?: string;
|
||||
query?: string;
|
||||
limit?: number;
|
||||
}){
|
||||
|
||||
}
|
||||
+15
-2
@@ -1,9 +1,10 @@
|
||||
import React, { createContext, useContext, ReactNode } from "react";
|
||||
|
||||
import React, { createContext, useContext, ReactNode, useEffect } from "react";
|
||||
import { saveUserToDatabase } from "./appwrite";
|
||||
import { getCurrentUser } from "./appwrite";
|
||||
import { useAppwrite } from "./useAppwrite";
|
||||
import { Redirect } from "expo-router";
|
||||
// import {replace} from "@remix-run/router";
|
||||
import { config } from "./appwrite";
|
||||
|
||||
interface GlobalContextType {
|
||||
isLoggedIn: boolean;
|
||||
@@ -34,6 +35,18 @@ export const GlobalProvider = ({ children }: {children: ReactNode}) => {
|
||||
fn: getCurrentUser,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (user && !loading) {
|
||||
// Save user data when they first log in
|
||||
saveUserToDatabase({
|
||||
userId: user.$id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
avatar: user.avatar,
|
||||
});
|
||||
}
|
||||
}, [user, loading]);
|
||||
|
||||
const isLoggedIn = !!user;
|
||||
console.log(JSON.stringify(user));
|
||||
|
||||
|
||||
Reference in new issue
Block a user