diff --git a/src/App.js b/src/App.js index 17f63ee..399c37b 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,7 @@ import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; import { ToastContainer } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; +import PrivateRoute from "./components/PrivateRoute"; import Navbar from "./components/Navbar"; import Explore from "./pages/Explore"; import Profile from "./pages/Profile"; @@ -16,7 +17,9 @@ function App() { } /> } /> - } /> + }> + } /> + } /> } /> } /> diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 7abbc1f..845d79b 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -45,7 +45,7 @@ function Navbar() { : "navbarListItemName" } > - Explore + Offers

  • navigate("/profile")}> @@ -61,7 +61,7 @@ function Navbar() { : "navbarListItemName" } > - Explore + Profile

  • diff --git a/src/components/PrivateRoute.jsx b/src/components/PrivateRoute.jsx new file mode 100644 index 0000000..88fbda8 --- /dev/null +++ b/src/components/PrivateRoute.jsx @@ -0,0 +1,14 @@ +import { useAuthStatus } from "../hooks/useAuthStatus"; +import { Navigate, Outlet } from "react-router-dom"; +import Spinner from "./Spinner"; + +const PrivateRoute = () => { + const { loggedIn, checkingStatus } = useAuthStatus(); + + if (checkingStatus) { + return + } +console.log(loggedIn) + return loggedIn ? : ; +}; +export default PrivateRoute; diff --git a/src/components/Spinner.jsx b/src/components/Spinner.jsx new file mode 100644 index 0000000..321c9b6 --- /dev/null +++ b/src/components/Spinner.jsx @@ -0,0 +1,7 @@ +function Spinner(){ + return ( +
    +
    +
    ) +} +export default Spinner diff --git a/src/hooks/useAuthStatus.js b/src/hooks/useAuthStatus.js new file mode 100644 index 0000000..65a1d45 --- /dev/null +++ b/src/hooks/useAuthStatus.js @@ -0,0 +1,24 @@ +import { useEffect, useState, useRef } from "react"; +import { getAuth, onAuthStateChanged } from "firebase/auth"; + +export const useAuthStatus = () => { + const [loggedIn, setLoggedIn] = useState(false); + const [checkingStatus, setCheckingStatus] = useState(true); + const isMouted = useRef(true); + + useEffect(() => { + if (isMouted) { + const auth = getAuth(); + onAuthStateChanged(auth, (user) => { + if (user) { + setLoggedIn(true); + } + setCheckingStatus(false); + }); + } + return () => { + isMouted.current = false; + }; + },[isMouted]); + return { loggedIn, checkingStatus }; +}; diff --git a/src/pages/Profile.jsx b/src/pages/Profile.jsx index 79c559c..665e962 100644 --- a/src/pages/Profile.jsx +++ b/src/pages/Profile.jsx @@ -1,13 +1,89 @@ +import { toast } from "react-toastify"; import { useEffect, useState } from "react"; -import { getAuth } from "firebase/auth"; +import { getAuth, updateProfile } from "firebase/auth"; +import { useNavigate, Link } from "react-router-dom"; +import { db } from "../firebase.config"; +import { updateDoc ,doc } from "firebase/firestore"; function Profile() { - const [user, setUser] = useState(); + const [changeDetails, setChangeDetails] = useState(false); const auth = getAuth(); - useEffect(() => { - setUser(auth.currentUser); - }, []); - - return user ?

    {user.displayName}

    : "Not Logged In"; + const [formData, setFormData] = useState({ + name: auth.currentUser.displayName, + email: auth.currentUser.email, + }); + const navigate = useNavigate(); + const { name, email } = formData; + const onLogout = () => { + auth.signOut(); + navigate("/"); + }; + + const onSubmit = async () => { + try { + if (auth.currentUser.displayName !== name) { + await updateProfile(auth.currentUser, { + displayName: name, + }); + const userRef = doc(db,'users',auth.currentUser.uid) + await updateDoc(userRef,{ + name + }) + } + } catch (error) { + toast.error('could no update profile details') + } + }; + const onChange = (e) => { + setFormData((prevState) => ({ + ...prevState, + [e.target.id]: e.target.value, + })); + }; + return ( +
    +
    +

    My Profile

    + +
    +
    +
    +

    +

    { + changeDetails && onSubmit(); + setChangeDetails((prevState) => !prevState); + }} + > + {changeDetails ? "done" : "change"} +

    +

    +
    +
    +
    + + +
    +
    +
    +
    + ); } export default Profile; diff --git a/src/pages/Signup.jsx b/src/pages/Signup.jsx index fa9993c..4e8277b 100644 --- a/src/pages/Signup.jsx +++ b/src/pages/Signup.jsx @@ -49,7 +49,8 @@ function Signup() { navigate("/"); } catch (error) { - toast.error("something went wron with the registration") + console.log(error) + toast.error("something went wrong with the registration") } }; return (