@@ -7,6 +7,20 @@ import { defineDashboardChain } from "@/lib/defineDashboardChain";
77import { isLoginRequired } from "@/utils/auth" ;
88import { LAST_VISITED_TEAM_PAGE_PATH } from "./app/(app)/team/components/last-visited-page/consts" ;
99
10+ type CookiesToSet = Record <
11+ string ,
12+ | [ string ]
13+ | [
14+ string ,
15+ {
16+ httpOnly : boolean ;
17+ sameSite ?: "lax" | "strict" | "none" ;
18+ secure : boolean ;
19+ maxAge : number ;
20+ } ,
21+ ]
22+ > ;
23+
1024// ignore assets, api - only intercept page routes
1125export const config = {
1226 matcher : [
@@ -24,12 +38,12 @@ export const config = {
2438} ;
2539
2640export async function middleware ( request : NextRequest ) {
27- const { pathname } = request . nextUrl ;
41+ const { pathname, searchParams } = request . nextUrl ;
2842
2943 // nebula subdomain handling
3044 const paths = pathname . slice ( 1 ) . split ( "/" ) ;
3145
32- let cookiesToSet : Record < string , string > | undefined ;
46+ let cookiesToSet : CookiesToSet = { } ;
3347
3448 const activeAccount = request . cookies . get ( COOKIE_ACTIVE_ACCOUNT ) ?. value ;
3549 const authCookie = activeAccount
@@ -56,12 +70,27 @@ export async function middleware(request: NextRequest) {
5670 cookiesToSet = { } ;
5771 }
5872
59- cookiesToSet [ key ] = value ;
73+ cookiesToSet [ key ] = [ value ] ;
6074 }
6175 }
6276 }
6377 }
6478
79+ // handle gclid (if it exists in search params)
80+ const gclid = searchParams . get ( "gclid" ) ;
81+ if ( gclid ) {
82+ cookiesToSet . gclid = [
83+ gclid ,
84+ {
85+ // TODO: define conversion window, for now 7d should do fine
86+ maxAge : 7 * 24 * 60 * 60 ,
87+ httpOnly : false ,
88+ sameSite : "lax" ,
89+ secure : true ,
90+ } ,
91+ ] ;
92+ }
93+
6594 // logged in paths
6695 if ( isLoginRequired ( pathname ) ) {
6796 // check if the user is logged in (has a valid auth cookie)
@@ -146,7 +175,7 @@ export async function middleware(request: NextRequest) {
146175 if ( cookiesToSet ) {
147176 const defaultResponse = NextResponse . next ( ) ;
148177 for ( const entry of Object . entries ( cookiesToSet ) ) {
149- defaultResponse . cookies . set ( entry [ 0 ] , entry [ 1 ] ) ;
178+ defaultResponse . cookies . set ( entry [ 0 ] , entry [ 1 ] [ 0 ] , entry [ 1 ] [ 1 ] ) ;
150179 }
151180
152181 return defaultResponse ;
@@ -162,15 +191,15 @@ function isPossibleAddressOrENSName(address: string) {
162191function rewrite (
163192 request : NextRequest ,
164193 relativePath : string ,
165- cookiesToSet : Record < string , string > | undefined ,
194+ cookiesToSet : CookiesToSet ,
166195) {
167196 const url = request . nextUrl . clone ( ) ;
168197 url . pathname = relativePath ;
169198 const res = NextResponse . rewrite ( url ) ;
170199
171200 if ( cookiesToSet ) {
172201 for ( const entry of Object . entries ( cookiesToSet ) ) {
173- res . cookies . set ( entry [ 0 ] , entry [ 1 ] ) ;
202+ res . cookies . set ( entry [ 0 ] , entry [ 1 ] [ 0 ] , entry [ 1 ] [ 1 ] ) ;
174203 }
175204 }
176205
@@ -184,7 +213,7 @@ function redirect(
184213 | {
185214 searchParams ?: string ;
186215 permanent ?: boolean ;
187- cookiesToSet ?: Record < string , string > | undefined ;
216+ cookiesToSet ?: CookiesToSet ;
188217 }
189218 | undefined ,
190219) {
@@ -196,7 +225,7 @@ function redirect(
196225
197226 if ( options ?. cookiesToSet ) {
198227 for ( const entry of Object . entries ( options . cookiesToSet ) ) {
199- res . cookies . set ( entry [ 0 ] , entry [ 1 ] ) ;
228+ res . cookies . set ( entry [ 0 ] , entry [ 1 ] [ 0 ] , entry [ 1 ] [ 1 ] ) ;
200229 }
201230 }
202231
0 commit comments