From 043bfcd53f505c3a20ad6a4aa5cc77002ecf2952 Mon Sep 17 00:00:00 2001 From: Gregor Zurowski Date: Thu, 19 Nov 2026 17:39:54 +0100 Subject: [PATCH 1/4] Stop sharing the auth token in a parent-domain cookie `LegacyAuthCookieCleanup` clears the cookie browsers still hold. It can be deleted two weeks after deploy, once every copy has expired. --- components/AuthSharingWrapper.tsx | 88 ------------------------ components/LegacyAuthCookieCleanup.tsx | 30 ++++++++ components/providers/ClientProviders.tsx | 79 +++++++++++---------- 3 files changed, 69 insertions(+), 128 deletions(-) delete mode 100644 components/AuthSharingWrapper.tsx create mode 100644 components/LegacyAuthCookieCleanup.tsx diff --git a/components/AuthSharingWrapper.tsx b/components/AuthSharingWrapper.tsx deleted file mode 100644 index 065b2f9c0..000000000 --- a/components/AuthSharingWrapper.tsx +++ /dev/null @@ -1,88 +0,0 @@ -'use client'; - -import { Dialog, Transition } from '@headlessui/react'; -import { Fragment } from 'react'; -import { useSession, signIn } from 'next-auth/react'; -import { useEffect, useState } from 'react'; -import { AuthSharingService } from '@/services/auth-sharing.service'; -import { Loader2 } from 'lucide-react'; - -export function AuthSharingWrapper({ children }: { children: React.ReactNode }) { - const { data: session, status } = useSession(); - const [isChecking, setIsChecking] = useState(false); - - useEffect(() => { - const checkSharedToken = async () => { - // Only check if we're not authenticated - if (status === 'unauthenticated') { - const sharedToken = AuthSharingService.getSharedAuthToken(); - - if (sharedToken) { - setIsChecking(true); - await signIn('credentials', { - authToken: sharedToken, - redirect: false, - }); - AuthSharingService.removeSharedAuthToken(); - setIsChecking(false); - } - } - }; - - checkSharedToken(); - }, [status]); - - useEffect(() => { - if (session?.authToken) { - AuthSharingService.setSharedAuthToken(session.authToken); - } - }, [session?.authToken]); - - return ( - <> - {children} - - - {}}> - -
- - -
-
- - -
- - - Authorizing... - -

- We found your existing session. Signing you in automatically... -

-
-
-
-
-
-
-
- - ); -} diff --git a/components/LegacyAuthCookieCleanup.tsx b/components/LegacyAuthCookieCleanup.tsx new file mode 100644 index 000000000..276dcba13 --- /dev/null +++ b/components/LegacyAuthCookieCleanup.tsx @@ -0,0 +1,30 @@ +'use client'; + +import Cookies from 'js-cookie'; +import { useEffect } from 'react'; + +// +// FIXME: Remove this file once the legacy auth cookie is no longer valid (14 days after deploy) +// +// The retired cross-app auth sharing kept the API token in a script-readable +// cookie on the parent domain, where every subdomain could read it. Each write +// set a 14-day expiry, so this can be deleted two weeks after deploy. +const LEGACY_AUTH_COOKIE = (() => { + switch (process.env.NEXT_PUBLIC_VERCEL_ENV) { + case 'production': + return { name: 'prod.researchhub.auth.token', domain: '.researchhub.com' }; + case 'preview': + case 'development': + return { name: 'stg.researchhub.auth.token', domain: '.staging.researchhub.com' }; + default: + return { name: 'local.researchhub.auth.token', domain: 'localhost' }; + } +})(); + +export function LegacyAuthCookieCleanup() { + useEffect(() => { + Cookies.remove(LEGACY_AUTH_COOKIE.name, { domain: LEGACY_AUTH_COOKIE.domain, path: '/' }); + }, []); + + return null; +} diff --git a/components/providers/ClientProviders.tsx b/components/providers/ClientProviders.tsx index f909e61fe..ee8b0f128 100644 --- a/components/providers/ClientProviders.tsx +++ b/components/providers/ClientProviders.tsx @@ -13,7 +13,7 @@ import { UserProvider } from '@/contexts/UserContext'; import { OrganizationProvider } from '@/contexts/OrganizationContext'; import { OnchainProvider } from '@/contexts/OnchainContext'; import { NavigationProvider } from '@/contexts/NavigationContext'; -import { AuthSharingWrapper } from '@/components/AuthSharingWrapper'; +import { LegacyAuthCookieCleanup } from '@/components/LegacyAuthCookieCleanup'; import { VerificationProvider } from '@/contexts/VerificationContext'; import SignupModalContainer from '@/components/modals/SignupModalContainer'; import { ShareModalProvider } from '@/contexts/ShareContext'; @@ -34,49 +34,48 @@ export function ClientProviders({ children, session }: ClientProvidersProps) { return ( <> + - - - <> - - - - - - {/* Above the layouts because the left sidebar - carries the funding power card and mounts - outside PageLayout on some routes. */} - - - - {/* Above the layouts: every page's left - sidebar lists these documents, and - navigating must not fetch them again. */} - - - - {/* Inside the user providers: the share - modal's post preview shows the sharer. */} - {children} - - - - - - - - - - - - - - - - + + <> + + + + + + {/* Above the layouts because the left sidebar + carries the funding power card and mounts + outside PageLayout on some routes. */} + + + + {/* Above the layouts: every page's left + sidebar lists these documents, and + navigating must not fetch them again. */} + + + + {/* Inside the user providers: the share + modal's post preview shows the sharer. */} + {children} + + + + + + + + + + + + + + + From a431da38906828b1828184a715c2e3ecf2039527 Mon Sep 17 00:00:00 2001 From: Gregor Zurowski Date: Thu, 19 Nov 2026 17:41:03 +0100 Subject: [PATCH 2/4] Remove the old app's shared logout route The retired old app redirected to `/api/auth/logout` on sign-out to clear this app's session cookies. It is not used in this app anymore. --- app/api/auth/logout/route.ts | 45 ------------------------------------ 1 file changed, 45 deletions(-) delete mode 100644 app/api/auth/logout/route.ts diff --git a/app/api/auth/logout/route.ts b/app/api/auth/logout/route.ts deleted file mode 100644 index f5e671bba..000000000 --- a/app/api/auth/logout/route.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { NextResponse } from 'next/server'; -import { AuthSharingService } from '@/services/auth-sharing.service'; - -/** - * Gets the cookie prefix based on the environment - * @returns The next-auth cookie prefix - */ -function getCookiePrefix(): string { - const env = process.env.NEXT_PUBLIC_VERCEL_ENV; - if (env === 'production' || env === 'preview') { - return '__Secure-'; - } - return ''; -} - -/** - * Handles the shared logout process by clearing the authentication cookies - * and redirecting to the callback URL or the origin. - * - * @param request - The HTTP request object - * @returns A redirect response to the callback URL or origin - */ -export async function GET(request: Request) { - const { searchParams, origin } = new URL(request.url); - const callbackUrl = searchParams.get('callbackUrl') || origin; - - try { - const expiredDate = new Date(0).toUTCString(); - const prefix = getCookiePrefix(); - - return new Response(null, { - status: 307, - headers: { - Location: callbackUrl, - 'Set-Cookie': [ - `${prefix}next-auth.session-token=; Expires=${expiredDate}; Path=/; HttpOnly; Secure`, - `${prefix}next-auth.csrf-token=; Expires=${expiredDate}; Path=/; HttpOnly; Secure`, - `${AuthSharingService.getAuthCookieName()}=; Expires=${expiredDate}; Path=/; Domain=${AuthSharingService.getParentDomain()}; HttpOnly; Secure`, - ].join(', '), - }, - }); - } catch (error) { - return NextResponse.redirect(callbackUrl, 307); - } -} From e8601beb07d5f284897e7f4f190ef67c0c8831d2 Mon Sep 17 00:00:00 2001 From: Gregor Zurowski Date: Thu, 19 Nov 2026 17:42:46 +0100 Subject: [PATCH 3/4] Replace `AuthSharingService` with a `signOutUser` helper Without the shared cookie, all that's left of the service is sign-out: clearing the analytics session and signing out of NextAuth. --- app/api/auth/[...nextauth]/auth.config.ts | 4 +- components/You/YouMoreCard.tsx | 4 +- .../events/catalyst/CatalystLoggedInBody.tsx | 8 +- components/menus/UserMenu.tsx | 4 +- config/constants.ts | 3 - contexts/UserContext.tsx | 4 +- services/auth-sharing.service.ts | 97 ------------------- utils/auth.ts | 7 ++ 8 files changed, 16 insertions(+), 115 deletions(-) delete mode 100644 services/auth-sharing.service.ts create mode 100644 utils/auth.ts diff --git a/app/api/auth/[...nextauth]/auth.config.ts b/app/api/auth/[...nextauth]/auth.config.ts index 6eb6a2f34..1bbdb7339 100644 --- a/app/api/auth/[...nextauth]/auth.config.ts +++ b/app/api/auth/[...nextauth]/auth.config.ts @@ -4,7 +4,6 @@ import GoogleProvider from 'next-auth/providers/google'; import TwitterProvider from 'next-auth/providers/twitter'; import CredentialsProvider from 'next-auth/providers/credentials'; import { AuthService } from '@/services/auth.service'; -import { AuthSharingService } from '@/services/auth-sharing.service'; // Debug flag - set to true to enable detailed authentication logging const DEBUG_AUTH = true; @@ -70,7 +69,7 @@ export const authOptions: NextAuthOptions = { }, async authorize(credentials, req) { try { - // If we have an authToken from the shared cookie, use that directly + // Email verification and MFA sign in with an already-issued token. if (credentials?.authToken) { try { // Fetch user data using the provided authToken @@ -83,7 +82,6 @@ export const authOptions: NextAuthOptions = { authToken: credentials.authToken, }; } catch (error) { - AuthSharingService.removeSharedAuthToken(); return promptInvalidCredentials(); } } diff --git a/components/You/YouMoreCard.tsx b/components/You/YouMoreCard.tsx index bcaceef18..8b76ea84a 100644 --- a/components/You/YouMoreCard.tsx +++ b/components/You/YouMoreCard.tsx @@ -25,7 +25,7 @@ import { useChangelogSeen } from '@/components/changelog/useChangelogSeen'; import { BaseMenu } from '@/components/ui/form/BaseMenu'; import { ResearchCoinIcon } from '@/components/ui/icons/ResearchCoinIcon'; import { useVerification } from '@/contexts/VerificationContext'; -import { AuthSharingService } from '@/services/auth-sharing.service'; +import { signOutUser } from '@/utils/auth'; import type { User } from '@/types/user'; import { cn } from '@/utils/styles'; import { CurrencySwitch } from './CurrencySwitch'; @@ -245,7 +245,7 @@ export function YouAccountCard({