From 6a262510bcfd0576946c503611a4ad70023f6290 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Thu, 13 Nov 2025 07:55:41 +1300 Subject: [PATCH] [SDK] Update useAuthToken to find auth token for any connected wallet --- .changeset/clean-tools-learn.md | 5 +++ .../react/core/hooks/wallets/useAuthToken.ts | 31 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 .changeset/clean-tools-learn.md diff --git a/.changeset/clean-tools-learn.md b/.changeset/clean-tools-learn.md new file mode 100644 index 00000000000..7c55f901a72 --- /dev/null +++ b/.changeset/clean-tools-learn.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Update useAuthToken() to find the auth token for any connected wallet instead of just the active one diff --git a/packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts b/packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts index 65ce148762c..501472d386d 100644 --- a/packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts +++ b/packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts @@ -1,5 +1,8 @@ -import { useActiveAccount } from "./useActiveAccount.js"; -import { useActiveWallet } from "./useActiveWallet.js"; +import type { + EcosystemWallet, + InAppWallet, +} from "../../../../wallets/in-app/core/wallet/types.js"; +import { useConnectedWallets } from "./useConnectedWallets.js"; /** * A hook that returns the authentication token (JWT) for the currently active wallet. @@ -26,16 +29,20 @@ import { useActiveWallet } from "./useActiveWallet.js"; * @wallet */ export function useAuthToken() { - const activeWallet = useActiveWallet(); - const activeAccount = useActiveAccount(); - // if the active wallet is an in-app wallet and the active account is the same as the active wallet's account, return the auth token for the in-app wallet - if ( - activeWallet?.getAuthToken && - activeAccount && - activeAccount.address === activeWallet.getAccount()?.address - ) { - return activeWallet.getAuthToken(); + const walletWithAuthToken = useWalletWithAuthToken(); + // if any connected wallet has an auth token, return it + if (walletWithAuthToken) { + return walletWithAuthToken.getAuthToken(); } - // all other wallets don't expose an auth token for now + // no wallet with an auth token found return null; } + +function useWalletWithAuthToken(): InAppWallet | EcosystemWallet | undefined { + const wallets = useConnectedWallets(); + const wallet = wallets.find((w) => !!w.getAuthToken) as + | InAppWallet + | EcosystemWallet + | undefined; + return wallet ?? undefined; +}