Skip to content

Commit 8316d61

Browse files
committed
Move auth root redirects to middleware and harden hydration
1 parent 357852e commit 8316d61

4 files changed

Lines changed: 20 additions & 29 deletions

File tree

src/app/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export default function RootLayout({
3636
children: React.ReactNode;
3737
}>) {
3838
return (
39-
<html lang="en" className="dark">
40-
<body className="font-sans antialiased">
39+
<html lang="en" className="dark" suppressHydrationWarning>
40+
<body className="font-sans antialiased" suppressHydrationWarning>
4141
{children}
4242
</body>
4343
</html>

src/app/page.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useEffect, useRef, useState } from 'react';
3+
import { useRef, useState } from 'react';
44
import { useInView, motion, AnimatePresence } from 'framer-motion';
55
import {
66
Check,
@@ -21,7 +21,6 @@ import {
2121
TrendingUp,
2222
} from 'lucide-react';
2323
import Link from 'next/link';
24-
import { createClient } from '@/lib/supabase/client';
2524

2625
/* ─────────────────────────────────────────────────────────
2726
ANIMATION PRIMITIVES
@@ -601,28 +600,6 @@ const proPlanFeatures = [
601600
═════════════════════════════════════════════════════════ */
602601

603602
export default function LandingPage() {
604-
useEffect(() => {
605-
const url = new URL(window.location.href);
606-
const code = url.searchParams.get('code');
607-
const tokenHash = url.searchParams.get('token_hash');
608-
609-
// If auth params accidentally land on "/", forward to callback to complete session exchange.
610-
if (code || tokenHash) {
611-
window.location.replace(`/auth/callback${url.search}`);
612-
return;
613-
}
614-
615-
// Keep logged-in users inside the app instead of showing marketing landing.
616-
const supabase = createClient();
617-
void supabase.auth.getUser().then(({ data: { user } }) => {
618-
if (user) {
619-
window.location.replace('/app/today');
620-
}
621-
}).catch(() => {
622-
// Ignore errors here; unauthenticated users should still see the landing page.
623-
});
624-
}, []);
625-
626603
return (
627604
<div className="min-h-screen bg-bg-primary font-sans relative">
628605
{/* Noise texture */}

src/lib/supabase/middleware.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { createServerClient } from '@supabase/ssr';
22
import { NextResponse, type NextRequest } from 'next/server';
33

44
export async function updateSession(request: NextRequest) {
5+
const { pathname, searchParams } = request.nextUrl;
6+
7+
// If OAuth params land on "/", forward to callback route before rendering landing page.
8+
if (pathname === '/' && (searchParams.get('code') || searchParams.get('token_hash'))) {
9+
const url = request.nextUrl.clone();
10+
url.pathname = '/auth/callback';
11+
return NextResponse.redirect(url);
12+
}
13+
514
let supabaseResponse = NextResponse.next({ request });
615

716
const supabase = createServerClient(
@@ -29,8 +38,6 @@ export async function updateSession(request: NextRequest) {
2938
data: { user },
3039
} = await supabase.auth.getUser();
3140

32-
const { pathname } = request.nextUrl;
33-
3441
// /reset-password requires a valid recovery session — allow if user is present
3542
// (user gets set from the recovery token exchanged at /auth/callback).
3643
// If no session, redirect to login instead of showing a broken form.
@@ -54,5 +61,12 @@ export async function updateSession(request: NextRequest) {
5461
return NextResponse.redirect(url);
5562
}
5663

64+
// Redirect authenticated users away from landing into app.
65+
if (pathname === '/' && user) {
66+
const url = request.nextUrl.clone();
67+
url.pathname = '/app/today';
68+
return NextResponse.redirect(url);
69+
}
70+
5771
return supabaseResponse;
5872
}

src/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export async function proxy(request: NextRequest) {
66
}
77

88
export const config = {
9-
matcher: ['/app/:path*', '/login', '/signup', '/onboarding', '/reset-password'],
9+
matcher: ['/', '/app/:path*', '/login', '/signup', '/onboarding', '/reset-password'],
1010
};

0 commit comments

Comments
 (0)