From 24d2156271a57ac29cb769fe494e929e09603c55 Mon Sep 17 00:00:00 2001 From: Jerome Hardaway Date: Wed, 27 Nov 2024 00:50:25 -0500 Subject: [PATCH] add updated code --- src/pages/login.tsx | 38 ++++++++++++-------------------- src/pages/profile.tsx | 50 ++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/pages/login.tsx b/src/pages/login.tsx index feb93678..1f23269f 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -20,44 +20,34 @@ type PageWithLayout = NextPage & { const Login: PageWithLayout = () => { const mounted = useMount(); - const { status } = useSession(); + const { status, data: session } = useSession(); const router = useRouter(); const [errorMessage, setErrorMessage] = useState(null); const [isRedirecting, setIsRedirecting] = useState(false); - // Handle authenticated state with proper return type - useEffect((): (() => void) => { - const cleanup = (): void => undefined; - - if (status === "authenticated" && !isRedirecting) { + useEffect(() => { + if (status === "authenticated" && session && !isRedirecting) { setIsRedirecting(true); - const timeout = setTimeout(() => { - router.push("/profile").catch(() => { - setErrorMessage("Failed to redirect to profile. Please try refreshing the page."); - setIsRedirecting(false); - }); - }, 100); - - return () => { - clearTimeout(timeout); - }; + router.replace("/profile").catch((error) => { + console.error("Redirect error:", error); + setErrorMessage("Failed to redirect to profile. Please try refreshing the page."); + setIsRedirecting(false); + }); } - - return cleanup; - }, [status, router, isRedirecting]); + }, [status, session, router, isRedirecting]); const handleSignIn = useCallback(async () => { try { setErrorMessage(null); const result = await signIn("github", { - callbackUrl: "/profile", - redirect: true, + redirect: false, }); - + if (result?.error) { setErrorMessage("Failed to sign in with GitHub. Please try again."); } - } catch { + } catch (error) { + console.error("Sign in error:", error); setErrorMessage("An unexpected error occurred. Please try again."); } }, []); @@ -142,4 +132,4 @@ export const getStaticProps: GetStaticProps = () => ({ }, }); -export default Login; \ No newline at end of file +export default Login; diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index 580cf6ed..6b4b250f 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -6,37 +6,57 @@ import Layout01 from "@layout/layout-01"; import Breadcrumb from "@components/breadcrumb"; import ProfileBio from "@containers/profile/bio"; import Spinner from "@ui/spinner"; -import { useUser } from "@contexts/user-context"; +import { useSession, signOut } from "next-auth/react"; import { useMount } from "@hooks"; -type PageProps = NextPage & { - Layout: typeof Layout01; +type PageProps = { + layout?: { + headerShadow: boolean; + headerFluid: boolean; + footerMode: string; + }; +}; + +type PageWithLayout = NextPage & { + Layout?: typeof Layout01; }; -const Profile: PageProps = () => { +const Profile: PageWithLayout = () => { const mounted = useMount(); - const { isLoggedIn, logout } = useUser(); + const { data: session, status } = useSession(); const router = useRouter(); useEffect(() => { - if (!isLoggedIn) { - void router.push("/login"); + if (status === "unauthenticated") { + router.replace("/login").catch((error) => { + console.error("Redirect error:", error); + }); } - }, [isLoggedIn, router]); + }, [status, router]); - if (!mounted) return null; + if (!mounted || status === "loading") { + return ( +
+ +
+ ); + } - if (!isLoggedIn) { + if (!session) { return ( -
+
); } - const handleLogout = () => { - logout(); - void router.push("/login"); + const handleLogout = async () => { + try { + await signOut({ redirect: false }); + await router.replace("/login"); + } catch (error) { + console.error("Logout error:", error); + } }; return ( @@ -63,7 +83,7 @@ const Profile: PageProps = () => { Profile.Layout = Layout01; -export const getStaticProps: GetStaticProps = () => { +export const getStaticProps: GetStaticProps = () => { return { props: { layout: {