Skip to content

Commit f291bdb

Browse files
committed
fix(wallet): disconnect both Privy session and wagmi connector
1 parent e073542 commit f291bdb

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

packages/frontend/src/components/wallet/wallet-balance-dropdown.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import { useState, useEffect } from 'react'
4-
import { useAccount } from 'wagmi'
4+
import { useAccount, useDisconnect } from 'wagmi'
55
import { usePrivy } from '@privy-io/react-auth'
66
import { useRouter } from 'next/navigation'
77
import { useTokenBalances, useSenseUsername } from '@/hooks/useTokenBalances'
@@ -37,10 +37,18 @@ type BalanceView = 'total' | 'network' | 'tokens'
3737

3838
export function WalletBalanceDropdown() {
3939
const { address, isConnected } = useAccount()
40-
// Disconnect routes through Privy's `logout` so the embedded-wallet
41-
// session ends. wagmi's useDisconnect alone would only drop the wagmi
42-
// connector and leave the Privy session live.
40+
// Disconnect needs to terminate both possible session types:
41+
// - Privy session (embedded wallet, Stage 1 default)
42+
// - wagmi connector session (legacy MetaMask / injected, or any cached
43+
// state from before the Privy migration).
44+
// Calling both is safe — each is a no-op if the corresponding session
45+
// doesn't exist. Calling only one leaves the other stuck.
4346
const { logout } = usePrivy()
47+
const { disconnect } = useDisconnect()
48+
const handleDisconnect = async () => {
49+
try { await logout() } catch {}
50+
try { disconnect() } catch {}
51+
}
4452
const router = useRouter()
4553
const { balances, isLoading, ethBalance, gameBalance, usdcBalance } = useTokenBalances()
4654
const { username: senseUsername, isLoading: usernameLoading } = useSenseUsername(address)
@@ -320,7 +328,7 @@ export function WalletBalanceDropdown() {
320328
<DropdownMenuSeparator />
321329

322330
{/* Disconnect */}
323-
<DropdownMenuItem onClick={() => logout()} className="text-red-600 focus:text-red-600">
331+
<DropdownMenuItem onClick={handleDisconnect} className="text-red-600 focus:text-red-600">
324332
<LogOut className="h-4 w-4 mr-2" />
325333
Disconnect Wallet
326334
</DropdownMenuItem>

packages/frontend/src/components/wallet/wallet-connection.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

33
import { useState } from 'react'
4+
import { useDisconnect } from 'wagmi'
45
import { usePrivy } from '@privy-io/react-auth'
56
import { Button } from '@/components/ui/button'
67
import {
@@ -34,6 +35,9 @@ interface WalletConnectionProps {
3435
export function WalletConnection({ children, className }: WalletConnectionProps) {
3536
const [isOpen, setIsOpen] = useState(false)
3637
const { ready, authenticated, login, logout, user } = usePrivy()
38+
// wagmi disconnect handles legacy/injected sessions that Privy's logout
39+
// doesn't know about — see comment in wallet-balance-dropdown.
40+
const { disconnect } = useDisconnect()
3741

3842
// Privy initialises async; consuming auth state before `ready` flickers
3943
// and can race with login. The skill flags this as the most common
@@ -55,12 +59,9 @@ export function WalletConnection({ children, className }: WalletConnectionProps)
5559
}
5660

5761
const handleLogout = async () => {
58-
try {
59-
await logout()
60-
setIsOpen(false)
61-
} catch (err) {
62-
console.error('Privy logout failed:', err)
63-
}
62+
try { await logout() } catch (err) { console.error('Privy logout failed:', err) }
63+
try { disconnect() } catch (err) { console.error('wagmi disconnect failed:', err) }
64+
setIsOpen(false)
6465
}
6566

6667
// Authenticated → trigger opens a small status dialog with a logout

0 commit comments

Comments
 (0)