@@ -3,25 +3,24 @@ import { NextResponse, type NextRequest } from "next/server";
33import { envContents } from "@repo/database/dbDotEnv" ;
44
55// This would allow to create Next pages gated by a login middleware,
6- // as described here: https://nextjs.org/docs/app/api-reference/file-conventions/middleware
7- // Not usable yet, waiting for ENG-373
8- // Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
6+ // as described here: https://supabase.com/docs/guides/auth/server-side/creating-a-client
97
108export const updateSession = async ( request : NextRequest ) => {
9+ let supabaseResponse = NextResponse . next ( {
10+ request,
11+ } ) ;
1112 const dbEnv = envContents ( ) ;
1213 const supabaseUrl = dbEnv . SUPABASE_URL ;
1314 const supabaseKey = dbEnv . SUPABASE_PUBLISHABLE_KEY ;
15+ if ( supabaseUrl === undefined || supabaseKey === undefined )
16+ throw new Error ( "Configuration error: supabase variables not configured." ) ;
1417
15- if ( ! supabaseUrl || ! supabaseKey ) {
16- throw new Error ( "Missing required Supabase environment variables" ) ;
17- }
18-
19- let supabaseResponse = NextResponse . next ( { request } ) ;
20-
18+ // With Fluid compute, don't put this client in a global environment
19+ // variable. Always create a new one on each request.
2120 const supabase = createServerClient ( supabaseUrl , supabaseKey , {
2221 cookies : {
2322 getAll : ( ) => request . cookies . getAll ( ) ,
24- setAll : ( cookiesToSet ) => {
23+ setAll : ( cookiesToSet , headers ) => {
2524 cookiesToSet . forEach ( ( { name, value } ) =>
2625 request . cookies . set ( name , value ) ,
2726 ) ;
@@ -31,33 +30,39 @@ export const updateSession = async (request: NextRequest) => {
3130 cookiesToSet . forEach ( ( { name, value, options } ) =>
3231 supabaseResponse . cookies . set ( name , value , options ) ,
3332 ) ;
33+ Object . entries ( headers ) . forEach ( ( [ key , value ] ) =>
34+ supabaseResponse . headers . set ( key , value ) ,
35+ ) ;
3436 } ,
3537 } ,
3638 } ) ;
3739
3840 // Do not run code between createServerClient and
39- // supabase.auth.getUser (). A simple mistake could make it very hard to debug
41+ // supabase.auth.getClaims (). A simple mistake could make it very hard to debug
4042 // issues with users being randomly logged out.
4143
42- // IMPORTANT: DO NOT REMOVE auth.getUser()
44+ // IMPORTANT: If you remove getClaims() and you use server-side rendering
45+ // with the Supabase client, your users may be randomly logged out.
46+
47+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
48+ const { data } = await supabase . auth . getClaims ( ) ;
4349
44- const {
45- data : { user } ,
46- } = await supabase . auth . getUser ( ) ;
50+ /* Wait on this until we have login
51+ const user = data?.claims;
4752
4853 if (
4954 !user &&
50- ! request . nextUrl . pathname . startsWith ( "/login" ) &&
5155 !request.nextUrl.pathname.startsWith("/auth")
5256 ) {
5357 // no user, potentially respond by redirecting the user to the login page
5458 const url = request.nextUrl.clone();
5559 url.pathname = "/auth/login";
5660 return NextResponse.redirect(url);
5761 }
62+ */
5863
59- // IMPORTANT: You *must* return the supabaseResponse object as it is.
60- // If you're creating a new response object with NextResponse.next() make sure to:
64+ // IMPORTANT: You *must* return the supabaseResponse object as it is. If you're
65+ // creating a new response object with NextResponse.next() make sure to:
6166 // 1. Pass the request in it, like so:
6267 // const myNewResponse = NextResponse.next({ request })
6368 // 2. Copy over the cookies, like so:
0 commit comments