@swype-org/react-sdk 0.1.15 → 0.1.16

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.cjs CHANGED
@@ -738,6 +738,28 @@ async function createPasskeyCredential(userIdentifier) {
738
738
  publicKey: publicKeyBytes ? toBase64(publicKeyBytes) : ""
739
739
  };
740
740
  }
741
+ async function deviceHasPasskey(credentialId) {
742
+ try {
743
+ const challenge = new Uint8Array(32);
744
+ crypto.getRandomValues(challenge);
745
+ await waitForDocumentFocus();
746
+ const assertion = await navigator.credentials.get({
747
+ publicKey: {
748
+ challenge,
749
+ rpId: resolvePasskeyRpId(),
750
+ allowCredentials: [{
751
+ type: "public-key",
752
+ id: base64ToBytes(credentialId)
753
+ }],
754
+ userVerification: "required",
755
+ timeout: 3e4
756
+ }
757
+ });
758
+ return assertion != null;
759
+ } catch {
760
+ return false;
761
+ }
762
+ }
741
763
  function useTransferPolling(intervalMs = 3e3) {
742
764
  const { apiBaseUrl } = useSwypeConfig();
743
765
  const { getAccessToken } = reactAuth.usePrivy();
@@ -2141,12 +2163,27 @@ function SwypePayment({
2141
2163
  if (!token || cancelled) return;
2142
2164
  const { config } = await fetchUserConfig(apiBaseUrl, token);
2143
2165
  if (cancelled) return;
2144
- if (!config.passkey || !activeCredentialId) {
2145
- setStep("register-passkey");
2146
- } else if (depositAmount != null && depositAmount > 0) {
2147
- setStep("ready");
2166
+ if (config.passkey?.credentialId) {
2167
+ const hasKey = activeCredentialId ? activeCredentialId === config.passkey.credentialId : await deviceHasPasskey(config.passkey.credentialId);
2168
+ if (cancelled) return;
2169
+ if (hasKey) {
2170
+ if (!activeCredentialId) {
2171
+ setActiveCredentialId(config.passkey.credentialId);
2172
+ window.localStorage.setItem(
2173
+ ACTIVE_CREDENTIAL_STORAGE_KEY,
2174
+ config.passkey.credentialId
2175
+ );
2176
+ }
2177
+ if (depositAmount != null && depositAmount > 0) {
2178
+ setStep("ready");
2179
+ } else {
2180
+ setStep("enter-amount");
2181
+ }
2182
+ } else {
2183
+ setStep("register-passkey");
2184
+ }
2148
2185
  } else {
2149
- setStep("enter-amount");
2186
+ setStep("register-passkey");
2150
2187
  }
2151
2188
  } catch {
2152
2189
  if (!cancelled) {
@@ -3462,6 +3499,7 @@ exports.SwypePayment = SwypePayment;
3462
3499
  exports.SwypeProvider = SwypeProvider;
3463
3500
  exports.createPasskeyCredential = createPasskeyCredential;
3464
3501
  exports.darkTheme = darkTheme;
3502
+ exports.deviceHasPasskey = deviceHasPasskey;
3465
3503
  exports.getTheme = getTheme;
3466
3504
  exports.lightTheme = lightTheme;
3467
3505
  exports.swypeApi = api_exports;