|
1 | 1 | import { getTeams } from "@/api/team"; |
2 | 2 | import { isLoginRequired } from "@/constants/auth"; |
3 | 3 | import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie"; |
| 4 | +import { cookies } from "next/headers"; |
4 | 5 | import { type NextRequest, NextResponse } from "next/server"; |
5 | 6 | import { getAddress } from "thirdweb"; |
6 | 7 | import { getChainMetadata } from "thirdweb/chains"; |
@@ -28,6 +29,30 @@ export async function middleware(request: NextRequest) { |
28 | 29 | ? request.cookies.get(COOKIE_PREFIX_TOKEN + getAddress(activeAccount)) |
29 | 30 | : null; |
30 | 31 |
|
| 32 | + // utm collection |
| 33 | + // NOTE: this is not working for pages with rewrites in next.config.js - (framer pages) |
| 34 | + // if user is already signed in - don't bother capturing utm params |
| 35 | + if (!authCookie) { |
| 36 | + const searchParamsEntires = request.nextUrl.searchParams.entries(); |
| 37 | + const utmParams: Map<string, string> = new Map(); |
| 38 | + for (const param of searchParamsEntires) { |
| 39 | + if (param[0].startsWith("utm_")) { |
| 40 | + utmParams.set(param[0], param[1]); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // if we have utm params, set them as cookies |
| 45 | + if (utmParams.size) { |
| 46 | + const currentCookies = await cookies(); |
| 47 | + for (const [key, value] of utmParams.entries()) { |
| 48 | + // if its already set - don't set it again |
| 49 | + if (!currentCookies.get(key)) { |
| 50 | + currentCookies.set(key, value); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
31 | 56 | // logged in paths |
32 | 57 | if (isLoginRequired(pathname)) { |
33 | 58 | // check if the user is logged in (has a valid auth cookie) |
|
0 commit comments