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

partial fixis need to fix dataa storage #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/ConnectionWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
102 changes: 56 additions & 46 deletions src/RainbowWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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"
);
}
});

Expand All @@ -265,6 +274,7 @@ export default function RainbowWallet() {
signInWithEthereum,
signOutWallet,
store?.user?.walletAddress,
chain?.name,
]);

return (
Expand Down
63 changes: 34 additions & 29 deletions src/firebase/firebaseFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ async function existsWalletAddress(walletAddress: string) {
* @param {function} handleUserData
*/
async function getUserData(
walletAddress: string,
chain: string
): Promise<authUserType> {
let getDataReturnObj: authUserType = {
walletAddress: "",
chain: "",
id: "",
isVerified: false,
};

walletAddress: string
): Promise<authUserType | null> {
try {
const docRef = collection(db, "user");
const userSnapshot = await getDocs(docRef);

let getDataReturnObj: authUserType | null = null;

userSnapshot.forEach((shot) => {
if (
shot &&
Expand All @@ -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<authUserType | null> {
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;
}

/**
Expand Down Expand Up @@ -357,6 +361,7 @@ async function checkPaperExecutionState(id: string) {
export {
existsEmail,
getSelectUserPaperData,
addUserData,
deleteUserPaperData,
getUserPaperData,
addUserPaperData,
Expand Down