Skip to content

Commit ff9ba30

Browse files
committed
Include current page's search params when redirecting to login page
1 parent f83d266 commit ff9ba30

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

apps/dashboard/src/@3rdweb-sdk/react/hooks/useLoggedInUser.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ export function useLoggedInUser(): {
5656
retry: false,
5757
queryFn: async () => {
5858
const searchParams = new URLSearchParams();
59-
if (pathname) {
60-
searchParams.set("pathname", pathname);
61-
}
59+
6260
if (connectedAddress) {
6361
searchParams.set("address", connectedAddress);
6462
}
@@ -76,10 +74,18 @@ export function useLoggedInUser(): {
7674
// legit use-case for now
7775
// eslint-disable-next-line no-restricted-syntax
7876
useEffect(() => {
79-
if (query.data?.redirectTo) {
80-
router.replace(query.data.redirectTo);
77+
if (query.data?.isLoggedIn === false) {
78+
// not using useSearchParams hook here to avoid adding Suspense boundaries everywhere this hook is being used
79+
const currentHref = new URL(window.location.href);
80+
const currentPathname = currentHref.pathname;
81+
const currentSearchParams = currentHref.searchParams.toString();
82+
router.replace(
83+
buildLoginPath(
84+
`${currentPathname}${currentSearchParams ? `?${currentSearchParams}` : ""}`,
85+
),
86+
);
8187
}
82-
}, [query.data?.redirectTo, router]);
88+
}, [query.data?.isLoggedIn, router]);
8389

8490
// if we are "disconnected" we are not logged in
8591
if (connectionStatus === "disconnected") {
@@ -135,3 +141,7 @@ export function useLoggedInUser(): {
135141
: null,
136142
};
137143
}
144+
145+
function buildLoginPath(pathname: string | undefined): string {
146+
return `/login${pathname ? `?next=${encodeURIComponent(pathname)}` : ""}`;
147+
}

apps/dashboard/src/app/api/auth/ensure-login/route.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@ import { cookies } from "next/headers";
44
import { type NextRequest, NextResponse } from "next/server";
55
import { getAddress } from "thirdweb/utils";
66

7-
export type EnsureLoginPayload = {
8-
pathname: string;
9-
address?: string;
10-
};
11-
127
export type EnsureLoginResponse = {
138
isLoggedIn: boolean;
149
jwt?: string;
15-
redirectTo?: string;
1610
};
1711

1812
export const GET = async (req: NextRequest) => {
1913
const address = req.nextUrl.searchParams.get("address");
20-
const pathname = req.nextUrl.searchParams.get("pathname");
2114

2215
const cookieStore = cookies();
2316
// if we are "disconnected" we are not logged in, clear the cookie and redirect to login
@@ -34,7 +27,6 @@ export const GET = async (req: NextRequest) => {
3427
cookieStore.delete(COOKIE_ACTIVE_ACCOUNT);
3528
return NextResponse.json({
3629
isLoggedIn: false,
37-
redirectTo: buildLoginPath(pathname),
3830
});
3931
}
4032

@@ -49,7 +41,6 @@ export const GET = async (req: NextRequest) => {
4941
cookieStore.delete(COOKIE_ACTIVE_ACCOUNT);
5042
return NextResponse.json({
5143
isLoggedIn: false,
52-
redirectTo: buildLoginPath(pathname),
5344
});
5445
}
5546

@@ -65,7 +56,6 @@ export const GET = async (req: NextRequest) => {
6556
cookieStore.delete(authCookieName);
6657
return NextResponse.json({
6758
isLoggedIn: false,
68-
redirectTo: buildLoginPath(pathname),
6959
});
7060
}
7161

@@ -87,7 +77,3 @@ export const GET = async (req: NextRequest) => {
8777
// if everything is good simply return true
8878
return NextResponse.json({ isLoggedIn: true, jwt: token });
8979
};
90-
91-
function buildLoginPath(pathname?: string | null): string {
92-
return `/login${pathname ? `?next=${encodeURIComponent(pathname)}` : ""}`;
93-
}

apps/dashboard/src/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ export async function middleware(request: NextRequest) {
3232
// check if the user is logged in (has a valid auth cookie)
3333

3434
if (!authCookie) {
35+
const searchParamsString = request.nextUrl.searchParams.toString();
36+
3537
// if not logged in, rewrite to login page
3638
return redirect(
3739
request,
3840
"/login",
39-
`next=${encodeURIComponent(pathname)}`,
41+
`next=${encodeURIComponent(`${pathname}${searchParamsString ? `?${searchParamsString}` : ""}`)}`,
4042
false,
4143
);
4244
}

0 commit comments

Comments
 (0)