Skip to content

Commit

Permalink
Firestore user profile is now created on first login
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuff0 committed Jan 9, 2025
1 parent 31a2f6a commit 1d46a34
Show file tree
Hide file tree
Showing 2 changed files with 8,074 additions and 8,703 deletions.
97 changes: 83 additions & 14 deletions client/app/services/AuthStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,97 @@ import {
import { Store } from "pullstate";

import { auth, db } from "../configs/firebaseConfig";
import { doc, setDoc } from "@firebase/firestore"; // Import Firestore functions
import { doc, getDoc, setDoc } from "@firebase/firestore"; // Import Firestore functions
import { UserProfile } from "@app/types/User";

interface AuthStoreInterface {
isLoggedin: boolean;
initialized: boolean;
userAuthInfo: User | null;
userProfile: UserProfile | null;
}

export const AuthStore = new Store<AuthStoreInterface>({
isLoggedin: false,
initialized: false,
userAuthInfo: null,
userProfile: null,
});

// TODO: Must call notifyUpdateProfile if connected to socket server
export const updateUserProfile = async (userId: string, profile: UserProfile) => {
try {
const docRef = doc(db, "users", userId);
await setDoc(docRef, profile);
} catch (e) {
console.error("Error updating user profile: ", e);
}
}
// // TODO: Must call notifyUpdateProfile if connected to socket server
// export const updateUserProfile = async (userId: string, profile: UserProfile) => {
// try {
// const docRef = doc(db, "users", userId);
// await setDoc(docRef, profile);
// } catch (e) {
// console.error("Error updating user profile: ", e);
// }
// }

const unsub = onAuthStateChanged(auth, (user) => {
const unsub = onAuthStateChanged(auth, async (user) => {
console.log("onAuthStateChanged", user);

let userProfile: UserProfile | null = null;

if (!!user) {
// User is signed in

try {
const docRef = doc(db, "users", user.uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
userProfile = docSnap.data() as UserProfile;
} else {
// Create user profile if not exists
await setDoc(docRef, userProfile = {
displayName: user.displayName || "New User",
profilePicture: 0,
});
}
} catch (e) {
console.error("Error getting user profile: ", e);
}

} else {
// User is signed out
}

AuthStore.update((store) => {
(store.initialized = true),
(store.isLoggedin = !!user),
(store.userAuthInfo = user);
store.initialized = true;
store.isLoggedin = !!user;
store.userAuthInfo = user;
store.userProfile = userProfile;
});
});

export const appSignIn = async (email: string, password: string) => {
try {
const response = await signInWithEmailAndPassword(auth, email, password);

let userProfile: UserProfile | null = null;

try {
const docRef = doc(db, "users", response.user.uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
userProfile = docSnap.data() as UserProfile;
} else {
// Create user profile if not exists
await setDoc(docRef, userProfile = {
displayName: response.user.displayName || "New User",
profilePicture: 0,
});
}
} catch (e) {
console.error("Error getting user profile: ", e);
}

AuthStore.update((store) => {
store.userAuthInfo = response?.user;
store.isLoggedin = !!response?.user;
store.userProfile = userProfile;
});

return { user: auth.currentUser };
} catch (e) {
return { error: e };
Expand Down Expand Up @@ -76,9 +126,28 @@ export const appSignUp = async (email: string, password: string) => {
password,
);

let userProfile: UserProfile | null = null;

try {
const docRef = doc(db, "users", response.user.uid);
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
userProfile = docSnap.data() as UserProfile;
} else {
// Create user profile if not exists
await setDoc(docRef, userProfile = {
displayName: response.user.displayName || "New User",
profilePicture: 0,
});
}
} catch (e) {
console.error("Error getting user profile: ", e);
}

AuthStore.update((store) => {
store.userAuthInfo = response.user;
store.isLoggedin = !!response.user;
store.userProfile = userProfile;
});

return { user: auth.currentUser };
Expand Down
Loading

0 comments on commit 1d46a34

Please sign in to comment.