@privy-io/react-auth 1.2.2 → 1.3.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 (2) hide show
  1. package/dist/index.js +62 -69
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -50,6 +50,10 @@ var ErrorCodes = {
50
50
  message: "Signature rejected",
51
51
  detail: "Please try connecting again."
52
52
  },
53
+ ERROR_TIMED_OUT: {
54
+ message: "Could not log in with wallet",
55
+ detail: "Please try connecting again."
56
+ },
53
57
  ERROR_WALLET_CONNECTION: {
54
58
  message: "Could not log in with wallet",
55
59
  detail: "Please try connecting again."
@@ -91,10 +95,17 @@ var PrivyConnectorError = class extends PrivyError {
91
95
  this.type = "connector_error";
92
96
  }
93
97
  };
94
- var SiweSignatureRejectedError = class extends PrivyError {
98
+ var WalletTimeoutError = class extends PrivyError {
95
99
  constructor() {
96
- super("Sign-in request rejected.");
100
+ super("Wallet timeout");
101
+ this.type = "wallet_error";
102
+ }
103
+ };
104
+ var SiweSignatureFailedError = class extends PrivyError {
105
+ constructor(reason) {
106
+ super(`Sign-in request ${reason || "timed out" /* TIMED_OUT */}.`);
97
107
  this.type = "siwe_error";
108
+ this.reason = reason || "rejected" /* REJECTED */;
98
109
  }
99
110
  };
100
111
  var wrapApiError = (error) => {
@@ -479,7 +490,7 @@ var SUPPORTED_WALLET_TYPES = ["metamask", "coinbase_wallet", "wallet_connect"];
479
490
  // src/connector.ts
480
491
  var WALLET_TYPE_STORAGE_KEY = "privy:wallet_type";
481
492
  var DEFAULT_WALLET_TIMEOUT_MS = 1e4;
482
- var walletTimeout = (error = new Error("Wallet timeout"), timeoutMs = DEFAULT_WALLET_TIMEOUT_MS) => new Promise(
493
+ var walletTimeout = (error = new WalletTimeoutError(), timeoutMs = DEFAULT_WALLET_TIMEOUT_MS) => new Promise(
483
494
  (_, reject) => setTimeout(() => {
484
495
  reject(error);
485
496
  }, timeoutMs)
@@ -626,8 +637,9 @@ Resources:
626
637
  new AsExternalProvider(this.getEthereumProvider())
627
638
  );
628
639
  const signer = ethersProvider.getSigner();
629
- return Promise.race([signer.signMessage(message), walletTimeout()]).catch(() => {
630
- throw new SiweSignatureRejectedError();
640
+ return Promise.race([signer.signMessage(message), walletTimeout()]).catch((e) => {
641
+ const reason = e instanceof WalletTimeoutError ? "timed out" /* TIMED_OUT */ : "rejected" /* REJECTED */;
642
+ throw new SiweSignatureFailedError(reason);
631
643
  });
632
644
  }
633
645
  prepareMessage(chainId, address, nonce) {
@@ -839,20 +851,10 @@ var EmailFlow = class {
839
851
  };
840
852
 
841
853
  // src/mobile.ts
842
- var isMobile = (userAgent) => {
843
- if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
844
- userAgent
845
- ) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(
846
- userAgent.substr(0, 4)
847
- )) {
848
- return true;
849
- } else {
850
- return false;
851
- }
852
- };
854
+ var import_react_device_detect = require("react-device-detect");
853
855
  var mobileRedirect = (walletType) => {
854
856
  const currentURL = new URL(window.location.href);
855
- if (!window.ethereum && isMobile(navigator.userAgent || navigator.vendor || window.opera)) {
857
+ if (!window.ethereum && import_react_device_detect.isMobile) {
856
858
  let url = void 0;
857
859
  switch (walletType) {
858
860
  case "metamask":
@@ -1058,10 +1060,12 @@ var ModalHeader = ({ backFn }) => {
1058
1060
  ]
1059
1061
  });
1060
1062
  };
1061
- var MiniModalHeader = () => {
1063
+ var MiniModalHeader = ({ backFn }) => {
1062
1064
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(StyledHeader, {
1063
1065
  children: [
1064
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Placeholder, {}),
1066
+ backFn ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(BackButton, {
1067
+ backFn
1068
+ }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Placeholder, {}),
1065
1069
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Placeholder, {}),
1066
1070
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(CancelButton, {})
1067
1071
  ]
@@ -1303,6 +1307,7 @@ var WalletConnect = ({ style, ...props }) => {
1303
1307
  };
1304
1308
 
1305
1309
  // src/components/ConnectWalletButtons.tsx
1310
+ var import_react_device_detect2 = require("react-device-detect");
1306
1311
  var import_jsx_runtime9 = require("react/jsx-runtime");
1307
1312
  var ConnectWalletButtons = () => {
1308
1313
  const { navigate } = usePrivyModal();
@@ -1311,7 +1316,7 @@ var ConnectWalletButtons = () => {
1311
1316
  children: [
1312
1317
  /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(ConnectWalletButton, {
1313
1318
  onClick: () => {
1314
- if (isMetamaskInstalled()) {
1319
+ if (isMetamaskInstalled() || import_react_device_detect2.isMobile) {
1315
1320
  initLoginWithWallet("metamask");
1316
1321
  navigate("AWAITING_CONNECTION" /* AWAITING_CONNECTION */);
1317
1322
  } else {
@@ -1324,13 +1329,13 @@ var ConnectWalletButtons = () => {
1324
1329
  children: "Metamask"
1325
1330
  }),
1326
1331
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", {
1327
- children: isMetamaskInstalled() ? "Connect" : "Install"
1332
+ children: isMetamaskInstalled() || import_react_device_detect2.isMobile ? "Connect" : "Install"
1328
1333
  })
1329
1334
  ]
1330
1335
  }),
1331
1336
  /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(ConnectWalletButton, {
1332
1337
  onClick: () => {
1333
- if (isCoinbaseWalletInstalled()) {
1338
+ if (isCoinbaseWalletInstalled() || import_react_device_detect2.isMobile) {
1334
1339
  initLoginWithWallet("coinbase_wallet");
1335
1340
  navigate("AWAITING_CONNECTION" /* AWAITING_CONNECTION */);
1336
1341
  } else {
@@ -1343,7 +1348,7 @@ var ConnectWalletButtons = () => {
1343
1348
  children: "Coinbase Wallet"
1344
1349
  }),
1345
1350
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", {
1346
- children: isCoinbaseWalletInstalled() ? "Connect" : "Install"
1351
+ children: isCoinbaseWalletInstalled() || import_react_device_detect2.isMobile ? "Connect" : "Install"
1347
1352
  })
1348
1353
  ]
1349
1354
  }),
@@ -5170,14 +5175,14 @@ var WalletEducation = ({ style, ...props }) => {
5170
5175
  };
5171
5176
 
5172
5177
  // src/screens/WalletEducationScreen.tsx
5173
- var import_react_device_detect = require("react-device-detect");
5178
+ var import_react_device_detect3 = require("react-device-detect");
5174
5179
  var import_jsx_runtime23 = require("react/jsx-runtime");
5175
5180
  var WalletEducationScreen = () => {
5176
5181
  const [activeView, setActiveView] = (0, import_react8.useState)("WHAT_IS_A_WALLET" /* WHAT_IS_A_WALLET */);
5177
5182
  const { navigateBack, navigate } = usePrivyModal();
5178
5183
  const backFn = activeView === "WHAT_IS_A_WALLET" /* WHAT_IS_A_WALLET */ ? navigateBack : activeView === "PICK_A_NEW_WALLET" /* PICK_A_NEW_WALLET */ ? () => setActiveView("WHAT_IS_A_WALLET" /* WHAT_IS_A_WALLET */) : () => setActiveView("PICK_A_NEW_WALLET" /* PICK_A_NEW_WALLET */);
5179
5184
  let extensionStoreLink;
5180
- if (import_react_device_detect.isFirefox) {
5185
+ if (import_react_device_detect3.isFirefox) {
5181
5186
  extensionStoreLink = "https://addons.mozilla.org/en-US/firefox/addon/ether-metamask/";
5182
5187
  } else {
5183
5188
  extensionStoreLink = "https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en";
@@ -5569,8 +5574,7 @@ var LaterButton2 = import_styled_components12.default.button`
5569
5574
  var import_react10 = require("react");
5570
5575
  var import_styled_components13 = __toESM(require("styled-components"));
5571
5576
  var import_jsx_runtime25 = require("react/jsx-runtime");
5572
- var MAX_RETRIES = 1;
5573
- var GO_BACK_DELAY_MS = 2e3;
5577
+ var MAX_RETRIES = 2;
5574
5578
  var WalletMapping = {
5575
5579
  metamask: {
5576
5580
  name: "MetaMask",
@@ -5589,7 +5593,7 @@ var ConnectionStatusScreen = () => {
5589
5593
  var _a;
5590
5594
  const [success, setSuccess] = (0, import_react10.useState)(false);
5591
5595
  const [errorCode, setErrorCode] = (0, import_react10.useState)(void 0);
5592
- const { navigate } = usePrivyModal();
5596
+ const { navigate, navigateBack } = usePrivyModal();
5593
5597
  const { getAuthMeta, closePrivyModal, loginWithWallet } = usePrivyInternal();
5594
5598
  const [numRetries, setNumRetries] = (0, import_react10.useState)(0);
5595
5599
  const { user } = usePrivy();
@@ -5599,8 +5603,12 @@ var ConnectionStatusScreen = () => {
5599
5603
  console.error(e);
5600
5604
  if ((e == null ? void 0 : e.status) === 422) {
5601
5605
  setErrorCode(ErrorCodes.ERROR_USER_EXISTS);
5602
- } else if (e instanceof SiweSignatureRejectedError) {
5603
- setErrorCode(ErrorCodes.ERROR_SIGNATURE_REJECTED);
5606
+ } else if (e instanceof SiweSignatureFailedError) {
5607
+ if (e.reason === "timed out" /* TIMED_OUT */) {
5608
+ setErrorCode(ErrorCodes.ERROR_TIMED_OUT);
5609
+ } else {
5610
+ setErrorCode(ErrorCodes.ERROR_SIGNATURE_REJECTED);
5611
+ }
5604
5612
  } else {
5605
5613
  setErrorCode(ErrorCodes.ERROR_WALLET_CONNECTION);
5606
5614
  }
@@ -5624,21 +5632,29 @@ var ConnectionStatusScreen = () => {
5624
5632
  const walletType = ((_a = getAuthMeta()) == null ? void 0 : _a.wallet) || "metamask";
5625
5633
  const walletName = WalletMapping[walletType].name;
5626
5634
  const WalletLogo = WalletMapping[walletType].component;
5635
+ const isRetryableError = errorCode === ErrorCodes.ERROR_WALLET_CONNECTION || errorCode === ErrorCodes.ERROR_SIGNATURE_REJECTED || errorCode === ErrorCodes.ERROR_TIMED_OUT;
5627
5636
  const title = success ? `Successfully connected with ${walletName}` : errorCode ? errorCode.message : `Waiting for ${walletName}`;
5628
- const walletInstructions = (walletType2) => {
5629
- switch (walletType2) {
5637
+ let subtitle = "";
5638
+ if (success) {
5639
+ subtitle = "You\u2019re good to go!";
5640
+ } else if (numRetries >= MAX_RETRIES && errorCode) {
5641
+ subtitle = "Unable to connect wallet";
5642
+ } else if (errorCode) {
5643
+ subtitle = errorCode.detail;
5644
+ } else {
5645
+ switch (walletType) {
5630
5646
  case "wallet_connect":
5631
- return "Open your mobile wallet app to continue";
5632
- case "coinbase_wallet":
5633
- return `Don't see the signature request? Check your other browser windows.`;
5647
+ subtitle = "Open your mobile wallet app to continue";
5648
+ break;
5634
5649
  default:
5635
- return `Don't see the signature request?`;
5650
+ subtitle = `Don\u2019t see your wallet modal? Check your other browser windows.`;
5636
5651
  }
5637
- };
5638
- const subtitle = success ? "You\u2019re good to go!" : errorCode ? errorCode.detail : walletInstructions(walletType);
5652
+ }
5639
5653
  return /* @__PURE__ */ (0, import_jsx_runtime25.jsxs)(import_jsx_runtime25.Fragment, {
5640
5654
  children: [
5641
- /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(MiniModalHeader, {}),
5655
+ /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(MiniModalHeader, {
5656
+ backFn: navigateBack
5657
+ }),
5642
5658
  /* @__PURE__ */ (0, import_jsx_runtime25.jsxs)(ConnectContainer4, {
5643
5659
  children: [
5644
5660
  /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(StackedContainer, {
@@ -5664,10 +5680,13 @@ var ConnectionStatusScreen = () => {
5664
5680
  })
5665
5681
  ]
5666
5682
  }),
5667
- !success ? /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(RetryOrBackButton, {
5668
- errorCode,
5669
- numRetries,
5670
- setNumRetries
5683
+ !success ? /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(CallToActionButton, {
5684
+ onClick: () => {
5685
+ setNumRetries(numRetries + 1);
5686
+ setErrorCode(void 0);
5687
+ },
5688
+ visible: isRetryableError && numRetries < MAX_RETRIES,
5689
+ children: "Retry"
5671
5690
  }) : null
5672
5691
  ]
5673
5692
  }),
@@ -5675,32 +5694,6 @@ var ConnectionStatusScreen = () => {
5675
5694
  ]
5676
5695
  });
5677
5696
  };
5678
- var RetryOrBackButton = ({
5679
- errorCode,
5680
- numRetries,
5681
- setNumRetries
5682
- }) => {
5683
- const { navigateBack } = usePrivyModal();
5684
- const [showCallToAction, setCallToAction] = (0, import_react10.useState)(false);
5685
- (0, import_react10.useEffect)(() => {
5686
- const timer = setTimeout(() => {
5687
- setCallToAction(true);
5688
- }, GO_BACK_DELAY_MS);
5689
- return () => clearTimeout(timer);
5690
- }, []);
5691
- const isRetryableError = errorCode === ErrorCodes.ERROR_WALLET_CONNECTION || errorCode === ErrorCodes.ERROR_SIGNATURE_REJECTED;
5692
- return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(import_jsx_runtime25.Fragment, {
5693
- children: (!errorCode || isRetryableError) && numRetries < MAX_RETRIES ? /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(CallToActionButton, {
5694
- onClick: () => setNumRetries(numRetries + 1),
5695
- visible: showCallToAction,
5696
- children: "Retry"
5697
- }) : /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(CallToActionButton, {
5698
- onClick: navigateBack,
5699
- visible: showCallToAction,
5700
- children: "Go back"
5701
- })
5702
- });
5703
- };
5704
5697
  var ConnectContainer4 = import_styled_components13.default.div`
5705
5698
  display: flex;
5706
5699
  flex-direction: column;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@privy-io/react-auth",
3
- "version": "1.2.2",
3
+ "version": "1.3.0-beta.1",
4
4
  "description": "React client for the Privy Auth API",
5
5
  "keywords": [
6
6
  "authentication",