@privy-io/react-auth 1.12.0-beta.1 → 1.12.0-beta.2

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.
package/dist/index.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import React from 'react';
2
- import { Web3Provider } from '@ethersproject/providers';
2
+ import { ExternalProvider, Web3Provider } from '@ethersproject/providers';
3
3
  import { AbstractProvider } from 'web3-core';
4
+ import { AxiosResponse, AxiosRequestConfig } from 'axios';
4
5
 
6
+ declare const SUPPORTED_OAUTH_PROVIDERS: readonly ["google", "discord", "twitter"];
7
+ type OAuthProviderType = typeof SUPPORTED_OAUTH_PROVIDERS[number];
5
8
  declare const SUPPORTED_WALLET_TYPES: readonly ["metamask", "coinbase_wallet", "wallet_connect"];
6
- declare type WalletType = typeof SUPPORTED_WALLET_TYPES[number];
7
- declare type LinkedAccountType = 'wallet' | 'email' | 'phone' | 'google_oauth' | 'twitter_oauth' | 'discord_oauth';
9
+ type WalletType = typeof SUPPORTED_WALLET_TYPES[number];
10
+ type LinkedAccountType = 'wallet' | 'email' | 'phone' | 'google_oauth' | 'twitter_oauth' | 'discord_oauth';
8
11
  /** @ignore */
9
12
  interface LinkMetadata {
10
13
  /** Account type, most commonly useful when filtering through linkedAccounts */
@@ -105,7 +108,7 @@ interface DiscordOAuthWithMetadata extends LinkMetadata, Discord {
105
108
  /** Denotes that this is a Discord account. */
106
109
  type: 'discord_oauth';
107
110
  }
108
- declare type LinkedAccountWithMetadata = WalletWithMetadata | EmailWithMetadata | PhoneWithMetadata | GoogleOAuthWithMetadata | TwitterOAuthWithMetadata | DiscordOAuthWithMetadata;
111
+ type LinkedAccountWithMetadata = WalletWithMetadata | EmailWithMetadata | PhoneWithMetadata | GoogleOAuthWithMetadata | TwitterOAuthWithMetadata | DiscordOAuthWithMetadata;
109
112
  interface User {
110
113
  /** The Privy-issued DID for the user. If you need to store additional information
111
114
  * about a user, you can use this DID to reference them. */
@@ -132,6 +135,24 @@ interface User {
132
135
  * that may be helpful for advanced use cases. */
133
136
  linkedAccounts: Array<LinkedAccountWithMetadata>;
134
137
  }
138
+ interface AppSettings {
139
+ id?: string;
140
+ name?: string;
141
+ verificationKey?: string;
142
+ logoUrl?: string;
143
+ theme?: 'System' | 'Light' | 'Dark' | string;
144
+ accentColor?: string;
145
+ walletAuth?: boolean;
146
+ emailAuth?: boolean;
147
+ smsAuth?: boolean;
148
+ googleOAuth?: boolean;
149
+ twitterOAuth?: boolean;
150
+ discordOAuth?: boolean;
151
+ termsAndConditionsUrl: string | null;
152
+ privacyPolicyUrl: string | null;
153
+ createdAt?: Date;
154
+ updatedAt?: Date;
155
+ }
135
156
 
136
157
  declare function getAccessToken(): Promise<string | null>;
137
158
  /**
@@ -192,6 +213,66 @@ interface EIP1193Provider {
192
213
  on: (eventName: string, listener: (...args: unknown[]) => void) => any;
193
214
  removeListener: (eventName: string | symbol, listener: (...args: any[]) => void) => any;
194
215
  }
216
+ declare class PrivyProxyProvider implements EIP1193Provider {
217
+ walletProvider?: EIP1193Provider;
218
+ private _subscriptions;
219
+ constructor(provider?: EIP1193Provider);
220
+ on(eventName: string, listener: (...args: any[]) => void): any;
221
+ request(request: {
222
+ method: string;
223
+ params?: any[] | undefined;
224
+ }): Promise<any>;
225
+ removeListener: (eventName: string | symbol, listener: (...args: any[]) => void) => any;
226
+ setProvider: (provider: EIP1193Provider) => void;
227
+ }
228
+ declare class AsExternalProvider extends PrivyProxyProvider implements ExternalProvider {
229
+ constructor(provider: EIP1193Provider);
230
+ isMetaMask?: boolean;
231
+ isStatus?: boolean;
232
+ host?: string;
233
+ path?: string;
234
+ sendAsync?: (request: {
235
+ method: string;
236
+ params?: Array<any>;
237
+ }, callback: (error: any, response: any) => void) => void;
238
+ send?: (request: {
239
+ method: string;
240
+ params?: Array<any>;
241
+ }, callback: (error: any, response: any) => void) => void;
242
+ }
243
+
244
+ declare class PrivyConnector {
245
+ private ethProvider;
246
+ walletType: WalletType | null;
247
+ constructor();
248
+ getEthereumProvider: () => EIP1193Provider;
249
+ initialize(): void;
250
+ destroy(): void;
251
+ getConnectedWallet(): Promise<Wallet | null>;
252
+ /**
253
+ * Connects to the wallet.
254
+ *
255
+ * @param options.showPrompt if true, prompts the user for a connection when no connection can be automatically re-established.
256
+ * @returns {string | null} EIP-55 mixed-case checksum-encoded address of connected wallet or null if user does not connect.
257
+ */
258
+ connect(options: {
259
+ showPrompt: boolean;
260
+ }): Promise<Wallet | null>;
261
+ private isConnected;
262
+ private promptConnection;
263
+ /**
264
+ * The currently connected EIP-155 chain id. E.g., `1` for Ethereum mainnet.
265
+ *
266
+ * @returns {string} The EIP-155 chain id.
267
+ */
268
+ chainId(): Promise<string>;
269
+ /**
270
+ * The currently connected address.
271
+ *
272
+ * @returns {string | null} EIP-55 mixed-case checksum-encoded address or null if not connected.
273
+ */
274
+ address(): Promise<string | null>;
275
+ }
195
276
 
196
277
  /**
197
278
  * Allows you to manage the user's current authentication state and access their linked accounts.
@@ -279,6 +360,11 @@ interface PrivyInterface {
279
360
  * 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.
280
361
  */
281
362
  getWeb3jsProvider: () => AbstractProvider;
363
+ /**
364
+ * Get the PrivyConnector object
365
+ * This shouldn't need to be used directly unless creating a plugin, like a WAGMI plugin
366
+ */
367
+ walletConnector: PrivyConnector | null;
282
368
  /**
283
369
  * Unlink an email account from a user, by passing the email address. Note that you can only unlink an email account if the user has at least one other account.
284
370
  */
@@ -328,4 +414,262 @@ declare const usePrivy: () => PrivyInterface;
328
414
 
329
415
  declare const VERSION: string;
330
416
 
331
- export { Discord, DiscordOAuthWithMetadata, Email, EmailWithMetadata, Google, GoogleOAuthWithMetadata, Phone, PhoneWithMetadata, PrivyInterface, PrivyProvider, PrivyProviderProps, Twitter, TwitterOAuthWithMetadata, User, VERSION, Wallet, WalletWithMetadata, getAccessToken, usePrivy };
417
+ interface DefaultsType {
418
+ baseURL: string;
419
+ timeout: number;
420
+ }
421
+ /**
422
+ * A raw http handler for making requests to the Privy API. It requires a Session
423
+ * object, which is used for fetching and including any tokens that are required
424
+ * for requests.
425
+ */
426
+ declare class Http {
427
+ private appId;
428
+ private session;
429
+ private defaults;
430
+ private sdkVersion;
431
+ constructor(appId: string, session: Session, defaults: DefaultsType);
432
+ get<T = any, R = AxiosResponse<T>, D = any>(path: string, config?: AxiosRequestConfig<D>): Promise<R>;
433
+ post<T = any, R = AxiosResponse<T>, D = any>(path: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
434
+ delete<T = any, R = AxiosResponse<T>, D = any>(path: string, config?: AxiosRequestConfig<D>): Promise<R>;
435
+ private buildConfig;
436
+ }
437
+
438
+ interface ResponseEmailAccount {
439
+ type: 'email';
440
+ address: string;
441
+ verified_at: number;
442
+ }
443
+ interface ResponsePhoneAccount {
444
+ type: 'phone';
445
+ phoneNumber: string;
446
+ verified_at: number;
447
+ }
448
+ interface ResponseEthereumAccount {
449
+ type: 'wallet';
450
+ address: string;
451
+ chain_type: 'ethereum';
452
+ verified_at: number;
453
+ }
454
+ interface ResponseOAuthGoogle {
455
+ type: 'google_oauth';
456
+ subject: string;
457
+ email: string;
458
+ name: string | null;
459
+ verified_at: number;
460
+ }
461
+ interface ResponseOAuthTwitter {
462
+ type: 'twitter_oauth';
463
+ subject: string;
464
+ username: string | null;
465
+ name: string | null;
466
+ verified_at: number;
467
+ }
468
+ interface ResponseOAuthDiscord {
469
+ type: 'discord_oauth';
470
+ subject: string;
471
+ username: string | null;
472
+ email: string | null;
473
+ verified_at: number;
474
+ }
475
+ type LinkedAccountsType = Array<ResponseEmailAccount | ResponsePhoneAccount | ResponseEthereumAccount | ResponseOAuthGoogle | ResponseOAuthTwitter | ResponseOAuthDiscord>;
476
+ interface GetCurrentUserResponse {
477
+ id: string;
478
+ created_at: number;
479
+ linked_accounts: LinkedAccountsType;
480
+ }
481
+
482
+ /**
483
+ * Valid /session and <>/authenticate calls will respond with a token
484
+ * as well as a valid user object for streamlining.
485
+ */
486
+ interface ValidSessionResponse {
487
+ user: GetCurrentUserResponse;
488
+ token: string;
489
+ refresh_token: string | null;
490
+ is_new_user?: boolean;
491
+ }
492
+ /**
493
+ * `Session` represents the session lifecycle of the login. It manages token
494
+ * storage, refresh, and deletion.
495
+ */
496
+ declare class Session {
497
+ private destroyOnce;
498
+ private refreshOnce;
499
+ private authenticateOnce;
500
+ private linkOnce;
501
+ private forkSessionOnce;
502
+ api?: Http;
503
+ constructor();
504
+ get token(): string | null;
505
+ get refreshToken(): string | null;
506
+ /**
507
+ * Checks to see if locally we have refresh credentials. Refresh
508
+ * credentials consist of:
509
+ *
510
+ * 1. Access token
511
+ * 2. Refresh token
512
+ *
513
+ * @returns true if we have what appear to be valid credentials, false otherwise.
514
+ */
515
+ hasRefreshCredentials(): boolean;
516
+ /**
517
+ * Checks if the session contains a valid token that is not
518
+ * expired or expiring soon.
519
+ *
520
+ * @returns true if token is considered active, false otherwise.
521
+ */
522
+ hasActiveToken(): boolean;
523
+ /**
524
+ * Checks if the session contains a valid token (correct signature, issuer, and app ID).
525
+ * Accepts an optional clockTolerance for checking the expiration claim on the session's JWT.
526
+ *
527
+ * @returns true if token is valid, false otherwise.
528
+ */
529
+ hasValidToken(verificationKey: string, appId: string, clockTolerance?: number): Promise<boolean>;
530
+ /**
531
+ * Authenticate the session.
532
+ * @returns Promise<User>
533
+ */
534
+ authenticate(authFlow: AuthFlow): Promise<{
535
+ user: User | null;
536
+ isNewUser?: boolean;
537
+ }>;
538
+ /**
539
+ * Link's a new account for a user.
540
+ * @returns Promise<User>
541
+ */
542
+ link(authFlow: AuthFlow): Promise<User | null>;
543
+ /**
544
+ * Renews the auth token using the refresh token.
545
+ * @return Promise<User> if successful with a logged-in user, Promise<null> otherwise.
546
+ */
547
+ refresh(): Promise<User | null>;
548
+ /**
549
+ * Creates a new session from the current session using the refresh token.
550
+ * @return Promise<string> if successful.
551
+ */
552
+ forkSession(): Promise<string>;
553
+ /**
554
+ * Destroy the session.
555
+ */
556
+ destroy(): Promise<void>;
557
+ private _authenticate;
558
+ private _link;
559
+ private _refresh;
560
+ private _destroy;
561
+ private _forkSession;
562
+ private destroyLocalState;
563
+ private storeToken;
564
+ private storeRefreshToken;
565
+ }
566
+
567
+ type AuthMeta = {
568
+ [key: string]: any;
569
+ };
570
+ /**
571
+ * An auth flow is an encapsulation of the business logic required for a given
572
+ * authentication process. It requires at least one definitive `authenticate`
573
+ * method that does the final token handshaking with the API, but may also
574
+ * include any number of methods/API calls necessary to set up the state (e.g.
575
+ * sending an email before being able to do a passwordless code login)
576
+ */
577
+ interface AuthFlow {
578
+ api?: Http;
579
+ /**
580
+ * Any meta information necessary for the auth flow, that may also need to be
581
+ * shared out to things like frontend components for displaying state of the
582
+ * auth flow
583
+ */
584
+ meta: AuthMeta;
585
+ /**
586
+ * Handles the API authentication call(s) to log users in.
587
+ * Any preconditions must be addressed prior to calling
588
+ */
589
+ authenticate(): Promise<ValidSessionResponse>;
590
+ /**
591
+ * Handles the API link call(s) to link new user accounts.
592
+ * Requires user to already be logged in when called.
593
+ * Any preconditions must be addressed prior to calling
594
+ */
595
+ link(): Promise<GetCurrentUserResponse>;
596
+ }
597
+
598
+ /**
599
+ * This should not be directly used by developers at the moment,
600
+ * so we doc-ignore it.
601
+ * @ignore
602
+ *
603
+ */
604
+ declare class PrivyClient {
605
+ private api;
606
+ private appId;
607
+ private session;
608
+ authFlow?: AuthFlow;
609
+ connector: PrivyConnector;
610
+ /**
611
+ * Creates a new Privy client.
612
+ * @param options Initialization options.
613
+ */
614
+ constructor(options: {
615
+ /**
616
+ * The URL of the Privy API. Defaults to `https://api.privy.io/v0`.
617
+ */
618
+ apiURL?: string;
619
+ /**
620
+ * The app id from your console
621
+ */
622
+ appId: string;
623
+ /**
624
+ * Time in milliseconds after which to timeout requests to the API and KMS. Defaults to `10000` (10 seconds).
625
+ */
626
+ timeout?: number;
627
+ });
628
+ authenticate(): Promise<{
629
+ user: User | null;
630
+ isNewUser?: boolean | undefined;
631
+ }>;
632
+ link(): Promise<User | null>;
633
+ logout(): Promise<void>;
634
+ startAuthFlow(authFlow: AuthFlow): void;
635
+ unlinkEmail(address: string): Promise<User>;
636
+ unlinkPhone(phoneNumber: string): Promise<User>;
637
+ unlinkWallet(address: string): Promise<User>;
638
+ unlinkOAuth(provider: OAuthProviderType, subject: string): Promise<User>;
639
+ /** DATA METHODS */
640
+ /**
641
+ * Fetches the currently authenticed user from the API or
642
+ * returns null if the user is not authenticated.
643
+ *
644
+ * This will refresh the user's access token and rotate
645
+ * the refresh token if needed.
646
+ *
647
+ * @returns Promise<User | null>
648
+ */
649
+ getAuthenticatedUser(): Promise<User | null>;
650
+ /**
651
+ * Grab the Privy access token for the currently logged in user. Verifies that the
652
+ * token has a valid signature, was issued by 'privy.io', and corresponds to the
653
+ * current app ID. If no valid token is found, this method will force a logout and return null.
654
+ *
655
+ * If the token is expired or expiring soon, this will attempt to
656
+ * first refresh the access token to ensure that the token is active.
657
+ *
658
+ * @returns Promise<string | null>
659
+ */
660
+ getAccessToken(): Promise<string | null>;
661
+ getAppSettings(): Promise<AppSettings>;
662
+ setActiveWallet(address: string, user: User): User;
663
+ /**
664
+ * Get a short-lived token to start a new Privy session from the existing authenticated session.
665
+ *
666
+ * Rotates the access token and refresh token.
667
+ * Raises an exception if the current session was already forked from a previous session,
668
+ * or if the current session is not authenticated.
669
+ *
670
+ * @returns Promise<string>
671
+ */
672
+ forkSession(): Promise<string>;
673
+ }
674
+
675
+ export { AsExternalProvider, Discord, DiscordOAuthWithMetadata, Email, EmailWithMetadata, Google, GoogleOAuthWithMetadata, Phone, PhoneWithMetadata, PrivyClient, PrivyConnector, PrivyInterface, PrivyProvider, PrivyProviderProps, PrivyProxyProvider, Twitter, TwitterOAuthWithMetadata, User, VERSION, Wallet, WalletWithMetadata, getAccessToken, usePrivy };