File tree Expand file tree Collapse file tree 9 files changed +66
-6
lines changed Expand file tree Collapse file tree 9 files changed +66
-6
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,11 @@ export type {
99
1010// wallet hooks
1111export { useActiveWallet } from "../react/core/hooks/wallets/useActiveWallet.js" ;
12- export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
1312export { useActiveWalletChain } from "../react/core/hooks/wallets/useActiveWalletChain.js" ;
1413export { useActiveWalletConnectionStatus } from "../react/core/hooks/wallets/useActiveWalletConnectionStatus.js" ;
1514export { useActiveAccount } from "../react/core/hooks/wallets/useActiveAccount.js" ;
15+ export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
16+ export { useAuthToken } from "../react/core/hooks/wallets/useAuthToken.js" ;
1617export { useAutoConnect } from "../react/native/hooks/wallets/useAutoConnect.js" ;
1718export { useConnect } from "../react/core/hooks/wallets/useConnect.js" ;
1819export { useConnectedWallets } from "../react/core/hooks/wallets/useConnectedWallets.js" ;
Original file line number Diff line number Diff line change @@ -40,10 +40,11 @@ export type { MediaRendererProps } from "../react/web/ui/MediaRenderer/types.js"
4040
4141// wallet hooks
4242export { useActiveWallet } from "../react/core/hooks/wallets/useActiveWallet.js" ;
43- export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
4443export { useActiveWalletChain } from "../react/core/hooks/wallets/useActiveWalletChain.js" ;
4544export { useActiveWalletConnectionStatus } from "../react/core/hooks/wallets/useActiveWalletConnectionStatus.js" ;
4645export { useActiveAccount } from "../react/core/hooks/wallets/useActiveAccount.js" ;
46+ export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
47+ export { useAuthToken } from "../react/core/hooks/wallets/useAuthToken.js" ;
4748export { useAutoConnect } from "../react/web/hooks/wallets/useAutoConnect.js" ;
4849export { useConnect } from "../react/core/hooks/wallets/useConnect.js" ;
4950export { useConnectedWallets } from "../react/core/hooks/wallets/useConnectedWallets.js" ;
Original file line number Diff line number Diff line change 1+ import { useActiveAccount } from "./useActiveAccount.js" ;
2+ import { useActiveWallet } from "./useActiveWallet.js" ;
3+
4+ /**
5+ * A hook that returns the authentication token (JWT) for the currently active wallet.
6+ * This token can be used to authorize API calls to your backend server.
7+ *
8+ * @returns The JWT string if the active wallet is an in-app wallet and matches the active account, null otherwise
9+ *
10+ * @example
11+ * ```tsx
12+ * function MyComponent() {
13+ * const authToken = useAuthToken();
14+ *
15+ * const fetchData = async () => {
16+ * const response = await fetch('https://api.example.com/data', {
17+ * headers: {
18+ * 'Authorization': `Bearer ${authToken}`
19+ * }
20+ * });
21+ * // ... handle response
22+ * };
23+ * }
24+ * ```
25+ *
26+ * @wallet
27+ */
28+ 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 ( ) ;
38+ }
39+ // all other wallets don't expose an auth token for now
40+ return null ;
41+ }
Original file line number Diff line number Diff line change 11import type { SocialAuthOption } from "../../../../wallets/types.js" ;
22import type { Account } from "../../../interfaces/wallet.js" ;
3+ import type { ClientScopedStorage } from "../authentication/client-scoped-storage.js" ;
34import type {
45 AuthArgsType ,
56 AuthLoginReturnType ,
@@ -38,4 +39,5 @@ export interface InAppConnector {
3839 linkProfile ( args : AuthArgsType ) : Promise < Profile [ ] > ;
3940 unlinkProfile ( args : Profile ) : Promise < Profile [ ] > ;
4041 getProfiles ( ) : Promise < Profile [ ] > ;
42+ storage : ClientScopedStorage ;
4143}
Original file line number Diff line number Diff line change @@ -55,9 +55,11 @@ export function createInAppWallet(args: {
5555 let adminAccount : Account | undefined = undefined ; // Admin account if smartAccountOptions were provided with connection
5656 let chain : Chain | undefined = undefined ;
5757 let client : ThirdwebClient | undefined ;
58+ let authToken : string | null = null ;
5859
5960 return {
6061 id : walletId ,
62+ getAuthToken : ( ) => authToken ,
6163 subscribe : emitter . subscribe ,
6264 getChain ( ) {
6365 if ( ! chain ) {
@@ -114,6 +116,7 @@ export function createInAppWallet(args: {
114116 account = connectedAccount ;
115117 adminAccount = _adminAccount ;
116118 chain = connectedChain ;
119+ authToken = await connector . storage . getAuthCookie ( ) ;
117120 trackConnect ( {
118121 client : options . client ,
119122 ecosystem,
@@ -168,6 +171,7 @@ export function createInAppWallet(args: {
168171 account = connectedAccount ;
169172 adminAccount = _adminAccount ;
170173 chain = connectedChain ;
174+ authToken = await connector . storage . getAuthCookie ( ) ;
171175 trackConnect ( {
172176 client : options . client ,
173177 ecosystem,
@@ -194,6 +198,7 @@ export function createInAppWallet(args: {
194198 account = undefined ;
195199 adminAccount = undefined ;
196200 chain = undefined ;
201+ authToken = null ;
197202 emitter . emit ( "disconnect" , undefined ) ;
198203 } ,
199204 switchChain : async ( newChain ) => {
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ type NativeConnectorOptions = {
2929export class InAppNativeConnector implements InAppConnector {
3030 private client : ThirdwebClient ;
3131 private ecosystem ?: Ecosystem ;
32- private storage : ClientScopedStorage ;
32+ public storage : ClientScopedStorage ;
3333 private passkeyDomain ?: string ;
3434 private wallet ?: IWebWallet ;
3535
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ export class InAppWebConnector implements InAppConnector {
5050 private client : ThirdwebClient ;
5151 private ecosystem ?: Ecosystem ;
5252 private querier : InAppWalletIframeCommunicator < AuthQuerierTypes > ;
53- private storage : ClientScopedStorage ;
53+ public storage : ClientScopedStorage ;
5454
5555 private wallet ?: IWebWallet ;
5656 /**
Original file line number Diff line number Diff line change @@ -151,8 +151,18 @@ export type Wallet<TWalletId extends WalletId = WalletId> = {
151151 * This is useful for smart wallets to get the underlying personal account
152152 */
153153 getAdminAccount ?: ( ) => Account | undefined ;
154- } ;
155154
155+ /**
156+ * Get the authentication token for the wallet.
157+ *
158+ * This method is not available for on all wallets. This method will be `undefined` if the wallet does not support it.
159+ * @example
160+ * ```ts
161+ * const authToken = await wallet.getAuthToken();
162+ * ```
163+ */
164+ getAuthToken ?: ( ) => string | null ;
165+ } ;
156166/**
157167 * Account interface
158168 *
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ import type {
4141 */
4242export function createWallet < const ID extends WalletId > (
4343 ...args : CreateWalletArgs < ID >
44- ) : Wallet < ID > {
44+ ) {
4545 const [ id , creationOptions ] = args ;
4646
4747 switch ( true ) {
You can’t perform that action at this time.
0 commit comments