Skip to content

Commit 39fd24f

Browse files
authored
Fix disconnect with walletConnect (#300)
1 parent b5f98a2 commit 39fd24f

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

packages/nextjs/app/admin/_components/AccessDenied.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { signOut } from "next-auth/react";
55
import { useDisconnect } from "wagmi";
66

77
export const AccessDenied = () => {
8-
const { disconnect } = useDisconnect();
8+
const { disconnectAsync } = useDisconnect();
99
return (
1010
<div className="flex flex-col items-center px-4 py-10 sm:py-20">
1111
<h1 className="text-3xl text-center font-extrabold mb-1">Access Denied</h1>
@@ -16,9 +16,13 @@ export const AccessDenied = () => {
1616
</Link>
1717
<button
1818
className="btn btn-primary border-2 border-primary"
19-
onClick={() => {
20-
disconnect();
21-
signOut();
19+
onClick={async () => {
20+
try {
21+
await disconnectAsync();
22+
await signOut();
23+
} catch (error) {
24+
console.error("Error during disconnecting/signing out:", error);
25+
}
2226
}}
2327
>
2428
Disconnect

packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/AddressInfoDropdown.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const AddressInfoDropdown = ({
3737
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3838
blockExplorerAddressLink,
3939
}: AddressInfoDropdownProps) => {
40-
const { disconnect } = useDisconnect();
40+
const { disconnectAsync } = useDisconnect();
4141
const checkSumAddress = getAddress(address);
4242

4343
const [addressCopied, setAddressCopied] = useState(false);
@@ -129,9 +129,13 @@ export const AddressInfoDropdown = ({
129129
<button
130130
className="menu-item text-error btn-sm !rounded-xl flex gap-3 py-3"
131131
type="button"
132-
onClick={() => {
133-
disconnect();
134-
signOut();
132+
onClick={async () => {
133+
try {
134+
await disconnectAsync();
135+
await signOut();
136+
} catch (error) {
137+
console.error("Error during disconnecting/signing out:", error);
138+
}
135139
}}
136140
>
137141
<ArrowLeftEndOnRectangleIcon className="h-6 w-4" /> <span>Disconnect</span>

packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/WrongNetworkDropdown.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { NetworkOptions } from "./NetworkOptions";
2+
import { signOut } from "next-auth/react";
23
import { useDisconnect } from "wagmi";
34
import { ArrowLeftOnRectangleIcon, ChevronDownIcon } from "@heroicons/react/24/outline";
45

56
export const WrongNetworkDropdown = () => {
6-
const { disconnect } = useDisconnect();
7+
const { disconnectAsync } = useDisconnect();
78

89
return (
910
<div className="dropdown dropdown-end mr-2">
@@ -20,7 +21,14 @@ export const WrongNetworkDropdown = () => {
2021
<button
2122
className="menu-item text-error btn-sm !rounded-xl flex gap-3 py-3"
2223
type="button"
23-
onClick={() => disconnect()}
24+
onClick={async () => {
25+
try {
26+
await disconnectAsync();
27+
await signOut();
28+
} catch (error) {
29+
console.error("Error during disconnecting/signing out:", error);
30+
}
31+
}}
2432
>
2533
<ArrowLeftOnRectangleIcon className="h-6 w-4 ml-2 sm:ml-0" />
2634
<span>Disconnect</span>

packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/index.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ export const RainbowKitCustomConnectButton = () => {
2525
const { targetNetwork } = useTargetNetwork();
2626
const { address: connectedAddress } = useAccount();
2727
const { data: user, isLoading: isLoadingUser } = useUser(connectedAddress);
28-
const { disconnect } = useDisconnect();
28+
const { disconnectAsync } = useDisconnect();
2929
const { userAddress: sessionUserAddress } = useAuthSession();
3030
const isAdmin = user?.role === UserRole.ADMIN;
3131
const { isAdmin: isAdminFromSession } = useAuthSession();
3232

3333
useEffect(() => {
34-
if (sessionUserAddress && connectedAddress && connectedAddress !== sessionUserAddress) {
35-
disconnect();
36-
signOut({ redirect: true, callbackUrl: "/" });
37-
}
38-
}, [connectedAddress, sessionUserAddress, disconnect]);
34+
const disconnectIfWrongAddress = async () => {
35+
if (sessionUserAddress && connectedAddress && connectedAddress !== sessionUserAddress) {
36+
try {
37+
await disconnectAsync();
38+
await signOut({ redirect: true, callbackUrl: "/" });
39+
} catch (error) {
40+
console.error("Error during disconnecting/signing out:", error);
41+
}
42+
}
43+
};
44+
45+
disconnectIfWrongAddress();
46+
}, [connectedAddress, sessionUserAddress, disconnectAsync]);
3947

4048
return (
4149
<ConnectButton.Custom>
@@ -69,7 +77,16 @@ export const RainbowKitCustomConnectButton = () => {
6977
return (
7078
<div className="flex items-baseline gap-4">
7179
<RegisterUser />
72-
<button className="flex items-center rounded-full bg-base-300" onClick={() => disconnect()}>
80+
<button
81+
className="flex items-center rounded-full bg-base-300"
82+
onClick={async () => {
83+
try {
84+
await disconnectAsync();
85+
} catch (error) {
86+
console.error("Error during disconnecting:", error);
87+
}
88+
}}
89+
>
7390
<XMarkIcon className="w-6 h-6" />
7491
</button>
7592
</div>

0 commit comments

Comments
 (0)