Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-tools-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Update useAuthToken() to find the auth token for any connected wallet instead of just the active one
31 changes: 19 additions & 12 deletions packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}
Loading