Skip to content

Commit e26d81c

Browse files
[SDK] Update useAuthToken to find auth token for any connected wallet (#8402)
1 parent be3316e commit e26d81c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

.changeset/clean-tools-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Update useAuthToken() to find the auth token for any connected wallet instead of just the active one

packages/thirdweb/src/react/core/hooks/wallets/useAuthToken.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { useActiveAccount } from "./useActiveAccount.js";
2-
import { useActiveWallet } from "./useActiveWallet.js";
1+
import type {
2+
EcosystemWallet,
3+
InAppWallet,
4+
} from "../../../../wallets/in-app/core/wallet/types.js";
5+
import { useConnectedWallets } from "./useConnectedWallets.js";
36

47
/**
58
* A hook that returns the authentication token (JWT) for the currently active wallet.
@@ -26,16 +29,20 @@ import { useActiveWallet } from "./useActiveWallet.js";
2629
* @wallet
2730
*/
2831
export function useAuthToken() {
29-
const activeWallet = useActiveWallet();
30-
const activeAccount = useActiveAccount();
31-
// 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
32-
if (
33-
activeWallet?.getAuthToken &&
34-
activeAccount &&
35-
activeAccount.address === activeWallet.getAccount()?.address
36-
) {
37-
return activeWallet.getAuthToken();
32+
const walletWithAuthToken = useWalletWithAuthToken();
33+
// if any connected wallet has an auth token, return it
34+
if (walletWithAuthToken) {
35+
return walletWithAuthToken.getAuthToken();
3836
}
39-
// all other wallets don't expose an auth token for now
37+
// no wallet with an auth token found
4038
return null;
4139
}
40+
41+
function useWalletWithAuthToken(): InAppWallet | EcosystemWallet | undefined {
42+
const wallets = useConnectedWallets();
43+
const wallet = wallets.find((w) => !!w.getAuthToken) as
44+
| InAppWallet
45+
| EcosystemWallet
46+
| undefined;
47+
return wallet ?? undefined;
48+
}

0 commit comments

Comments
 (0)