diff --git a/frontend/src/components/common/auth-checker.tsx b/frontend/src/components/common/auth-checker.tsx new file mode 100644 index 00000000..e57ea0ff --- /dev/null +++ b/frontend/src/components/common/auth-checker.tsx @@ -0,0 +1,34 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; +import { AuthContext } from "@/contexts/AuthContext"; +import { getAuth, onAuthStateChanged } from "firebase/auth"; +import { useContext } from "react"; + +interface AuthCheckerProps { + children: React.ReactNode; +} + +export default function AuthChecker({ children }: AuthCheckerProps) { + const auth = getAuth(); + const router = useRouter(); + const { user: currentUser, authIsReady } = useContext(AuthContext); + + const currentPage = router.pathname; + + useEffect(() => { + if (!currentUser && currentPage !== "/") { + onAuthStateChanged(auth, (user) => { + if (!user) { + router.push("/"); + } + }); + } + }); + + if (currentPage === "/") { + return children; + } + + return (currentUser && children) +} + diff --git a/frontend/src/components/common/navbar.tsx b/frontend/src/components/common/navbar.tsx index 15cf3815..4c2d5dfe 100644 --- a/frontend/src/components/common/navbar.tsx +++ b/frontend/src/components/common/navbar.tsx @@ -1,16 +1,98 @@ import Link from "next/link"; import { Button } from "../ui/button"; import Image from "next/image"; +import { AuthContext } from "@/contexts/AuthContext"; +import { useContext, useEffect, useState } from "react"; +import { Tabs, TabsList, TabsTrigger } from "../ui/tabs"; +import { useRouter } from "next/router"; +import { Avatar, AvatarImage } from "@radix-ui/react-avatar"; +import { ChevronDown } from "lucide-react"; +import { + DropdownMenu, + DropdownMenuCheckboxItem, + DropdownMenuContent, + DropdownMenuLabel, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu"; +import { useLogout } from "@/firebase-client/useLogout"; +import { useLogin } from "@/firebase-client/useLogin"; + +enum TabsOptions { + INTERVIEWS = "interviews", + QUESTIONS = "questions", + NULL = "", +} + export default function Navbar() { + const { user: currentUser, authIsReady } = useContext(AuthContext); + const [activeTab, setActiveTab] = useState(TabsOptions.NULL); + + const { login } = useLogin(); + const { logout } = useLogout(); + const router = useRouter(); + + const currentPage = router.pathname; + + useEffect(() => { + if (currentPage === "/interviews") { + setActiveTab(TabsOptions.INTERVIEWS); + } else if (currentPage === "/questions") { + setActiveTab(TabsOptions.QUESTIONS); + } else { + setActiveTab(TabsOptions.NULL); + } + }, [currentPage]); + return (
-
- CodeParty logo -
- - +
+
+ CodeParty logo + {currentUser &&
+ + + + + Interviews + + + + + Questions + + + + +
}
+ {!currentUser &&
+ + +
} + {currentUser && + + + + + + router.push("/profile")}> + Profile + + + Settings + + + Log Out + + + + }
) diff --git a/frontend/src/pages/_app.tsx b/frontend/src/pages/_app.tsx index 3c180e2e..734350a9 100644 --- a/frontend/src/pages/_app.tsx +++ b/frontend/src/pages/_app.tsx @@ -3,6 +3,7 @@ import type { AppProps } from 'next/app' import Layout from '../components/common/layout' import { Noto_Sans } from 'next/font/google' import AuthContextProvider from "@/contexts/AuthContext"; +import AuthChecker from '@/components/common/auth-checker'; const notoSans = Noto_Sans({ weight: ['400', '500', '600', '700', '800', '900'], @@ -19,9 +20,11 @@ export default function App({ Component, pageProps }: AppProps) { `}
- - - + + + + +
diff --git a/frontend/src/pages/questions/[id]/index.tsx b/frontend/src/pages/questions/[id]/index.tsx index 5b1d1d69..81027aac 100644 --- a/frontend/src/pages/questions/[id]/index.tsx +++ b/frontend/src/pages/questions/[id]/index.tsx @@ -5,7 +5,6 @@ import { TypographyBody } from "@/components/ui/typography"; import { useRouter } from "next/router"; import { useContext, useEffect, useState } from "react"; import { Question } from "../../../../types/QuestionTypes"; -import { auth } from "@/firebase-client/firebase_config"; import { questionApiPathAddress } from "@/firebase-client/gateway-address"; import { AuthContext } from "@/contexts/AuthContext";