diff --git a/src/App.js b/src/App.js index c85e55d..e7c6c72 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,5 @@ -import { useState, useEffect } from "react"; +import { useState } from "react"; +import { useNavigate } from "react-router-dom"; import Dashboard from "./pages/Dashboard"; import ComplaintAnalytics from "./pages/ComplaintAnalytics"; import OverAllAnalytics from "./pages/OverAllAnalytics"; @@ -17,54 +18,50 @@ import EditProfile from "./pages/EditProfile"; import ChangeMesses from "./pages/ChangeMesses"; import Assign from "./pages/Assign"; import { auth } from "./firebaseUtils/firebaseConfig"; -import DetailsModel from "./components/DetailsModal"; -import { onAuthStateChanged } from "firebase/auth"; +// import DetailsModel from "./components/DetailsModal"; +// import { onAuthStateChanged } from "firebase/auth"; import { toast, ToastContainer } from "react-toastify"; -import { LoginPrompt } from "./components/LoginPrompt"; import SendNotice from "./pages/SendNotice"; +import { signInWithPopup, GoogleAuthProvider } from "firebase/auth"; +import { isInDb } from "./firebaseUtils/getRequests"; +const provider = new GoogleAuthProvider(); function App() { - const [shouldShowDetailsModel, setShouldShowDetailsModel] = useState(false); + // const [shouldShowDetailsModel, setShouldShowDetailsModel] = useState(false); const [user, setUser] = useState(false); - const [showLoginPrompt, setShowLoginPrompt] = useState(false); + const navigate = useNavigate(); - useEffect(() => { - const unsubscribe = onAuthStateChanged(auth, (user) => { - if (user) { - const detailsIncomplete = localStorage.getItem("details") === "false"; - setShouldShowDetailsModel(detailsIncomplete); - toast.success("succsesfully logged in"); - } else { - setShouldShowDetailsModel(false); - setUser(false); - } - }); + const handleUserLogin = async () => { + try { + const result = await signInWithPopup(auth, provider); + const credential = GoogleAuthProvider.credentialFromResult(result); + // eslint-disable-next-line no-unused-vars + const token = credential.accessToken; + const user = result.user; - return () => { - unsubscribe(); - }; - }, []); - - useEffect(() => { - const interval = setInterval(() => { - if (user) { - setShowLoginPrompt(true); + const check = await isInDb(user.uid); + setUser(user); + toast.success("Successfully logged in"); + if (check) { + localStorage.setItem("details", "true"); + } else { + localStorage.setItem("details", "false"); } - }, 30000); - - return () => clearInterval(interval); - }, [user]); - + localStorage.setItem("id", user.uid); + navigate("/"); + } catch (error) { + console.log("error", error.code, "error msg: ", error.message); + } + }; return (
- {shouldShowDetailsModel && ( + {/* {shouldShowDetailsModel && ( setShouldShowDetailsModel(false)} /> - )} - {showLoginPrompt && } + )} */} }> } /> @@ -87,9 +84,12 @@ function App() { } /> } /> } /> - } /> + } /> - } /> + } + />
); diff --git a/src/components/SignInCard.js b/src/components/SignInCard.js index d2ef286..6ccc7bd 100644 --- a/src/components/SignInCard.js +++ b/src/components/SignInCard.js @@ -6,7 +6,6 @@ import Typography from "@mui/material/Typography"; import { styled } from "@mui/material/styles"; import GoogleIcon from "@mui/icons-material/Google"; import logo from "../images/rguktLogo.png"; -import { handleUserLogin } from "../firebaseUtils/login"; import { toast } from "react-toastify"; const Card = styled(MuiCard)(({ theme }) => ({ display: "flex", @@ -26,10 +25,10 @@ const Card = styled(MuiCard)(({ theme }) => ({ }), })); -export default function SignInCard() { - const handleLogin = async () => { +export default function SignInCard({ handleLogin }) { + const handleUserLogin = async () => { try { - await handleUserLogin(); + await handleLogin(); } catch (error) { toast.error("Failed to login"); } @@ -56,7 +55,7 @@ export default function SignInCard() {