Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/dashboard/src/@/actions/stripe-actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "server-only";

import { headers } from "next/headers";
import { cookies, headers } from "next/headers";
import Stripe from "stripe";
import type { Team } from "@/api/team/get-team";
import {
Expand Down Expand Up @@ -88,6 +88,9 @@ export async function fetchClientSecret(team: Team) {
throw new Error("No customer ID found");
}

// try to get the gclid cookie
const gclid = (await cookies()).get("gclid")?.value;

// Create Checkout Sessions from body params.
const session = await stripe.checkout.sessions.create({
ui_mode: "embedded",
Expand Down Expand Up @@ -117,6 +120,8 @@ export async function fetchClientSecret(team: Team) {
missing_payment_method: "cancel",
},
},
// if gclid exists, set it as a metadata field so we can attribute the conversion later
metadata: gclid ? { gclid } : undefined,
},
});

Expand Down
45 changes: 37 additions & 8 deletions apps/dashboard/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import { defineDashboardChain } from "@/lib/defineDashboardChain";
import { isLoginRequired } from "@/utils/auth";
import { LAST_VISITED_TEAM_PAGE_PATH } from "./app/(app)/team/components/last-visited-page/consts";

type CookiesToSet = Record<
string,
| [string]
| [
string,
{
httpOnly: boolean;
sameSite?: "lax" | "strict" | "none";
secure: boolean;
maxAge: number;
},
]
>;

// ignore assets, api - only intercept page routes
export const config = {
matcher: [
Expand All @@ -24,12 +38,12 @@ export const config = {
};

export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const { pathname, searchParams } = request.nextUrl;

// nebula subdomain handling
const paths = pathname.slice(1).split("/");

let cookiesToSet: Record<string, string> | undefined;
let cookiesToSet: CookiesToSet = {};

const activeAccount = request.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value;
const authCookie = activeAccount
Expand All @@ -56,12 +70,27 @@ export async function middleware(request: NextRequest) {
cookiesToSet = {};
}

cookiesToSet[key] = value;
cookiesToSet[key] = [value];
}
}
}
}

// handle gclid (if it exists in search params)
const gclid = searchParams.get("gclid");
if (gclid) {
cookiesToSet.gclid = [
gclid,
{
// TODO: define conversion window, for now 7d should do fine
maxAge: 7 * 24 * 60 * 60,
httpOnly: false,
sameSite: "lax",
secure: true,
},
];
}

// logged in paths
if (isLoginRequired(pathname)) {
// check if the user is logged in (has a valid auth cookie)
Expand Down Expand Up @@ -146,7 +175,7 @@ export async function middleware(request: NextRequest) {
if (cookiesToSet) {
const defaultResponse = NextResponse.next();
for (const entry of Object.entries(cookiesToSet)) {
defaultResponse.cookies.set(entry[0], entry[1]);
defaultResponse.cookies.set(entry[0], entry[1][0], entry[1][1]);
}

return defaultResponse;
Expand All @@ -162,15 +191,15 @@ function isPossibleAddressOrENSName(address: string) {
function rewrite(
request: NextRequest,
relativePath: string,
cookiesToSet: Record<string, string> | undefined,
cookiesToSet: CookiesToSet,
) {
const url = request.nextUrl.clone();
url.pathname = relativePath;
const res = NextResponse.rewrite(url);

if (cookiesToSet) {
for (const entry of Object.entries(cookiesToSet)) {
res.cookies.set(entry[0], entry[1]);
res.cookies.set(entry[0], entry[1][0], entry[1][1]);
}
}

Expand All @@ -184,7 +213,7 @@ function redirect(
| {
searchParams?: string;
permanent?: boolean;
cookiesToSet?: Record<string, string> | undefined;
cookiesToSet?: CookiesToSet;
}
| undefined,
) {
Expand All @@ -196,7 +225,7 @@ function redirect(

if (options?.cookiesToSet) {
for (const entry of Object.entries(options.cookiesToSet)) {
res.cookies.set(entry[0], entry[1]);
res.cookies.set(entry[0], entry[1][0], entry[1][1]);
}
}

Expand Down
Loading