diff --git a/client/app/configs/firebaseConfig.ts b/client/app/configs/firebaseConfig.ts index ed93ff380..e8308ecbe 100644 --- a/client/app/configs/firebaseConfig.ts +++ b/client/app/configs/firebaseConfig.ts @@ -1,4 +1,4 @@ -import { API_KEY, AUTH_DOMAIN } from "@env"; // Don't worry about this env error! +import { API_KEY, AUTH_DOMAIN, PROJECT_ID } from "@env"; // Don't worry about this env error! import AsyncStorage from "@react-native-async-storage/async-storage"; import { initializeApp, getApp, getApps } from "@firebase/app"; import { @@ -7,10 +7,12 @@ import { getAuth, Auth, } from "@firebase/auth"; +import { getFirestore } from "@firebase/firestore"; const firebaseConfig = { apiKey: API_KEY || "Mock-Key", authDomain: AUTH_DOMAIN, + projectId: PROJECT_ID, }; let app; @@ -31,4 +33,5 @@ if (!getApps().length) { auth = getAuth(); } +export const db = getFirestore(); //Export Const DB for UserConfig export { app, auth }; diff --git a/client/app/services/AuthStore.ts b/client/app/services/AuthStore.ts index 108e3d558..5d81ff216 100644 --- a/client/app/services/AuthStore.ts +++ b/client/app/services/AuthStore.ts @@ -7,7 +7,8 @@ import { } from "@firebase/auth"; import { Store } from "pullstate"; -import { auth } from "../configs/firebaseConfig"; +import { auth , db } from "../configs/firebaseConfig"; +import { doc , setDoc } from "@firebase/firestore"; // Import Firestore functions interface AuthStoreInterface { isLoggedin: boolean; @@ -21,6 +22,30 @@ export const AuthStore = new Store({ userAuthInfo: null, }); +const createUserConfig = async (userId: string) => { + try { + const docRef = doc(db, "UserConfigs", userId); + + await setDoc(docRef, { + // TODO: create a matching UserConfig type in the app/types folder. + // In documentation: make explicit that the key for UserConfig documents is the same as a uid from the user auth collection. + isConnected: false, + lastConnectionTime: "", + displayName: "", + userIcon: { + imageType: 0, + color: "#02efdb" + }, + darkMode: false, + notificationsEnabled: false, + language: "English", + + }); + } catch (e){ + console.error("Error creating UserConfig: ", e); + } +} + const unsub = onAuthStateChanged(auth, (user) => { console.log("onAuthStateChanged", user); AuthStore.update((store) => { @@ -68,6 +93,11 @@ export const appSignUp = async (email: string, password: string) => { store.userAuthInfo = response.user; store.isLoggedin = !!response.user; }); + + if (response.user) { + await createUserConfig(response.user.uid); + } + return { user: auth.currentUser }; } catch (e) { return { error: e }; diff --git a/client/config_example.md b/client/config_example.md index 23e5d4fb3..ff47af161 100644 --- a/client/config_example.md +++ b/client/config_example.md @@ -14,3 +14,4 @@ LOCATION_REFRESH_RATE=3000 API_KEY = place_your_apiKey_here AUTH_DOMAIN = place_your_authDomain_here +PROJECT_ID = place_your_projectId_here