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/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); - } -} 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/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({