Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ import { useSearchParams } from 'next/navigation';
import { LoginForm, DevQuickLogin } from "../../components/forms/login-form"
import { ModernAuthLayout } from "../../components/layout/modern-auth-layout"
import { Alert, AlertDescription } from "../../components/ui/alert";
import { hasSearchParam } from "../../utils/searchParams";

function LoginContent() {
const searchParams = useSearchParams();
const action = searchParams?.get('action');
const message = searchParams?.get('message');
const isPostingReply = action === 'posting_reply';
const isSessionRevoked = message === 'session_revoked';
const isAddingAccount = hasSearchParam(searchParams, 'switchAccount');

return (
<ModernAuthLayout afterCard={<DevQuickLogin />}>
{isAddingAccount && (
<Alert className="mb-4 bg-primary/10 border-theme-medium">
<Icon name="UserPlus" size={16} className="text-primary" />
<AlertDescription className="text-primary">
Sign in with another account to add it to your account switcher.
</AlertDescription>
</Alert>
)}

{isPostingReply && (
<Alert className="mb-4 bg-primary/10 border-theme-medium">
<Icon name="AlertCircle" size={16} className="text-primary" />
Expand Down
10 changes: 7 additions & 3 deletions app/components/forms/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Link from 'next/link';
import { getEnvironmentType } from '../../utils/environmentConfig';
import { looksLikeEmail } from '@/utils/validationPatterns';
import { ChallengeWrapper, useChallengeToken } from '../auth/ChallengeWrapper';
import { hasSearchParam } from '../../utils/searchParams';

// Constants for rate limiting
const MAX_ATTEMPTS_BEFORE_WARNING = 3;
Expand Down Expand Up @@ -139,10 +140,13 @@ export function LoginForm() {
setWarning('');
}, []);

// Redirect if already authenticated
// Redirect if already authenticated (but not when adding another account)
useEffect(() => {
const isAddingAccount = searchParams?.has('switchAccount');
if (!authLoading && isAuthenticated && !isAddingAccount) {
// searchParams can be null before Next.js resolves the URL; hasSearchParam
// also checks window.location as a fallback so we never accidentally redirect
// away when the user is in the middle of an add-another-account flow.
const urlHasSwitchAccount = hasSearchParam(searchParams, 'switchAccount');
if (!authLoading && isAuthenticated && !urlHasSwitchAccount) {
router.push('/home');
}
}, [isAuthenticated, authLoading, router, searchParams]);
Expand Down
13 changes: 11 additions & 2 deletions app/components/layout/MobileAccountMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,18 @@ export default function MobileAccountMenu() {
}
};

const addAnotherAccount = () => {
const addAnotherAccount = (event: React.MouseEvent) => {
event.preventDefault();
event.stopPropagation();
setIsOpen(false);
router.push("/auth/login?switchAccount=1");
// Use window.location.href instead of router.push so the page performs a
// full reload. With router.push (client-side nav) the AuthProvider is
// already settled (isLoading=false, isAuthenticated=true) and the login
// form's redirect guard can fire before useSearchParams reflects the new
// URL, bouncing the user back immediately. A full reload reinitialises the
// AuthProvider with isLoading=true, keeping the guard silent while the
// params are read correctly from the address bar.
window.location.href = "/auth/login?switchAccount=1";
};

const forgetAccount = async (uid: string) => {
Expand Down
28 changes: 28 additions & 0 deletions app/utils/searchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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);
}
Comment on lines +2 to +26
return false;
}