11import { getTeams } from "@/api/team" ;
22import { isLoginRequired } from "@/constants/auth" ;
33import { COOKIE_ACTIVE_ACCOUNT , COOKIE_PREFIX_TOKEN } from "@/constants/cookie" ;
4+ import { cookies } from "next/headers" ;
45import { type NextRequest , NextResponse } from "next/server" ;
56import { getAddress } from "thirdweb" ;
67import { getChainMetadata } from "thirdweb/chains" ;
@@ -22,12 +23,38 @@ export const config = {
2223} ;
2324
2425export async function middleware ( request : NextRequest ) {
26+ const defaultResponse = NextResponse . next ( ) ;
2527 const { pathname } = request . nextUrl ;
2628 const activeAccount = request . cookies . get ( COOKIE_ACTIVE_ACCOUNT ) ?. value ;
2729 const authCookie = activeAccount
2830 ? request . cookies . get ( COOKIE_PREFIX_TOKEN + getAddress ( activeAccount ) )
2931 : null ;
3032
33+ // utm collection
34+ // NOTE: this is not working for pages with rewrites in next.config.js - (framer pages)
35+ // if user is already signed in - don't bother capturing utm params
36+ if ( ! authCookie ) {
37+ const searchParamsEntires = request . nextUrl . searchParams . entries ( ) ;
38+ const utmParams : Map < string , string > = new Map ( ) ;
39+ for ( const param of searchParamsEntires ) {
40+ if ( param [ 0 ] . startsWith ( "utm_" ) ) {
41+ utmParams . set ( param [ 0 ] , param [ 1 ] ) ;
42+ }
43+ }
44+
45+ // if we have utm params, set them as cookies
46+ if ( utmParams . size ) {
47+ const currentCookies = await cookies ( ) ;
48+ for ( const [ key , value ] of utmParams . entries ( ) ) {
49+ // if its already set - don't set it again
50+ if ( ! currentCookies . get ( key ) ) {
51+ currentCookies . set ( key , value ) ;
52+ defaultResponse . cookies . set ( key , value ) ;
53+ }
54+ }
55+ }
56+ }
57+
3158 // logged in paths
3259 if ( isLoginRequired ( pathname ) ) {
3360 // check if the user is logged in (has a valid auth cookie)
@@ -117,6 +144,8 @@ export async function middleware(request: NextRequest) {
117144 }
118145 // END /<address>/... case
119146 // all other cases are handled by the file system router so we just fall through
147+
148+ return defaultResponse ;
120149}
121150
122151function isPossibleEVMAddress ( address : string ) {
0 commit comments