Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] Fix redirect issue after signin by using router.replace from client component #246

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ export async function authenticate(
password: formData.get("password") as string,
});
revalidatePath("/", "layout");
redirect("/");
} catch (error) {
if ((error as Error).message.includes("Authentication failed")) {
return "CredentialSignin";
}
throw error;
}
// This is a temporary fix for the redirect issue. It seems that the redirect is not working as expected.
// Instead of redirecting from server action, router.refresh from client component is used.
// Maybe this is related?: https://github.com/vercel/next.js/issues/58263
// redirect('/', 'layout');
return "Authenticated";
Comment on lines +66 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented-out code if it's no longer needed to keep the codebase clean and maintainable.

-  // This is a temporary fix for the redirect issue. It seems that the redirect is not working as expected.
-  // Instead of redirecting from server action, router.refresh from client component is used.
-  // Maybe this is related?: https://github.com/vercel/next.js/issues/58263
-  // redirect('/', 'layout');

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// This is a temporary fix for the redirect issue. It seems that the redirect is not working as expected.
// Instead of redirecting from server action, router.refresh from client component is used.
// Maybe this is related?: https://github.com/vercel/next.js/issues/58263
// redirect('/', 'layout');
return "Authenticated";
return "Authenticated";

}

function getAccessToken() {
Expand Down
5 changes: 5 additions & 0 deletions frontend/app/ui/auth/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useFormState, useFormStatus } from "react-dom";
import { useRouter } from "next/navigation";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the import path for useRouter is correct. The correct import path from Next.js is "next/router", not "next/navigation".

- import { useRouter } from "next/navigation";
+ import { useRouter } from "next/router";

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { useRouter } from "next/navigation";
import { useRouter } from "next/router";


export default function LoginForm() {
return (
Expand All @@ -26,6 +27,10 @@ export default function LoginForm() {
function Form() {
const { currentUser } = useAuthContext();
const [code, action] = useFormState(authenticate, undefined);
const router = useRouter();
if (code === "Authenticated") {
router.replace("/");
}
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redirection logic based on the code state value is correctly implemented. However, consider adding a dependency array to a useEffect hook to avoid potential issues with this logic executing more than once or at an unintended time.

+ import { useEffect } from 'react';
+ 
+ useEffect(() => {
+   if (code === "Authenticated") {
+     router.replace("/");
+   }
+ }, [code, router]);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const router = useRouter();
if (code === "Authenticated") {
router.replace("/");
}
import { useEffect } from 'react';
useEffect(() => {
if (code === "Authenticated") {
router.replace("/");
}
}, [code, router]);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's going to redirect, there is no way this code is going to be executed twice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@takumihara: You're correct that in this specific case, the redirection should ideally only occur once due to the nature of the navigation action. However, wrapping the redirection logic in a useEffect hook with [code, router] as dependencies is a best practice in React development. It ensures that the effect runs exactly once when the code state changes to "Authenticated", providing a safeguard against potential unexpected re-renders before the navigation completes. This approach enhances the predictability and maintainability of the code, especially in more complex applications where state changes and effects might interact in less straightforward ways. It's about adhering to best practices that help prevent subtle bugs in larger, more complex scenarios.

return (
<>
<form action={action}>
Expand Down
Loading