Skip to content

Commit 0d69c01

Browse files
authored
Merge pull request dubinc#2274 from dubinc/handle-next
Improve handling of `?next=` query param
2 parents 6d19e9e + e1d5df3 commit 0d69c01

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

apps/web/lib/middleware/partners.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const AUTHENTICATED_PATHS = [
1212
];
1313

1414
export default async function PartnersMiddleware(req: NextRequest) {
15-
const { path, fullPath } = parse(req);
15+
const { path, fullPath, searchParamsObj } = parse(req);
1616

1717
const user = await getUserViaToken(req);
1818

@@ -41,7 +41,13 @@ export default async function PartnersMiddleware(req: NextRequest) {
4141

4242
if (!defaultPartnerId && !path.startsWith("/onboarding")) {
4343
return NextResponse.redirect(new URL("/onboarding", req.url));
44-
} else if (path === "/" || path.startsWith("/pn_")) {
44+
}
45+
46+
if (searchParamsObj.next) {
47+
return NextResponse.redirect(new URL(searchParamsObj.next, req.url));
48+
}
49+
50+
if (path === "/" || path.startsWith("/pn_")) {
4551
return NextResponse.redirect(new URL("/programs", req.url));
4652
} else if (isLoginPath) {
4753
// if is custom program login or register path, redirect to /programs/:programSlug

apps/web/lib/middleware/utils/parse.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ export const parse = (req: NextRequest) => {
1515

1616
// fullPath is the full URL path (along with search params)
1717
const searchParams = req.nextUrl.searchParams.toString();
18+
const searchParamsObj = Object.fromEntries(req.nextUrl.searchParams);
1819
const searchParamsString = searchParams.length > 0 ? `?${searchParams}` : "";
1920
const fullPath = `${path}${searchParamsString}`;
2021

2122
// Here, we are using decodeURIComponent to handle foreign languages like Hebrew
2223
const key = decodeURIComponent(path.split("/")[1]); // key is the first part of the path (e.g. dub.sh/stats/github -> stats)
2324
const fullKey = decodeURIComponent(path.slice(1)); // fullKey is the full path without the first slash (to account for multi-level subpaths, e.g. d.to/github/repo -> github/repo)
2425

25-
return { domain, path, fullPath, key, fullKey, searchParamsString };
26+
return {
27+
domain,
28+
path,
29+
fullPath,
30+
key,
31+
fullKey,
32+
searchParamsObj,
33+
searchParamsString,
34+
};
2635
};

apps/web/lib/middleware/workspaces.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ export default async function WorkspacesMiddleware(
88
req: NextRequest,
99
user: UserProps,
1010
) {
11-
const { path, searchParamsString } = parse(req);
11+
const { path, searchParamsObj, searchParamsString } = parse(req);
12+
13+
// special case for handling ?next= query param
14+
if (searchParamsObj.next) {
15+
return NextResponse.redirect(new URL(searchParamsObj.next, req.url));
16+
}
1217

1318
const defaultWorkspace = await getDefaultWorkspace(user);
1419

packages/email/src/templates/connect-payout-reminder.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default function ConnectPayoutReminder({
106106

107107
<Section className="mt-8 text-center">
108108
<Link
109-
href="https://partners.dub.co/settings/payouts"
109+
href="https://partners.dub.co/register?next=/settings/payouts"
110110
className="box-border block w-full rounded-md bg-black px-0 py-4 text-center text-sm font-medium leading-none text-white no-underline"
111111
>
112112
Connect payout details

0 commit comments

Comments
 (0)