Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create UserConfig Collection after User Signup #271

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion client/app/configs/firebaseConfig.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -31,4 +33,5 @@ if (!getApps().length) {
auth = getAuth();
}

export const db = getFirestore(); //Export Const DB for UserConfig
export { app, auth };
32 changes: 31 additions & 1 deletion client/app/services/AuthStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
} 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;
Expand All @@ -21,10 +22,34 @@
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) => {

Check warning on line 49 in client/app/services/AuthStore.ts

View workflow job for this annotation

GitHub Actions / lint (21.x)

'unsub' is assigned a value but never used
console.log("onAuthStateChanged", user);
AuthStore.update((store) => {
(store.initialized = true),

Check warning on line 52 in client/app/services/AuthStore.ts

View workflow job for this annotation

GitHub Actions / lint (21.x)

Expected an assignment or function call and instead saw an expression
(store.isLoggedin = !!user),
(store.userAuthInfo = user);
});
Expand Down Expand Up @@ -68,6 +93,11 @@
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 };
Expand Down
1 change: 1 addition & 0 deletions client/config_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading