Skip to content

Commit bed6e2f

Browse files
committed
Add useAuthToken hook for in-app wallet authentication
1 parent 88533d9 commit bed6e2f

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 &&
34+
activeWallet.id === "inApp" &&
35+
activeAccount &&
36+
activeAccount.address === activeWallet.getAccount()?.address
37+
) {
38+
return activeWallet.getAuthToken();
39+
}
40+
// all other wallets don't expose an auth token for now
41+
return null;
42+
}

packages/thirdweb/src/wallets/in-app/core/interfaces/connector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SocialAuthOption } from "../../../../wallets/types.js";
22
import type { Account } from "../../../interfaces/wallet.js";
3+
import type { ClientScopedStorage } from "../authentication/client-scoped-storage.js";
34
import 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
}

packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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) => {

packages/thirdweb/src/wallets/in-app/native/native-connector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type NativeConnectorOptions = {
2929
export 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

packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
/**

packages/thirdweb/src/wallets/interfaces/wallet.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ 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+
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: TWalletId extends "inApp" ? () => string | null : never;
154165
};
155166

156167
/**

0 commit comments

Comments
 (0)