Update supbase

This commit is contained in:
Nate Kelley 2025-08-11 16:44:25 -06:00
parent 2fee9a2311
commit 34cc2226b9
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
1 changed files with 14 additions and 14 deletions

View File

@ -30,25 +30,25 @@ export async function updateSession(request: NextRequest) {
} }
); );
// Get the session data first // Do not run code between createServerClient and
const { data: sessionData } = await supabase.auth.getSession();
// Preemptively refresh if expiring soon (within 5 minutes)
if (sessionData.session?.expires_at) {
const expiresAtTimestamp = sessionData.session.expires_at * 1000; // ms
const now = Date.now();
const timeUntilExpiry = expiresAtTimestamp - now;
const refreshWindowMs = 5 * 60 * 1000; // 5 minutes
if (timeUntilExpiry < refreshWindowMs) {
await supabase.auth.refreshSession();
}
}
// Get the user (this will use the refreshed session if we refreshed it) // Get the user (this will use the refreshed session if we refreshed it)
const { const {
data: { user } data: { user }
} = await supabase.auth.getUser(); } = await supabase.auth.getUser();
// IMPORTANT: You *must* return the supabaseResponse object as it is.
// If you're creating a new response object with NextResponse.next() make sure to:
// 1. Pass the request in it, like so:
// const myNewResponse = NextResponse.next({ request })
// 2. Copy over the cookies, like so:
// myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())
// 3. Change the myNewResponse object to fit your needs, but avoid changing
// the cookies!
// 4. Finally:
// return myNewResponse
// If this is not done, you may be causing the browser and server to go out
// of sync and terminate the user's session prematurely!
return [supabaseResponse, user] as [NextResponse, User | null]; return [supabaseResponse, user] as [NextResponse, User | null];
} }