diff --git a/src/app/logout/page.jsx b/src/app/logout/page.jsx deleted file mode 100644 index 8938bada..00000000 --- a/src/app/logout/page.jsx +++ /dev/null @@ -1,14 +0,0 @@ -'use client'; -import { useContext } from 'react'; -import { AuthContext } from '@/context/auth-context'; -import { PrimaryButton } from '@/components/shared/Typography/Buttons'; -function Page() { - const { handleSignOut } = useContext(AuthContext); - return ( -
- Log out -
- ); -} - -export default Page; diff --git a/src/app/register/page.jsx b/src/app/register/page.jsx index 8473758d..27911669 100644 --- a/src/app/register/page.jsx +++ b/src/app/register/page.jsx @@ -52,6 +52,8 @@ function Page() { const userDetails = getUserDetails(); if (userDetails.name) { setIsLoggedIn(true); + } else { + setIsLoggedIn(false); } }, [getUserDetails, userInfo]); diff --git a/src/components/Marginals/navbar/navbar.jsx b/src/components/Marginals/navbar/navbar.jsx index c2808c48..24657476 100644 --- a/src/components/Marginals/navbar/navbar.jsx +++ b/src/components/Marginals/navbar/navbar.jsx @@ -1,5 +1,5 @@ 'use client'; -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useContext } from 'react'; import { HamburgerContainer, MainBar, @@ -19,10 +19,14 @@ import Image from 'next/image'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import { AnimatePresence, motion } from 'framer-motion'; +import { AuthContext } from '@/context/auth-context'; +import { useUserDetails } from '@/hooks/useUserDetails'; const Navbar = () => { const [isOpen, setIsOpen] = useState(false); const pathname = usePathname(); + const { handleSignOut, userInfo } = useContext(AuthContext); + const [isLoggedIn, setIsLoggedIn] = useState(false); function handleToggle() { setIsOpen(!isOpen); @@ -36,6 +40,16 @@ const Navbar = () => { handleCloseMenu(); }, [pathname]); + const getUserDetails = useUserDetails(); + useEffect(() => { + const userDetails = getUserDetails(); + if (userDetails.name) { + setIsLoggedIn(true); + } else { + setIsLoggedIn(false); + } + }, [getUserDetails, userInfo]); + return (
@@ -54,11 +68,15 @@ const Navbar = () => { ))} - - - {ButtonData.title} - - + {isLoggedIn ? ( + Logout + ) : ( + + + {ButtonData.title} + + + )} @@ -89,11 +107,15 @@ const Navbar = () => { ))} - - - {ButtonData.title} - - + {isLoggedIn ? ( + Logout + ) : ( + + + {ButtonData.title} + + + )} diff --git a/src/context/auth-context.jsx b/src/context/auth-context.jsx index faa626b8..dfce3bb2 100644 --- a/src/context/auth-context.jsx +++ b/src/context/auth-context.jsx @@ -7,7 +7,7 @@ import { auth } from '@/firebase/firebase'; export const AuthContext = createContext(); export const AuthProvider = ({ children }) => { - const [userInfo, setUserData] = useState({}); + const [userInfo, setUserInfo] = useState({}); const [authLoading, setAuthLoading] = useState(false); const handleGoogleSignIn = async () => { @@ -23,7 +23,7 @@ export const AuthProvider = ({ children }) => { expires: 7, sameSite: 'strict', }); - setUserData(userData); + setUserInfo(userData); toast.success('Successfully signed in with Google.'); } else { toast.error('Google sign-in failed. Please try again.'); @@ -39,7 +39,7 @@ export const AuthProvider = ({ children }) => { const handleSignOut = async () => { try { await signOutUser(); - setUserData({}); + setUserInfo({}); Cookies.remove('userData'); toast.success('Successfully signed out.'); } catch (error) { @@ -50,7 +50,7 @@ export const AuthProvider = ({ children }) => { return ( {children}