@@ -2,6 +2,15 @@ import { createServerClient } from '@supabase/ssr';
22import { NextResponse , type NextRequest } from 'next/server' ;
33
44export 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}
0 commit comments