File tree 9 files changed +66
-6
lines changed
9 files changed +66
-6
lines changed Original file line number Diff line number Diff line change @@ -9,10 +9,11 @@ export type {
9
9
10
10
// wallet hooks
11
11
export { useActiveWallet } from "../react/core/hooks/wallets/useActiveWallet.js" ;
12
- export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
13
12
export { useActiveWalletChain } from "../react/core/hooks/wallets/useActiveWalletChain.js" ;
14
13
export { useActiveWalletConnectionStatus } from "../react/core/hooks/wallets/useActiveWalletConnectionStatus.js" ;
15
14
export { 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" ;
16
17
export { useAutoConnect } from "../react/native/hooks/wallets/useAutoConnect.js" ;
17
18
export { useConnect } from "../react/core/hooks/wallets/useConnect.js" ;
18
19
export { 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"
40
40
41
41
// wallet hooks
42
42
export { useActiveWallet } from "../react/core/hooks/wallets/useActiveWallet.js" ;
43
- export { useAdminWallet } from "../react/core/hooks/wallets/useAdminWallet.js" ;
44
43
export { useActiveWalletChain } from "../react/core/hooks/wallets/useActiveWalletChain.js" ;
45
44
export { useActiveWalletConnectionStatus } from "../react/core/hooks/wallets/useActiveWalletConnectionStatus.js" ;
46
45
export { 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" ;
47
48
export { useAutoConnect } from "../react/web/hooks/wallets/useAutoConnect.js" ;
48
49
export { useConnect } from "../react/core/hooks/wallets/useConnect.js" ;
49
50
export { 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 1
1
import type { SocialAuthOption } from "../../../../wallets/types.js" ;
2
2
import type { Account } from "../../../interfaces/wallet.js" ;
3
+ import type { ClientScopedStorage } from "../authentication/client-scoped-storage.js" ;
3
4
import type {
4
5
AuthArgsType ,
5
6
AuthLoginReturnType ,
@@ -38,4 +39,5 @@ export interface InAppConnector {
38
39
linkProfile ( args : AuthArgsType ) : Promise < Profile [ ] > ;
39
40
unlinkProfile ( args : Profile ) : Promise < Profile [ ] > ;
40
41
getProfiles ( ) : Promise < Profile [ ] > ;
42
+ storage : ClientScopedStorage ;
41
43
}
Original file line number Diff line number Diff line change @@ -55,9 +55,11 @@ export function createInAppWallet(args: {
55
55
let adminAccount : Account | undefined = undefined ; // Admin account if smartAccountOptions were provided with connection
56
56
let chain : Chain | undefined = undefined ;
57
57
let client : ThirdwebClient | undefined ;
58
+ let authToken : string | null = null ;
58
59
59
60
return {
60
61
id : walletId ,
62
+ getAuthToken : ( ) => authToken ,
61
63
subscribe : emitter . subscribe ,
62
64
getChain ( ) {
63
65
if ( ! chain ) {
@@ -114,6 +116,7 @@ export function createInAppWallet(args: {
114
116
account = connectedAccount ;
115
117
adminAccount = _adminAccount ;
116
118
chain = connectedChain ;
119
+ authToken = await connector . storage . getAuthCookie ( ) ;
117
120
trackConnect ( {
118
121
client : options . client ,
119
122
ecosystem,
@@ -168,6 +171,7 @@ export function createInAppWallet(args: {
168
171
account = connectedAccount ;
169
172
adminAccount = _adminAccount ;
170
173
chain = connectedChain ;
174
+ authToken = await connector . storage . getAuthCookie ( ) ;
171
175
trackConnect ( {
172
176
client : options . client ,
173
177
ecosystem,
@@ -194,6 +198,7 @@ export function createInAppWallet(args: {
194
198
account = undefined ;
195
199
adminAccount = undefined ;
196
200
chain = undefined ;
201
+ authToken = null ;
197
202
emitter . emit ( "disconnect" , undefined ) ;
198
203
} ,
199
204
switchChain : async ( newChain ) => {
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ type NativeConnectorOptions = {
29
29
export class InAppNativeConnector implements InAppConnector {
30
30
private client : ThirdwebClient ;
31
31
private ecosystem ?: Ecosystem ;
32
- private storage : ClientScopedStorage ;
32
+ public storage : ClientScopedStorage ;
33
33
private passkeyDomain ?: string ;
34
34
private wallet ?: IWebWallet ;
35
35
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ export class InAppWebConnector implements InAppConnector {
50
50
private client : ThirdwebClient ;
51
51
private ecosystem ?: Ecosystem ;
52
52
private querier : InAppWalletIframeCommunicator < AuthQuerierTypes > ;
53
- private storage : ClientScopedStorage ;
53
+ public storage : ClientScopedStorage ;
54
54
55
55
private wallet ?: IWebWallet ;
56
56
/**
Original file line number Diff line number Diff line change @@ -151,8 +151,18 @@ export type Wallet<TWalletId extends WalletId = WalletId> = {
151
151
* This is useful for smart wallets to get the underlying personal account
152
152
*/
153
153
getAdminAccount ?: ( ) => Account | undefined ;
154
- } ;
155
154
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
+ } ;
156
166
/**
157
167
* Account interface
158
168
*
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ import type {
41
41
*/
42
42
export function createWallet < const ID extends WalletId > (
43
43
...args : CreateWalletArgs < ID >
44
- ) : Wallet < ID > {
44
+ ) {
45
45
const [ id , creationOptions ] = args ;
46
46
47
47
switch ( true ) {
You can’t perform that action at this time.
0 commit comments