Skip to content

Fix: "Add another account" button now navigates to sign-in page#208

Merged
sirjamesgray merged 2 commits into
mainfrom
copilot/fix-switch-account-dropdown-action
May 11, 2026
Merged

Fix: "Add another account" button now navigates to sign-in page#208
sirjamesgray merged 2 commits into
mainfrom
copilot/fix-switch-account-dropdown-action

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

  • Identify root cause: stale searchParams during client-side navigation causes premature redirect
  • Change addAnotherAccount to use window.location.href (full page load) instead of router.push
  • Update hasSearchParam to always check window.location.search first (OR logic, not only-if-null)
  • Simplify button onClick to pass addAnotherAccount directly (code review feedback)

- Fixed premature redirect-to-home when authenticated user visits
  /auth/login?switchAccount=1, caused by searchParams being momentarily
  null during Next.js client-side navigation
- Added event.preventDefault/stopPropagation to addAnotherAccount handler,
  consistent with other interactive buttons in the dropdown
- Added informational banner on login page when ?switchAccount=1 is present
- Extracted shared hasSearchParam() utility to avoid duplicating the
  searchParams null-fallback logic

Agent-Logs-Url: https://github.com/WeWriteApp/WeWrite/sessions/a0477169-9657-4fc2-8de4-c3efe4606891

Co-authored-by: sirjamesgray <16139439+sirjamesgray@users.noreply.github.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
wewrite Ready Ready Preview, Comment May 11, 2026 2:32am

Request Review

- Changed addAnotherAccount to use window.location.href (full page reload)
  instead of router.push (client-side nav) to prevent premature redirect.
  With router.push the AuthProvider stays settled (isLoading=false) so the
  login form's redirect guard fires before useSearchParams has the new URL.
- Updated hasSearchParam to always check window.location.search first (as
  the authoritative source), then fall back to the searchParams object.
  Previously only fell back to window.location when searchParams was null,
  missing the stale-but-non-null case during client-side navigation.

Agent-Logs-Url: https://github.com/WeWriteApp/WeWrite/sessions/39dca12d-9ffe-4242-9469-01ba643071e8

Co-authored-by: sirjamesgray <16139439+sirjamesgray@users.noreply.github.com>
@sirjamesgray sirjamesgray marked this pull request as ready for review May 11, 2026 02:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Resolves an account-switching flow bug where clicking “Add another account” could immediately bounce an already-authenticated user away from the login page due to stale useSearchParams() during client-side navigation.

Changes:

  • Added a hasSearchParam helper to detect query flags using window.location.search as a more reliable source during navigation.
  • Updated the login redirect guard to use hasSearchParam so authenticated users aren’t redirected when switchAccount is present.
  • Changed “Add another account” navigation to window.location.href to force a full reload and avoid auth/searchParams race conditions; also simplified the click handler wiring.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
app/utils/searchParams.ts Introduces hasSearchParam helper for safer query detection during client-side navigation.
app/components/layout/MobileAccountMenu.tsx Forces full-page navigation for add-account flow to avoid stale params/auth state issues.
app/components/forms/login-form.tsx Updates authenticated-redirect logic to honor switchAccount via the new helper.
app/auth/login/page.tsx Shows an add-account banner when switchAccount is present using the new helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/utils/searchParams.ts
Comment on lines +2 to +26
* Returns true if the given key exists in the provided Next.js searchParams object
* OR in the current browser URL.
*
* Two failure modes are handled:
* 1. `useSearchParams()` returns null before Next.js has resolved the URL during
* client-side navigation — the window.location fallback covers this case.
* 2. `useSearchParams()` returns a non-null but *stale* object (from the previous
* page) when the component mounts before the router has flushed the new params —
* the window.location fallback is always checked regardless.
*/
export function hasSearchParam(
searchParams: { has(key: string): boolean } | null | undefined,
key: string
): boolean {
// Always check window.location first — it reflects the address bar immediately
// and is immune to any stale searchParams object from the previous render.
if (typeof window !== 'undefined') {
if (new URLSearchParams(window.location.search).has(key)) {
return true;
}
}
// Fall back to the searchParams object (may be null/stale on client-side nav)
if (searchParams != null) {
return searchParams.has(key);
}
@sirjamesgray sirjamesgray merged commit 9b65c30 into main May 11, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants