Skip to content

Commit

Permalink
add updated code (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehardaway authored Nov 27, 2024
1 parent b71cf83 commit 070c533
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
38 changes: 14 additions & 24 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,34 @@ type PageWithLayout = NextPage<LoginProps> & {

const Login: PageWithLayout = () => {
const mounted = useMount();
const { status } = useSession();
const { status, data: session } = useSession();
const router = useRouter();
const [errorMessage, setErrorMessage] = useState<string | null>(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.");
}
}, []);
Expand Down Expand Up @@ -142,4 +132,4 @@ export const getStaticProps: GetStaticProps<LoginProps> = () => ({
},
});

export default Login;
export default Login;
50 changes: 35 additions & 15 deletions src/pages/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageProps> & {
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 (
<div className="tw-fixed tw-bg-white tw-top-0 tw-z-50 tw-w-screen tw-h-screen tw-flex tw-justify-center tw-items-center">
<Spinner />
</div>
);
}

if (!isLoggedIn) {
if (!session) {
return (
<div className="tw-fixed tw-bg-light-100 tw-top-0 tw-z-50 tw-w-screen tw-h-screen tw-flex tw-justify-center tw-items-center">
<div className="tw-fixed tw-bg-white tw-top-0 tw-z-50 tw-w-screen tw-h-screen tw-flex tw-justify-center tw-items-center">
<Spinner />
</div>
);
}

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 (
Expand All @@ -63,7 +83,7 @@ const Profile: PageProps = () => {

Profile.Layout = Layout01;

export const getStaticProps: GetStaticProps = () => {
export const getStaticProps: GetStaticProps<PageProps> = () => {
return {
props: {
layout: {
Expand Down

0 comments on commit 070c533

Please sign in to comment.