diff --git a/src/ConnectionWallet.tsx b/src/ConnectionWallet.tsx index 343f53b..b01dac5 100644 --- a/src/ConnectionWallet.tsx +++ b/src/ConnectionWallet.tsx @@ -123,7 +123,7 @@ function ConnectionWallet(): JSX.Element { .then((resp) => console.log(resp)) .catch((error) => console.log(error)); - await getUserData(account, ""); + await getUserData(account); let ethBalance = await web3.eth.getBalance(account); setEthereumAccount(account); diff --git a/src/RainbowWallet.tsx b/src/RainbowWallet.tsx index 5a4c629..7e51fbc 100644 --- a/src/RainbowWallet.tsx +++ b/src/RainbowWallet.tsx @@ -29,7 +29,7 @@ import { ConnectButton } from "@rainbow-me/rainbowkit"; import { useEffect, useCallback, useState, useMemo } from "react"; import { UserContext } from "./context/ContextProvider"; -import { getUserData } from "./firebase/firebaseFunctions"; +import { addUserData, getUserData } from "./firebase/firebaseFunctions"; import { Button } from "@mui/material"; import { ExpandMore } from "@mui/icons-material"; import { toast } from "react-toastify"; @@ -153,17 +153,19 @@ export default function RainbowWallet() { } = await verifyRes.json(); if (newVerifyRes.status) { - return newVerifyRes; + return { + type: "success", + status: newVerifyRes.status, + data: newVerifyRes.data, + }; } } } } } catch (error: any) { toast.error("User rejected signing"); + return { type: "error", userRejected: true }; } - // return { - // status: false, - // }; }, [provider]); const signOutWallet = useCallback(() => { @@ -182,69 +184,76 @@ export default function RainbowWallet() { }; }); }, [setStore]); - const connectMetamaskWallet = useCallback( - async (address: string) => { + async (address: string, chain: string) => { try { - if (address !== undefined && chain?.unsupported !== undefined) { + if (address !== undefined && chain !== undefined) { console.log("execution"); - const firebaseResponse = await getUserData(address, chain?.name); - if (firebaseResponse.id) { - setAuthState(true); + // Check if the user data already exists in Firestore. + const firebaseResponse = await getUserData(address); + + // If the user exists and has an id, return the user data. + if (firebaseResponse?.id) { return firebaseResponse; + } else { + // If the user doesn't exist and the sign in is confirmed, add the user to Firestore. + const signInResponse = await signInWithEthereum(); + + // Only if sign-in is successful and not rejected by the user, add user to database + if (!signInResponse?.userRejected && signInResponse?.status) { + const newUserResponse = await addUserData(address, chain); + if (newUserResponse?.id) { + setAuthState(true); + return newUserResponse; + } + } } } - } catch (err) { + } catch (err: any) { console.log(err); alert(`Something went wrong: ${err}`); } }, - [chain?.name, chain?.unsupported] + [signInWithEthereum] ); useEffect(() => { if ( address !== undefined && - store?.user?.walletAddress === "" + store?.user?.walletAddress === "" && + chain?.name !== undefined // Check if chain?.name is not undefined // authState ) { console.log("line 166, useEffect"); - connectMetamaskWallet(address).then((resp: any) => { + connectMetamaskWallet(address, chain.name).then((resp: any) => { + // chain.name will be string here console.log("firebase: ", resp); if (resp?.walletAddress) { - signInWithEthereum() - .then((resEth) => { - console.log(resEth); - - setStore((prev: any) => { - return { - ...prev, - user: { - walletAddress: resp?.walletAddress - ? resp?.walletAddress - : "", - chain: resp?.chain ? resp?.chain : "", - id: resp?.id ? resp?.id : "", - isVerified: resp?.isVerified, - }, - }; - }); - - sessionStorage.setItem( - "walletAddress", - resp?.walletAddress ? resp?.walletAddress : "" - ); - sessionStorage.setItem("chain", resp?.chain ? resp?.chain : ""); - sessionStorage.setItem("id", resp?.id ? resp?.id : ""); - - sessionStorage.setItem( - "isVerified", - resp?.isVerified ? "true" : "false" - ); - }) - .catch((err) => console.log(err)); + setStore((prev: any) => { + return { + ...prev, + user: { + walletAddress: resp?.walletAddress || "", + chain: resp?.chain || "", + id: resp?.id || "", + isVerified: resp?.isVerified || false, + }, + }; + }); + + sessionStorage.setItem( + "walletAddress", + resp?.walletAddress ? resp?.walletAddress : "" + ); + sessionStorage.setItem("chain", resp?.chain ? resp?.chain : ""); + sessionStorage.setItem("id", resp?.id ? resp?.id : ""); + + sessionStorage.setItem( + "isVerified", + resp?.isVerified ? "true" : "false" + ); } }); @@ -265,6 +274,7 @@ export default function RainbowWallet() { signInWithEthereum, signOutWallet, store?.user?.walletAddress, + chain?.name, ]); return ( diff --git a/src/firebase/firebaseFunctions.ts b/src/firebase/firebaseFunctions.ts index 0d6f756..4efa956 100644 --- a/src/firebase/firebaseFunctions.ts +++ b/src/firebase/firebaseFunctions.ts @@ -36,20 +36,14 @@ async function existsWalletAddress(walletAddress: string) { * @param {function} handleUserData */ async function getUserData( - walletAddress: string, - chain: string -): Promise { - let getDataReturnObj: authUserType = { - walletAddress: "", - chain: "", - id: "", - isVerified: false, - }; - + walletAddress: string +): Promise { try { const docRef = collection(db, "user"); const userSnapshot = await getDocs(docRef); + let getDataReturnObj: authUserType | null = null; + userSnapshot.forEach((shot) => { if ( shot && @@ -66,32 +60,42 @@ async function getUserData( } }); - if (!getDataReturnObj.id) { - const addUserCollection = collection(db, "user"); + return getDataReturnObj; + } catch (error) { + console.error("Error fetching user data: ", error); + return null; + } +} - const addUserSnapshot = await addDoc(addUserCollection, { +async function addUserData( + walletAddress: string, + chain: string +): Promise { + try { + const addUserCollection = collection(db, "user"); + + const addUserSnapshot = await addDoc(addUserCollection, { + walletAddress: walletAddress, + chain: chain, + isVerified: true, + createdAt: Timestamp.now(), + }); + + if (addUserSnapshot && addUserSnapshot.id) { + return { walletAddress: walletAddress, chain: chain, + id: addUserSnapshot.id, isVerified: true, - createdAt: Timestamp.now(), - }); - - if (addUserSnapshot && addUserSnapshot.id) { - getDataReturnObj = { - walletAddress: walletAddress, - chain: chain, - id: addUserSnapshot.id, - isVerified: true, - }; - } else { - console.log("No data is saved"); - } + }; + } else { + console.log("No data is saved"); + return null; } } catch (error) { - console.error("Error fetching user data: ", error); + console.error("Error adding user data: ", error); + return null; } - - return getDataReturnObj; } /** @@ -357,6 +361,7 @@ async function checkPaperExecutionState(id: string) { export { existsEmail, getSelectUserPaperData, + addUserData, deleteUserPaperData, getUserPaperData, addUserPaperData,