Skip to content

Commit 070c533

Browse files
add updated code (#642)
1 parent b71cf83 commit 070c533

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

src/pages/login.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,34 @@ type PageWithLayout = NextPage<LoginProps> & {
2020

2121
const Login: PageWithLayout = () => {
2222
const mounted = useMount();
23-
const { status } = useSession();
23+
const { status, data: session } = useSession();
2424
const router = useRouter();
2525
const [errorMessage, setErrorMessage] = useState<string | null>(null);
2626
const [isRedirecting, setIsRedirecting] = useState(false);
2727

28-
// Handle authenticated state with proper return type
29-
useEffect((): (() => void) => {
30-
const cleanup = (): void => undefined;
31-
32-
if (status === "authenticated" && !isRedirecting) {
28+
useEffect(() => {
29+
if (status === "authenticated" && session && !isRedirecting) {
3330
setIsRedirecting(true);
34-
const timeout = setTimeout(() => {
35-
router.push("/profile").catch(() => {
36-
setErrorMessage("Failed to redirect to profile. Please try refreshing the page.");
37-
setIsRedirecting(false);
38-
});
39-
}, 100);
40-
41-
return () => {
42-
clearTimeout(timeout);
43-
};
31+
router.replace("/profile").catch((error) => {
32+
console.error("Redirect error:", error);
33+
setErrorMessage("Failed to redirect to profile. Please try refreshing the page.");
34+
setIsRedirecting(false);
35+
});
4436
}
45-
46-
return cleanup;
47-
}, [status, router, isRedirecting]);
37+
}, [status, session, router, isRedirecting]);
4838

4939
const handleSignIn = useCallback(async () => {
5040
try {
5141
setErrorMessage(null);
5242
const result = await signIn("github", {
53-
callbackUrl: "/profile",
54-
redirect: true,
43+
redirect: false,
5544
});
56-
45+
5746
if (result?.error) {
5847
setErrorMessage("Failed to sign in with GitHub. Please try again.");
5948
}
60-
} catch {
49+
} catch (error) {
50+
console.error("Sign in error:", error);
6151
setErrorMessage("An unexpected error occurred. Please try again.");
6252
}
6353
}, []);
@@ -142,4 +132,4 @@ export const getStaticProps: GetStaticProps<LoginProps> = () => ({
142132
},
143133
});
144134

145-
export default Login;
135+
export default Login;

src/pages/profile.tsx

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,57 @@ import Layout01 from "@layout/layout-01";
66
import Breadcrumb from "@components/breadcrumb";
77
import ProfileBio from "@containers/profile/bio";
88
import Spinner from "@ui/spinner";
9-
import { useUser } from "@contexts/user-context";
9+
import { useSession, signOut } from "next-auth/react";
1010
import { useMount } from "@hooks";
1111

12-
type PageProps = NextPage & {
13-
Layout: typeof Layout01;
12+
type PageProps = {
13+
layout?: {
14+
headerShadow: boolean;
15+
headerFluid: boolean;
16+
footerMode: string;
17+
};
18+
};
19+
20+
type PageWithLayout = NextPage<PageProps> & {
21+
Layout?: typeof Layout01;
1422
};
1523

16-
const Profile: PageProps = () => {
24+
const Profile: PageWithLayout = () => {
1725
const mounted = useMount();
18-
const { isLoggedIn, logout } = useUser();
26+
const { data: session, status } = useSession();
1927
const router = useRouter();
2028

2129
useEffect(() => {
22-
if (!isLoggedIn) {
23-
void router.push("/login");
30+
if (status === "unauthenticated") {
31+
router.replace("/login").catch((error) => {
32+
console.error("Redirect error:", error);
33+
});
2434
}
25-
}, [isLoggedIn, router]);
35+
}, [status, router]);
2636

27-
if (!mounted) return null;
37+
if (!mounted || status === "loading") {
38+
return (
39+
<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">
40+
<Spinner />
41+
</div>
42+
);
43+
}
2844

29-
if (!isLoggedIn) {
45+
if (!session) {
3046
return (
31-
<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">
47+
<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">
3248
<Spinner />
3349
</div>
3450
);
3551
}
3652

37-
const handleLogout = () => {
38-
logout();
39-
void router.push("/login");
53+
const handleLogout = async () => {
54+
try {
55+
await signOut({ redirect: false });
56+
await router.replace("/login");
57+
} catch (error) {
58+
console.error("Logout error:", error);
59+
}
4060
};
4161

4262
return (
@@ -63,7 +83,7 @@ const Profile: PageProps = () => {
6383

6484
Profile.Layout = Layout01;
6585

66-
export const getStaticProps: GetStaticProps = () => {
86+
export const getStaticProps: GetStaticProps<PageProps> = () => {
6787
return {
6888
props: {
6989
layout: {

0 commit comments

Comments
 (0)