@privy-io/react-auth 1.3.1-beta.3 → 1.4.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +108 -323
  2. package/dist/index.js +61 -61
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -1,242 +1,155 @@
1
- import { AxiosResponse, AxiosRequestConfig } from 'axios';
2
1
  import React from 'react';
3
2
  import { Web3Provider } from '@ethersproject/providers';
4
3
  import { AbstractProvider } from 'web3-core';
5
4
 
6
5
  declare const SUPPORTED_WALLET_TYPES: readonly ["metamask", "coinbase_wallet", "wallet_connect"];
7
6
  declare type WalletType = typeof SUPPORTED_WALLET_TYPES[number];
7
+ /** @ignore */
8
8
  interface LinkMetadata {
9
- /** Link type, most commonly useful when filtering through linkedAccounts */
9
+ /** Account type, most commonly useful when filtering through linkedAccounts */
10
10
  type: 'wallet' | 'email' | 'google_oauth' | 'twitter_oauth' | 'discord_oauth';
11
- /** Datetime when this email or wallet was associated to the user account. */
11
+ /** Datetime when this account was linked to the user. */
12
12
  verifiedAt: Date;
13
13
  }
14
+ /**
15
+ * Object representation of a user's wallet.
16
+ */
14
17
  interface Wallet {
15
- /** Address of the wallet */
18
+ /** The wallet address. */
16
19
  address: string;
17
- /** Chain type of the wallet address */
20
+ /** Chain type of the wallet address. */
18
21
  chainType: 'ethereum' | 'solana';
19
- /** Not yet implemented.
22
+ /**
23
+ * @experimental
24
+ * DO NOT USE: This feature is under active development.
20
25
  *
21
26
  * The chain ID of the active connected wallet, if used to verify in the current session.
22
27
  * Only present for the active wallet.
23
28
  *
24
29
  * e.g. testnet, mainnet, goerli */
25
30
  chainId?: string;
26
- /** Not yet implemented.
31
+ /**
32
+ * @experimental
33
+ * DO NOT USE: This feature is under active development.
34
+ *
27
35
  * The wallet type of the active connected wallet, if used to verify in the current session.
28
36
  * Only present for the active wallet.
29
37
  */
30
38
  walletType?: WalletType;
31
39
  }
40
+ /** Object representation of a user's email. */
32
41
  interface Email {
33
- /** Email address */
42
+ /** The email address. */
34
43
  address: string;
35
44
  }
45
+ /** Object representation of a user's Google account. */
36
46
  interface Google {
47
+ /** The `sub` claim from the Google-issued JWT for this account. */
37
48
  subject: string;
49
+ /** The email associated with the Google account. */
38
50
  email: string;
51
+ /** The name associated with the Google account. */
39
52
  name: string | null;
40
53
  }
54
+ /** Object representation of a user's Twitter account. */
41
55
  interface Twitter {
56
+ /** The `sub` claim from the Twitter-issued JWT for this account. */
42
57
  subject: string;
58
+ /** The username associated with the Twitter account. */
43
59
  username: string | null;
60
+ /** The name associated with the Twitter account. */
44
61
  name: string | null;
45
62
  }
63
+ /** Object representation of a user's Discord account. */
46
64
  interface Discord {
65
+ /** The `sub` claim from the Discord-issued JWT for this account. */
47
66
  subject: string;
67
+ /** The username associated with the Discord account. */
48
68
  username: string | null;
69
+ /** The email associated with the Discord account. */
49
70
  email: string | null;
50
71
  }
72
+ /** Object representation of a user's email, with additional metadata for advanced use cases. */
51
73
  interface EmailWithMetadata extends LinkMetadata, Email {
74
+ /** Denotes that this is an email account. */
52
75
  type: 'email';
53
76
  }
77
+ /** Object representation of a user's wallet, with additional metadata for advanced use cases. */
54
78
  interface WalletWithMetadata extends LinkMetadata, Wallet {
79
+ /** Denotes that this is a wallet account. */
55
80
  type: 'wallet';
56
81
  }
82
+ /** Object representation of a user's Google Account, with additional metadata for advanced use cases. */
57
83
  interface GoogleOAuthWithMetadata extends LinkMetadata, Google {
84
+ /** Denotes that this is a Google account. */
58
85
  type: 'google_oauth';
59
86
  }
87
+ /** Object representation of a user's Twitter Account, with additional metadata for advanced use cases. */
60
88
  interface TwitterOAuthWithMetadata extends LinkMetadata, Twitter {
89
+ /** Denotes that this is a Twitter account. */
61
90
  type: 'twitter_oauth';
62
91
  }
92
+ /** Object representation of a user's Discord Account, with additional metadata for advanced use cases. */
63
93
  interface DiscordOAuthWithMetadata extends LinkMetadata, Discord {
94
+ /** Denotes that this is a Discord account. */
64
95
  type: 'discord_oauth';
65
96
  }
66
97
  interface User {
67
- /** The Privy DID for the user. If you need to store or link additional information
68
- * about a user, you can use this DID to reference the user identity. */
98
+ /** The Privy-issued DID for the user. If you need to store additional information
99
+ * about a user, you can use this DID to reference them. */
69
100
  id: string;
70
- /** When the user was created */
101
+ /** The datetime of when the user was created. */
71
102
  createdAt: Date;
72
- /** The email associated with this user. It cannot be associated with another account. */
103
+ /** The user's email address, if they have linked one. It cannot be linked to another user. */
73
104
  email?: Email;
74
- /** The wallet associated with this user. It cannot be associated with another account. */
105
+ /** The user's wallet address, if they have linked one. It cannot be linked to another user. */
75
106
  wallet?: Wallet;
76
- /** The google oauth account associated with this user */
107
+ /** The user's Google account, if they have linked one. It cannot be linked to another user. */
77
108
  google?: Google;
78
- /** The twitter oauth account associated with this user */
109
+ /** The user's Twitter account, if they have linked one. It cannot be linked to another user. */
79
110
  twitter?: Twitter;
80
- /** The discord oauth account associated with this user */
111
+ /** The user's Discord account, if they have linked one. It cannot be linked to another user. */
81
112
  discord?: Discord;
82
- /** The list of accounts associated with this user. Accounts contain additional metadata
83
- * about the account and its linking that may be necessary for advanced use cases. */
113
+ /** The list of accounts associated with this user. Each account contains additional metadata
114
+ * that may be helpful for advanced use cases. */
84
115
  linkedAccounts: Array<WalletWithMetadata | EmailWithMetadata | GoogleOAuthWithMetadata | TwitterOAuthWithMetadata | DiscordOAuthWithMetadata>;
85
116
  }
86
- interface AppSettings {
87
- id?: string;
88
- name?: string;
89
- verificationKey?: string;
90
- logoUrl?: string;
91
- theme?: 'System' | 'Light' | 'Dark' | string;
92
- accentColor?: string;
93
- createdAt?: Date;
94
- updatedAt?: Date;
95
- }
96
-
97
- interface DefaultsType {
98
- baseURL: string;
99
- timeout: number;
100
- }
101
- /**
102
- * A raw http handler for making requests to the Privy API. It requires a Session
103
- * object, which is used for fetching and including any tokens that are required
104
- * for requests.
105
- */
106
- declare class Http {
107
- private appId;
108
- private session;
109
- private defaults;
110
- constructor(appId: string, session: Session, defaults: DefaultsType);
111
- get<T = any, R = AxiosResponse<T>, D = any>(path: string, config?: AxiosRequestConfig<D>): Promise<R>;
112
- post<T = any, R = AxiosResponse<T>, D = any>(path: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
113
- delete<T = any, R = AxiosResponse<T>, D = any>(path: string, config?: AxiosRequestConfig<D>): Promise<R>;
114
- private buildConfig;
115
- }
116
-
117
- interface ResponseEmailAccount {
118
- type: 'email';
119
- address: string;
120
- verified_at: number;
121
- }
122
- interface ResponseEthereumAccount {
123
- type: 'wallet';
124
- address: string;
125
- chain_type: 'ethereum';
126
- verified_at: number;
127
- }
128
- interface ResponseOAuthGoogle {
129
- type: 'google_oauth';
130
- subject: string;
131
- email: string;
132
- name: string | null;
133
- verified_at: number;
134
- }
135
- interface ResponseOAuthTwitter {
136
- type: 'twitter_oauth';
137
- subject: string;
138
- username: string | null;
139
- name: string | null;
140
- verified_at: number;
141
- }
142
- interface ResponseOAuthDiscord {
143
- type: 'discord_oauth';
144
- subject: string;
145
- username: string | null;
146
- email: string | null;
147
- verified_at: number;
148
- }
149
- declare type LinkedAccountsType = Array<ResponseEmailAccount | ResponseEthereumAccount | ResponseOAuthGoogle | ResponseOAuthTwitter | ResponseOAuthDiscord>;
150
- interface GetCurrentUserResponse {
151
- id: string;
152
- created_at: number;
153
- linked_accounts: LinkedAccountsType;
154
- }
155
117
 
156
118
  /**
157
- * Valid /session and <>/authenticate calls will respond with a token
158
- * as well as a valid user object for streamlining.
119
+ * Properties to initialize the {@link PrivyProvider}.
159
120
  */
160
- interface ValidSessionResponse {
161
- user: GetCurrentUserResponse;
162
- token: string;
163
- refresh_token: string | null;
164
- }
165
- /**
166
- * `Session` represents the session lifecycle of the login. It manages token
167
- * storage, refresh, and deletion.
168
- */
169
- declare class Session {
170
- private destroyOnce;
171
- private refreshOnce;
172
- private authenticateOnce;
173
- api?: Http;
174
- constructor();
175
- get token(): string | null;
176
- get refreshToken(): string | null;
121
+ interface PrivyProviderProps {
122
+ /** Your Privy App ID, which can be retrieved from the Privy console. */
123
+ appId: string;
177
124
  /**
178
- * Checks to see if locally we have refresh credentials. Refresh
179
- * credentials consist of:
180
- *
181
- * 1. Access token
182
- * 2. Refresh token
183
- *
184
- * @returns true if we have what appear to be valid credentials, false otherwise.
125
+ * @experimental DO NOT USE: This feature is under active development.
185
126
  */
186
- hasRefreshCredentials(): boolean;
127
+ showSocials?: boolean;
187
128
  /**
188
- * Checks if the session contains a valid token that is not
189
- * expired or expiring soon.
129
+ * An optional callback that will execute once a `login` call successfully completes.
190
130
  *
191
- * @returns true if token is valid and considered active, false otherwise.
131
+ * You may access the `user` object corresponding to the authenticated user within this callback.
192
132
  */
193
- hasActiveToken(): boolean;
194
- /**
195
- * Authenticate the session.
196
- * @returns Promise<User>
197
- */
198
- authenticate(authFlow: AuthFlow): Promise<User | null>;
199
- /**
200
- * Renews the auth token using the refresh token.
201
- * @return Promise<User> if successful with a logged-in user, Promise<null> otherwise.
202
- */
203
- refresh(): Promise<User | null>;
204
- /**
205
- * Destroy the session.
206
- */
207
- destroy(): Promise<void>;
208
- private _authenticate;
209
- private _refresh;
210
- private _destroy;
211
- private destroyLocalState;
212
- private storeToken;
213
- private storeRefreshToken;
133
+ onSuccess?: (user: User) => void;
134
+ /** @ignore */
135
+ children: React.ReactNode;
214
136
  }
215
-
216
- declare type AuthMeta = {
217
- [key: string]: any;
218
- };
219
137
  /**
220
- * An auth flow is an encapsulation of the business logic required for a given
221
- * authentication process. It requires at least one definitive `authenticate`
222
- * method that does the final token handshaking with the API, but may also
223
- * include any number of methods/API calls necessary to set up the state (e.g.
224
- * sending an email before being able to do a passwordless code login)
138
+ * Passes the Privy authentication context to your React components.
139
+ *
140
+ * This should wrap any components that will to use the Privy SDK via the {@link usePrivy} hook. As an example:
141
+ *
142
+ * ```typescript
143
+ * // At your application root (e.g. `_app.tsx` in NextJS):
144
+ * import {PrivyProvider} from '@privy-io/react-auth';
145
+ *
146
+ * <PrivyProvider appId="APP_ID_FROM_CONSOLE" onSuccess={() => console.log('Success!')}>
147
+ * <Component {...pageProps} />
148
+ * </PrivyProvider>
149
+ * ```
150
+ *
225
151
  */
226
- interface AuthFlow {
227
- api?: Http;
228
- /**
229
- * Any meta information necessary for the auth flow, that may also need to be
230
- * shared out to things like frontend components for displaying state of the
231
- * auth flow
232
- */
233
- meta: AuthMeta;
234
- /**
235
- * Should handle the actual authentication step. An preconditions must be
236
- * addressed prior to calling
237
- */
238
- authenticate(): Promise<ValidSessionResponse>;
239
- }
152
+ declare const PrivyProvider: (props: PrivyProviderProps) => JSX.Element;
240
153
 
241
154
  declare global {
242
155
  interface Window {
@@ -252,127 +165,22 @@ interface EIP1193Provider {
252
165
  removeListener: (eventName: string | symbol, listener: (...args: any[]) => void) => any;
253
166
  }
254
167
 
255
- declare class PrivyConnector {
256
- private ethProvider;
257
- walletType: WalletType | null;
258
- constructor();
259
- getEthereumProvider: () => EIP1193Provider;
260
- initialize(): void;
261
- destroy(): void;
262
- /**
263
- * Connects to the wallet.
264
- *
265
- * @param showPrompt if true, prompts the user for a connection when no connection can be automatically re-established.
266
- * @param walletType? the wallet type to connect to. If not provided, defaults to the walletType that the connector was initialized with.
267
- * @returns {string | null} EIP-55 mixed-case checksum-encoded address of connected wallet or null if user does not connect.
268
- */
269
- connect(options: {
270
- showPrompt: boolean;
271
- }): Promise<string | null>;
272
- private isConnected;
273
- private promptConnection;
274
- /**
275
- * The currently connected EIP-155 chain id. E.g., `1` for Ethereum mainnet.
276
- *
277
- * @returns {string} The EIP-155 chain id.
278
- */
279
- chainId(): Promise<string>;
280
- /**
281
- * The currently connected address.
282
- *
283
- * @returns {string | null} EIP-55 mixed-case checksum-encoded address or null if not connected.
284
- */
285
- address(): Promise<string | null>;
286
- }
287
-
288
- declare class PrivyClient {
289
- private api;
290
- private appId;
291
- private session;
292
- authFlow?: AuthFlow;
293
- connector: PrivyConnector;
294
- /**
295
- * Creates a new Privy client.
296
- * @param options Initialization options.
297
- */
298
- constructor(options: {
299
- /**
300
- * The URL of the Privy API. Defaults to `https://api.privy.io/v0`.
301
- */
302
- apiURL?: string;
303
- /**
304
- * The app id from your console
305
- */
306
- appId: string;
307
- /**
308
- * Time in milliseconds after which to timeout requests to the API and KMS. Defaults to `10000` (10 seconds).
309
- */
310
- timeout?: number;
311
- });
312
- authenticate(): Promise<User | null>;
313
- logout(): Promise<void>;
314
- startAuthFlow(authFlow: AuthFlow): void;
315
- /** DATA METHODS */
316
- /**
317
- * Fetches the currently authenticed user from the API or
318
- * returns null if the user is not authenticated.
319
- *
320
- * This will refresh the user's access token and rotate
321
- * the refresh token if needed.
322
- *
323
- * @returns Promise<User | null>
324
- */
325
- getAuthenticatedUser(): Promise<User | null>;
326
- /**
327
- * Grab the Privy access token for the currently logged in user.
328
- *
329
- * If the token is expired or expiring soon, this will attempt to
330
- * first refresh the access token to ensure that the token is active.
331
- *
332
- * @returns Promise<string | null>
333
- */
334
- getAccessToken(): Promise<string | null>;
335
- getAppSettings(): Promise<AppSettings>;
336
- }
337
-
338
- interface PrivyProviderProps {
339
- /** Your app id, taken from the Privy console. */
340
- appId: string;
341
- /**
342
- * onSuccess is run when an auth (`login`) or linking (`linkEmail` or `linkWallet`)
343
- * action is completed successfully.
344
- */
345
- showSocials?: boolean;
346
- onSuccess?: (user: User) => void;
347
- children: React.ReactNode;
348
- }
349
168
  /**
350
- * The Privy React context provider.
351
- *
352
- * ```typescript
353
- * // At your application root:
354
- * import {PrivyProvider} from '@privy-io/react-auth';
355
- *
356
- * <PrivyProvider appId="APP_ID_FROM_CONSOLE" onSuccess={() => console.log('Success!')}>
357
- * <Component {...pageProps} />
358
- * </PrivyProvider>
359
- * ```
169
+ * Allows you to manage the user's current authentication state and access their linked accounts.
170
+ * You can access the fields and methods documented here via the {@link usePrivy} hook.
360
171
  */
361
- declare const PrivyProvider: (props: PrivyProviderProps) => JSX.Element;
362
-
363
172
  interface PrivyInterface {
364
173
  /**
365
- * Check whether the PrivyProvider is ready to be used. We recommend you wait for this to
366
- * be true before checking values such as authenticated and user.
174
+ * Check whether the `PrivyProvider` is ready to be used. You should wait for this to
175
+ * be true before using values such as `authenticated` and `user`.
367
176
  */
368
177
  ready: boolean;
369
178
  /**
370
- * True if the user is authenticated, false if they are not. In the event that the
371
- * client is offline, this will be set to true if the user has a valid token,
372
- * but we are unable to fetch the user object.
179
+ * True if the user is authenticated, false otherwise.
180
+ *
181
+ * You should always check that `ready` is true before using this value. Otherwise,
182
+ * the value may outdated while the Privy client fetches fresh tokens.
373
183
  *
374
- * Note: this value should be checked in conjunction with `ready` - otherwise
375
- * the value may incorrectly show authentication state while proper setup is completed.
376
184
  */
377
185
  authenticated: boolean;
378
186
  /**
@@ -380,76 +188,53 @@ interface PrivyInterface {
380
188
  */
381
189
  user: User | null;
382
190
  /**
383
- * Opens the Privy login modal and starts a login flow
191
+ * Opens the Privy login modal and prompts the user to login.
384
192
  */
385
193
  login: () => void;
386
194
  /**
387
- * Opens the Privy modal and starts a flow to link an email to an already existing account.
195
+ * For users who are authenticated, opens the Privy modal and prompts the user to link an email.
388
196
  */
389
197
  linkEmail: () => void;
390
198
  /**
391
- * Opens the Privy modal and starts a flow to link a wallet to an already existing account.
199
+ * For users who are authenticated, opens the Privy modal and prompts the user to link a wallet.
392
200
  */
393
201
  linkWallet: () => void;
394
202
  /**
395
- * Log the current user out. This clears the user state. You can await this call to take an
396
- * action once the log out is complete - for example, redirecting to a specific page.
203
+ * Log the current user out and clears their authentication state. `authenticated` will become false, `user` will become null, and the Privy Auth tokens will be deleted from the browser's local storage.
204
+ *
205
+ * You may await this call to take an action once logout is complete (e.g. redirecting to a specific page).
397
206
  */
398
207
  logout: () => Promise<void>;
399
208
  /**
400
- * Get the Privy access token. This will automatically attempt to refresh the
401
- * session if the token is expired or about to expire. You can use this to validate
402
- * the user is properly authenticated on your backend with the verification key issued
403
- * to your Privy app.
209
+ * Get the Privy access token ([JWT](https://jwt.io/)) for an authenticated user. Returns null for an unauthenticated user.
210
+ *
211
+ * You may use this token to authorize requests sent from your frontend, and can validate it in your backend against your app's Privy verification key.
212
+ *
213
+ * This will automatically attempt to refresh the session if the token is expired or about to expire.
404
214
  */
405
215
  getAccessToken: () => Promise<string | null>;
406
- getEthereumProvider: () => EIP1193Provider;
407
- getEthersProvider: () => Web3Provider;
408
- getWeb3jsProvider: () => AbstractProvider;
409
- }
410
- declare const usePrivy: () => PrivyInterface;
411
-
412
- declare abstract class PrivyError extends Error {
413
216
  /**
414
- * Privy error type.
415
- */
416
- abstract type: string;
417
- /**
418
- * Original Error object, it the error originated client-side.
217
+ * Get an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193)-compatible provider from the user's wallet, if the user has connected one.
218
+ *
219
+ * You may then use the Ethereum Javascript API syntax to send JSON-RPC requests to the user's wallet.
419
220
  */
420
- cause?: Error;
221
+ getEthereumProvider: () => EIP1193Provider;
421
222
  /**
422
- * @param type Privy error type.
423
- * @param message Human-readable message.
424
- * @param cause Source of this error.
223
+ *
224
+ * Get an [ethers.js](https://docs.ethers.io/v5/)-compatible provider from the user's wallet, if the user has connected one.
225
+ *
425
226
  */
426
- protected constructor(message: string, cause?: unknown);
427
- toString(): string;
428
- }
429
- /**
430
- * Issues with the Privy api.
431
- */
432
- declare class PrivyApiError extends PrivyError {
433
- type: string;
227
+ getEthersProvider: () => Web3Provider;
434
228
  /**
435
- * HTTP status code, or `null` if no response was received.
229
+ * Get a [web3.js](https://web3js.readthedocs.io/en/v1.8.0/)-compatible provider from the user's wallet, if the user has connected one.
436
230
  */
437
- status: number | null;
438
- constructor(type: string, status: number | null, message: string, cause?: unknown);
439
- }
440
- /**
441
- * Issues with the Privy client.
442
- */
443
- declare class PrivyClientError extends PrivyError {
444
- type: string;
445
- constructor(message: string, cause?: unknown);
231
+ getWeb3jsProvider: () => AbstractProvider;
446
232
  }
447
233
  /**
448
- * The Session instance threw an exception.
234
+ * [React Hook](https://reactjs.org/docs/hooks-intro.html) that allows you to manage the user's current authentication state and access their linked accounts.
235
+ *
236
+ * You should use this hook to access the Privy SDK from within your React components and custom hooks.
449
237
  */
450
- declare class PrivySessionError extends PrivyError {
451
- type: string;
452
- constructor(message: string, cause?: unknown);
453
- }
238
+ declare const usePrivy: () => PrivyInterface;
454
239
 
455
- export { Discord, DiscordOAuthWithMetadata, Email, EmailWithMetadata, Google, GoogleOAuthWithMetadata, PrivyApiError, PrivyClient, PrivyClientError, PrivyError, PrivyProvider, PrivySessionError, Twitter, TwitterOAuthWithMetadata, User, Wallet, WalletWithMetadata, usePrivy };
240
+ export { Discord, DiscordOAuthWithMetadata, Email, EmailWithMetadata, Google, GoogleOAuthWithMetadata, PrivyInterface, PrivyProvider, PrivyProviderProps, Twitter, TwitterOAuthWithMetadata, User, Wallet, WalletWithMetadata, usePrivy };