Skip to content

Fix: Login redirect to original destinationΒ #743

@timothyfroehlich

Description

@timothyfroehlich

Currently, when a non-logged-in user tries to access a protected route, the middleware correctly redirects them to /login?next=/original-path. However, the login process ignores this next parameter and always redirects the user to /dashboard upon successful authentication.

Problem Locations:

  1. src/app/(auth)/login/page.tsx: If a user is already logged in and hits the login page with a next parameter, they are redirected to /dashboard instead of the next destination.
  2. src/app/(auth)/login/login-form.tsx: Needs to capture the next search parameter from the URL and include it in the form (e.g., as a hidden input).
  3. src/app/(auth)/actions.ts (loginAction): Needs to check for the next parameter in formData and redirect to it instead of /dashboard if present.

Proposed Solution:

  1. Update LoginPage to read next from search params (if possible, though Server Components get params via props).
  2. Update LoginForm to read next from useSearchParams or props.
  3. Add <input type="hidden" name="next" value={next} /> to the form.
  4. Update loginAction to:
    const next = formData.get("next") as string | null;
    redirect(next ?? "/dashboard");

Metadata

Metadata

Labels

authTasks related to authentication and user managementbugSomething isn't workingfix

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions