Skip to content

Commit 715d073

Browse files
committed
refactor(wallet-ui): redirect to ecosystem subroute once
1 parent 6771cfe commit 715d073

File tree

8 files changed

+48
-45
lines changed

8 files changed

+48
-45
lines changed

apps/wallet-ui/src/app/[ecosystem]/(authed)/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default async function Layout(props: {
1212

1313
const { children } = props;
1414

15-
await authedOnly();
15+
await authedOnly(params.ecosystem);
1616
const ecosystem = await getEcosystemInfo(params.ecosystem);
1717
return (
1818
<div className="flex w-full flex-col items-stretch">

apps/wallet-ui/src/app/[ecosystem]/login/layout.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { getCurrentUser } from "../../../lib/auth";
33

44
export default async function Layout({
55
children,
6-
}: { children: React.ReactNode }) {
6+
params,
7+
}: { children: React.ReactNode; params: Promise<{ ecosystem: string }> }) {
8+
const { ecosystem } = await params;
79
const userAddress = await getCurrentUser();
810
if (userAddress) {
9-
redirect(`/wallet/${userAddress}`);
11+
redirect(`${ecosystem}/wallet/${userAddress}`);
1012
}
1113

1214
return (
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { getCurrentUser } from "@/lib/auth";
22
import { redirect } from "next/navigation";
33

4-
export default async function Page() {
5-
const user = await getCurrentUser();
6-
if (user) {
7-
redirect("/wallet/${user}");
4+
export default async function Page({
5+
params,
6+
}: { params: Promise<{ ecosystem: string }> }) {
7+
const { ecosystem } = await params;
8+
const address = await getCurrentUser();
9+
if (address) {
10+
redirect(`${ecosystem}/wallet/${address}`);
811
}
9-
redirect("/login");
12+
redirect(`${ecosystem}/login`);
1013
}

apps/wallet-ui/src/app/[ecosystem]/wc/page.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import { redirect } from "next/navigation";
44

55
export default async function Page(props: {
66
searchParams: Promise<{ uri: string }>;
7+
params: Promise<{ ecosystem: string }>;
78
}) {
8-
const searchParams = await props.searchParams;
9-
10-
const { uri } = searchParams;
9+
const [{ uri }, { ecosystem }] = await Promise.all([
10+
props.searchParams,
11+
props.params,
12+
]);
1113

1214
const currentUser = await getCurrentUser();
1315

1416
if (!currentUser) {
15-
redirect(`/login?uri=${encodeURIComponent(uri)}`);
17+
redirect(`${ecosystem}/login?uri=${encodeURIComponent(uri)}`);
1618
}
1719

18-
redirect(`/wallet/${currentUser}?uri=${encodeURIComponent(uri)}`);
20+
redirect(`${ecosystem}/wallet/${currentUser}?uri=${encodeURIComponent(uri)}`);
1921
}

apps/wallet-ui/src/components/ConnectButton.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use client";
2+
import { logout } from "@/lib/auth";
23
import { client } from "@/lib/client";
34
import { useTheme } from "next-themes";
45
import {
@@ -23,6 +24,9 @@ export default function ConnectButton({
2324
wallets={[ecosystemWallet(ecosystem)]}
2425
client={client}
2526
theme={theme === "light" ? "light" : "dark"}
27+
onDisconnect={() => {
28+
logout();
29+
}}
2630
/>
2731
);
2832
}

apps/wallet-ui/src/components/ConnectEmbed.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function ConnectEmbed() {
2525
const success = await login(loginParams);
2626
if (success) {
2727
router.push(
28-
`/wallet/${loginParams.payload.address}?${searchParams.toString()}`,
28+
`/${params.ecosystem}/wallet/${loginParams.payload.address}?${searchParams.toString()}`,
2929
);
3030
}
3131
},

apps/wallet-ui/src/lib/auth.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,23 @@ export async function login(payload: VerifyLoginPayloadParams) {
4141
secure: process.env.NODE_ENV !== "development",
4242
sameSite: "strict",
4343
maxAge: 3600,
44-
domain: process.env.NEXT_PUBLIC_ROOT_DOMAIN,
44+
domain:
45+
process.env.NODE_ENV === "development"
46+
? "localhost"
47+
: process.env.NEXT_PUBLIC_ROOT_DOMAIN,
4548
path: "/",
4649
});
4750
return true;
4851
}
4952
return false;
5053
}
5154

52-
export async function authedOnly() {
55+
export async function authedOnly(ecosystem?: string) {
5356
const loggedIn = await getCurrentUser();
5457
if (loggedIn) {
5558
return;
5659
}
57-
redirect("/login");
60+
redirect(`/${ecosystem || ""}/login`);
5861
}
5962

6063
export async function isLoggedIn(): Promise<boolean> {

apps/wallet-ui/src/middleware.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
import { type NextRequest, NextResponse } from "next/server";
22

3-
export const config = {
4-
matcher: [
5-
/*
6-
* Match all paths except for:
7-
* 1. /api routes
8-
* 2. /_next (Next.js internals)
9-
* 3. /_static (inside /public)
10-
* 4. all root files inside /public (e.g. /favicon.ico)
11-
*/
12-
"/((?!api/|_next/|_static/|_vercel|[\\w-]+\\.\\w+).*)",
13-
],
14-
};
3+
export const config = { matcher: "/((?!.*\\.).*)" };
154

16-
const ROOT_DOMAIN = process.env.NEXT_PUBLIC_ROOT_DOMAIN;
17-
export default async function middleware(req: NextRequest) {
18-
// Get the request hostname (e.g. demo.thirdweb.com)
19-
const hostname = req.headers.get("host");
5+
export function middleware(request: NextRequest) {
6+
const url = request.nextUrl;
7+
const hostname = request.headers.get("host") || "";
208

21-
const searchParams = req.nextUrl.searchParams.toString();
22-
const url = req.nextUrl;
23-
const path = `${url.pathname}${
24-
searchParams.length > 0 ? `?${searchParams}` : ""
25-
}`;
9+
// Match pattern: something.ecosystem.domain.tld
10+
const match = hostname.match(/^([^.]+)\.ecosystem\.([^.]+\.[^.]+)$/);
2611

27-
// keep root application at `/`
28-
if (hostname === ROOT_DOMAIN || hostname === null) {
29-
return NextResponse.next();
30-
}
12+
if (match) {
13+
const [_, subdomain, primaryDomain] = match;
14+
15+
// Redirect to ecosystem.domain.tld/subdomain
16+
const newUrl = new URL(`${url.protocol}//ecosystem.${primaryDomain}`);
17+
newUrl.pathname = `/${subdomain}${url.pathname}`;
18+
newUrl.search = url.search;
3119

32-
// rewrite everything else to `/[ecosystem]/... dynamic route
33-
const ecosystem = hostname.split(".")[0];
20+
// 308 is permanent redirect, preserves request method
21+
return NextResponse.redirect(newUrl, 308);
22+
}
3423

35-
return NextResponse.rewrite(new URL(`/${ecosystem}${path}`, req.url));
24+
return NextResponse.next();
3625
}

0 commit comments

Comments
 (0)