Skip to content

Commit 4573e2c

Browse files
committed
[Dashboard] Remove useLoggedInUser and fix login loop (#5695)
Fixes: DASH-577
1 parent 324e602 commit 4573e2c

File tree

245 files changed

+2558
-1182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+2558
-1182
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use server";
2+
3+
import type { Account } from "@3rdweb-sdk/react/hooks/useApi";
4+
import { cookies } from "next/headers";
5+
import { getAddress } from "thirdweb";
6+
import { COOKIE_PREFIX_TOKEN } from "../constants/cookie";
7+
import { API_SERVER_URL } from "../constants/env";
8+
9+
/**
10+
* Check that the connected wallet is valid for the current active account
11+
*/
12+
export async function isWalletValidForActiveAccount(params: {
13+
address: string;
14+
account: Account;
15+
authToken: string;
16+
}) {
17+
const cookieStore = await cookies();
18+
const authCookieNameForAddress =
19+
COOKIE_PREFIX_TOKEN + getAddress(params.address);
20+
21+
// authToken for this wallet address should be present
22+
const authTokenForAddress = cookieStore.get(authCookieNameForAddress)?.value;
23+
if (!authTokenForAddress) {
24+
return false;
25+
}
26+
27+
// this authToken should be same as current active authToken
28+
if (authTokenForAddress !== params.authToken) {
29+
return false;
30+
}
31+
32+
// authToken should be valid
33+
const accountRes = await fetch(`${API_SERVER_URL}/v1/account/me`, {
34+
method: "GET",
35+
headers: {
36+
Authorization: `Bearer ${authTokenForAddress}`,
37+
},
38+
});
39+
40+
if (accountRes.status !== 200) {
41+
return false;
42+
}
43+
44+
const account = (await accountRes.json()).data as Account;
45+
46+
// the current account should match the account fetched for the authToken
47+
return account.id === params.account.id;
48+
}

apps/dashboard/src/@3rdweb-sdk/react/components/connect-wallet/index.tsx

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"use client";
22

3-
import { Spinner } from "@/components/ui/Spinner/Spinner";
43
import { Button } from "@/components/ui/button";
54
import { useThirdwebClient } from "@/constants/thirdweb.client";
65
import { useStore } from "@/lib/reactive";
7-
import { cn } from "@/lib/utils";
86
import { getSDKTheme } from "app/components/sdk-component-theme";
97
import { CustomChainRenderer } from "components/selects/CustomChainRenderer";
108
import { mapV4ChainToV5Chain } from "contexts/map-chains";
@@ -18,6 +16,7 @@ import type { Chain } from "thirdweb";
1816
import {
1917
ConnectButton,
2018
type NetworkSelectorProps,
19+
useActiveAccount,
2120
useConnectModal,
2221
} from "thirdweb/react";
2322
import { useFavoriteChainIds } from "../../../../app/(dashboard)/(chain)/components/client/star-button";
@@ -29,16 +28,15 @@ import {
2928
addRecentlyUsedChainId,
3029
recentlyUsedChainIdsStore,
3130
} from "../../../../stores/chainStores";
32-
import { useLoggedInUser } from "../../hooks/useLoggedInUser";
3331
import { popularChains } from "../popularChains";
3432

3533
export const CustomConnectWallet = (props: {
3634
loginRequired?: boolean;
3735
connectButtonClassName?: string;
3836
signInLinkButtonClassName?: string;
3937
detailsButtonClassName?: string;
40-
loadingButtonClassName?: string;
4138
chain?: Chain;
39+
isLoggedIn: boolean;
4240
}) => {
4341
const thirdwebClient = useThirdwebClient();
4442
const loginRequired =
@@ -120,25 +118,11 @@ export const CustomConnectWallet = (props: {
120118
]);
121119

122120
// ensures login status on pages that need it
123-
const { isPending, isLoggedIn } = useLoggedInUser();
121+
const { isLoggedIn } = props;
124122
const pathname = usePathname();
123+
const account = useActiveAccount();
125124

126-
if (isPending) {
127-
return (
128-
<>
129-
<div
130-
className={cn(
131-
"flex h-[48px] w-[144px] items-center justify-center rounded-lg border border-border bg-muted",
132-
props.loadingButtonClassName,
133-
)}
134-
>
135-
<Spinner className="size-4" />
136-
</div>
137-
</>
138-
);
139-
}
140-
141-
if (!isLoggedIn && loginRequired) {
125+
if ((!isLoggedIn || !account) && loginRequired) {
142126
return (
143127
<>
144128
<Button

0 commit comments

Comments
 (0)