@block-auth.io/core 0.5.1 → 0.6.0
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 +130 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +130 -37
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1474,8 +1474,8 @@ async function emitOauthSignup(deps) {
|
|
|
1474
1474
|
|
|
1475
1475
|
// src/auth/txns.ts
|
|
1476
1476
|
function requestSignin(args) {
|
|
1477
|
-
const { wsc, token, url, service, apiKey } = args;
|
|
1478
|
-
wsc.emit("user:txns:create", { token, url, service, apiKey });
|
|
1477
|
+
const { wsc, token, url, service, apiKey, sessionId } = args;
|
|
1478
|
+
wsc.emit("user:txns:create", { token, url, service, apiKey, sessionId });
|
|
1479
1479
|
}
|
|
1480
1480
|
function cancelTxns(args) {
|
|
1481
1481
|
args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
|
|
@@ -2291,7 +2291,8 @@ function createBlockAuthClient(config3) {
|
|
|
2291
2291
|
token: userToken(),
|
|
2292
2292
|
url: typeof window !== "undefined" ? window.location.href : "",
|
|
2293
2293
|
service: $ui.get().serviceName ?? "blockauth",
|
|
2294
|
-
apiKey: config3.apiKey
|
|
2294
|
+
apiKey: config3.apiKey,
|
|
2295
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2295
2296
|
}),
|
|
2296
2297
|
cancelTxns: () => cancelTxns({
|
|
2297
2298
|
wsc: provider.wsc,
|
|
@@ -2347,7 +2348,8 @@ function createBlockAuthClient(config3) {
|
|
|
2347
2348
|
}
|
|
2348
2349
|
|
|
2349
2350
|
// src/runtime/authRuntime.ts
|
|
2350
|
-
var SIGNIN_TIMEOUT_MS =
|
|
2351
|
+
var SIGNIN_TIMEOUT_MS = 12e4;
|
|
2352
|
+
var USER_TXNS_POLL_MS = 1500;
|
|
2351
2353
|
function createAuthRuntime(client, callbacks = {}) {
|
|
2352
2354
|
const provider = client.provider;
|
|
2353
2355
|
const hookState = {
|
|
@@ -2356,12 +2358,45 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2356
2358
|
isSigning: false,
|
|
2357
2359
|
isLoggingOut: false,
|
|
2358
2360
|
lastCheckedDid: null,
|
|
2359
|
-
signinAttemptId: 0
|
|
2361
|
+
signinAttemptId: 0,
|
|
2362
|
+
pendingSignin: null,
|
|
2363
|
+
pendingTxnId: null,
|
|
2364
|
+
settledAttemptId: 0
|
|
2360
2365
|
};
|
|
2361
2366
|
let getUserTxnsInterval = null;
|
|
2362
2367
|
let unsignTimeout = null;
|
|
2363
2368
|
let signinTimeout = null;
|
|
2364
2369
|
const wsc = () => provider.wsc;
|
|
2370
|
+
const emitSdkSign = (token, address) => {
|
|
2371
|
+
wsc().emit("sdk:sign", { token, address });
|
|
2372
|
+
};
|
|
2373
|
+
const ownTxnItems = (items) => {
|
|
2374
|
+
const apiKey = client.apiKey;
|
|
2375
|
+
if (!apiKey) return items;
|
|
2376
|
+
return items.filter((item) => {
|
|
2377
|
+
let preParams = item?.pre_params;
|
|
2378
|
+
if (typeof preParams === "string") {
|
|
2379
|
+
try {
|
|
2380
|
+
preParams = JSON.parse(preParams);
|
|
2381
|
+
} catch {
|
|
2382
|
+
preParams = null;
|
|
2383
|
+
}
|
|
2384
|
+
}
|
|
2385
|
+
const appKey = preParams?.appKey;
|
|
2386
|
+
return !appKey || appKey === apiKey;
|
|
2387
|
+
});
|
|
2388
|
+
};
|
|
2389
|
+
const pollUserTxns = (token, shouldContinue, intervalMs) => {
|
|
2390
|
+
if (getUserTxnsInterval) clearInterval(getUserTxnsInterval);
|
|
2391
|
+
getUserTxnsInterval = setInterval(() => {
|
|
2392
|
+
if (shouldContinue()) {
|
|
2393
|
+
wsc().emit("user:txns", { token });
|
|
2394
|
+
} else if (getUserTxnsInterval) {
|
|
2395
|
+
clearInterval(getUserTxnsInterval);
|
|
2396
|
+
getUserTxnsInterval = null;
|
|
2397
|
+
}
|
|
2398
|
+
}, intervalMs);
|
|
2399
|
+
};
|
|
2365
2400
|
const onSuccess = callbacks.onSuccess ?? (async (_address) => {
|
|
2366
2401
|
});
|
|
2367
2402
|
const onError = callbacks.onError ?? (async () => {
|
|
@@ -2373,9 +2408,15 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2373
2408
|
await onSuccess(address);
|
|
2374
2409
|
};
|
|
2375
2410
|
const _onError = async () => {
|
|
2411
|
+
if (unsignTimeout) {
|
|
2412
|
+
clearTimeout(unsignTimeout);
|
|
2413
|
+
unsignTimeout = null;
|
|
2414
|
+
}
|
|
2376
2415
|
hookState.isUnsignTimeoutSet = false;
|
|
2377
2416
|
hookState.isSigned = false;
|
|
2378
2417
|
hookState.isSigning = false;
|
|
2418
|
+
hookState.pendingSignin = null;
|
|
2419
|
+
hookState.pendingTxnId = null;
|
|
2379
2420
|
if (getUserTxnsInterval) {
|
|
2380
2421
|
clearInterval(getUserTxnsInterval);
|
|
2381
2422
|
getUserTxnsInterval = null;
|
|
@@ -2385,6 +2426,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2385
2426
|
setIsSigning(false);
|
|
2386
2427
|
setIsSigningLoader(false);
|
|
2387
2428
|
setIsAcceptingTxn(false);
|
|
2429
|
+
closeAllModals();
|
|
2388
2430
|
resetUser();
|
|
2389
2431
|
await onError();
|
|
2390
2432
|
};
|
|
@@ -2439,6 +2481,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2439
2481
|
}
|
|
2440
2482
|
hookState.isSigning = false;
|
|
2441
2483
|
hookState.isSigned = false;
|
|
2484
|
+
hookState.pendingSignin = null;
|
|
2485
|
+
hookState.pendingTxnId = null;
|
|
2442
2486
|
setIsSigned(false);
|
|
2443
2487
|
setIsSigning(false);
|
|
2444
2488
|
setIsSigningLoader(false);
|
|
@@ -2492,6 +2536,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2492
2536
|
hookState.isSigning = false;
|
|
2493
2537
|
hookState.isSigned = false;
|
|
2494
2538
|
hookState.isUnsignTimeoutSet = false;
|
|
2539
|
+
hookState.pendingSignin = null;
|
|
2540
|
+
hookState.pendingTxnId = null;
|
|
2495
2541
|
}
|
|
2496
2542
|
};
|
|
2497
2543
|
const onSignup = (result) => {
|
|
@@ -2535,10 +2581,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2535
2581
|
const _eDid = browserBase.getExternalService().did;
|
|
2536
2582
|
const service = browserBase.getExternalService().service;
|
|
2537
2583
|
const currentDid = service === "blockauth" ? _sDid : _eDid;
|
|
2538
|
-
|
|
2539
|
-
token: _accessToken,
|
|
2540
|
-
address: currentDid
|
|
2541
|
-
});
|
|
2584
|
+
emitSdkSign(_accessToken, currentDid);
|
|
2542
2585
|
} else {
|
|
2543
2586
|
if (hasPersistedSessionCandidate) {
|
|
2544
2587
|
const _accessToken = wsc().accessToken;
|
|
@@ -2566,11 +2609,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2566
2609
|
const signedExpiredAt = new Date(signedExpiredAtDateString);
|
|
2567
2610
|
const timeDifference = signedExpiredAt.getTime() - currentDate.getTime();
|
|
2568
2611
|
unsignTimeout = setTimeout(() => {
|
|
2569
|
-
|
|
2570
|
-
wsc().emit("sdk:sign", {
|
|
2571
|
-
token: _accessToken,
|
|
2572
|
-
address: did
|
|
2573
|
-
});
|
|
2612
|
+
emitSdkSign(wsc().accessToken, did);
|
|
2574
2613
|
}, timeDifference);
|
|
2575
2614
|
};
|
|
2576
2615
|
const signinUnsigned = async () => {
|
|
@@ -2610,23 +2649,69 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2610
2649
|
}
|
|
2611
2650
|
void isUserSignedIn();
|
|
2612
2651
|
};
|
|
2652
|
+
const captureOwnTxnId = (items) => {
|
|
2653
|
+
if (!hookState.pendingSignin || hookState.pendingTxnId != null) return;
|
|
2654
|
+
const initialItem = items.find((item) => item.state === "initial");
|
|
2655
|
+
if (initialItem && typeof initialItem.id !== "undefined") {
|
|
2656
|
+
hookState.pendingTxnId = initialItem.id;
|
|
2657
|
+
}
|
|
2658
|
+
};
|
|
2659
|
+
const settleSigningAttempt = (outcome) => {
|
|
2660
|
+
setWaitingForSuccessLogInSince(0);
|
|
2661
|
+
setIsSigning(false);
|
|
2662
|
+
hookState.isSigning = false;
|
|
2663
|
+
if (getUserTxnsInterval) {
|
|
2664
|
+
clearInterval(getUserTxnsInterval);
|
|
2665
|
+
getUserTxnsInterval = null;
|
|
2666
|
+
}
|
|
2667
|
+
if (outcome === "cancelled") {
|
|
2668
|
+
hookState.pendingSignin = null;
|
|
2669
|
+
hookState.pendingTxnId = null;
|
|
2670
|
+
hookState.settledAttemptId = hookState.signinAttemptId;
|
|
2671
|
+
if (!hookState.isSigned) closeAllModals();
|
|
2672
|
+
return;
|
|
2673
|
+
}
|
|
2674
|
+
if (hookState.settledAttemptId === hookState.signinAttemptId) {
|
|
2675
|
+
return;
|
|
2676
|
+
}
|
|
2677
|
+
hookState.settledAttemptId = hookState.signinAttemptId;
|
|
2678
|
+
const pendingSignin = hookState.pendingSignin;
|
|
2679
|
+
hookState.pendingSignin = null;
|
|
2680
|
+
const willVerifySign = outcome === "resolved" && !hookState.isSigned && !!pendingSignin;
|
|
2681
|
+
if (willVerifySign) {
|
|
2682
|
+
emitSdkSign(pendingSignin.accessToken, pendingSignin.did);
|
|
2683
|
+
} else if (!hookState.isSigned) {
|
|
2684
|
+
closeAllModals();
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2613
2687
|
const onUserTxns = async (data) => {
|
|
2614
2688
|
const { success, message } = data;
|
|
2615
2689
|
if (!success) return;
|
|
2616
|
-
const
|
|
2690
|
+
const items = ownTxnItems(message.items ?? []);
|
|
2691
|
+
captureOwnTxnId(items);
|
|
2692
|
+
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
2617
2693
|
if (!hasInitialTxns) {
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2694
|
+
settleSigningAttempt("resolved");
|
|
2695
|
+
}
|
|
2696
|
+
setRequests(items);
|
|
2697
|
+
};
|
|
2698
|
+
const onUserTxnsUpdate = async (data) => {
|
|
2699
|
+
const { success, message } = data;
|
|
2700
|
+
if (!success) return;
|
|
2701
|
+
const items = ownTxnItems(message.items ?? []);
|
|
2702
|
+
captureOwnTxnId(items);
|
|
2703
|
+
const hasInitialTxns = items.some((item) => item.state === "initial");
|
|
2704
|
+
if (!hasInitialTxns) {
|
|
2705
|
+
const txnId = hookState.pendingTxnId;
|
|
2706
|
+
const wasCancelled = txnId != null && !items.some((item) => item.id === txnId);
|
|
2707
|
+
settleSigningAttempt(
|
|
2708
|
+
wasCancelled ? "cancelled" : items.length > 0 ? "resolved" : "none"
|
|
2709
|
+
);
|
|
2625
2710
|
}
|
|
2626
|
-
setRequests(
|
|
2711
|
+
setRequests(items);
|
|
2627
2712
|
};
|
|
2628
2713
|
const onUserTxnsCreate = async (data) => {
|
|
2629
|
-
const { success } = data;
|
|
2714
|
+
const { success, message } = data;
|
|
2630
2715
|
if (success) {
|
|
2631
2716
|
setIsSigning(true);
|
|
2632
2717
|
hookState.isSigning = true;
|
|
@@ -2641,13 +2726,22 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2641
2726
|
const s = browserBase.getSession();
|
|
2642
2727
|
const externalService = browserBase.getExternalService();
|
|
2643
2728
|
let token = s.token;
|
|
2729
|
+
let did = s.did;
|
|
2644
2730
|
if (externalService.service !== "blockauth") {
|
|
2645
2731
|
token = externalService.token;
|
|
2732
|
+
did = externalService.did;
|
|
2646
2733
|
}
|
|
2734
|
+
const accessToken = wsc()?.accessToken;
|
|
2735
|
+
hookState.pendingSignin = accessToken && did ? { accessToken, did, attemptId } : null;
|
|
2736
|
+
hookState.pendingTxnId = null;
|
|
2737
|
+
captureOwnTxnId(ownTxnItems(message?.items ?? []));
|
|
2647
2738
|
wsc().emit("user:txns", { token });
|
|
2739
|
+
pollUserTxns(token, () => hookState.isSigning, USER_TXNS_POLL_MS);
|
|
2648
2740
|
} else {
|
|
2649
2741
|
setIsSigning(false);
|
|
2650
2742
|
hookState.isSigning = false;
|
|
2743
|
+
hookState.pendingSignin = null;
|
|
2744
|
+
hookState.pendingTxnId = null;
|
|
2651
2745
|
}
|
|
2652
2746
|
};
|
|
2653
2747
|
const onSdkSign = async (data) => {
|
|
@@ -2670,19 +2764,14 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2670
2764
|
localStorage.removeItem("blockauth-logged-out");
|
|
2671
2765
|
}
|
|
2672
2766
|
if (getUserTxnsInterval === null) {
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
getUserTxnsInterval = null;
|
|
2679
|
-
}
|
|
2680
|
-
}, 500);
|
|
2767
|
+
pollUserTxns(
|
|
2768
|
+
_token,
|
|
2769
|
+
() => hookState.isSigning && hookState.isSigned,
|
|
2770
|
+
500
|
|
2771
|
+
);
|
|
2681
2772
|
}
|
|
2682
2773
|
setUnsignTimeout(message.signedExpiredAt, _did);
|
|
2683
|
-
|
|
2684
|
-
closeAllModals();
|
|
2685
|
-
}
|
|
2774
|
+
closeAllModals();
|
|
2686
2775
|
await fetchUserProfile(provider, _did);
|
|
2687
2776
|
setWaitingForSuccessLogInSince(0);
|
|
2688
2777
|
await _onSuccess(_did);
|
|
@@ -2746,6 +2835,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2746
2835
|
setIsSigning(false);
|
|
2747
2836
|
setIsSigningLoader(false);
|
|
2748
2837
|
hookState.isSigning = false;
|
|
2838
|
+
hookState.pendingSignin = null;
|
|
2839
|
+
hookState.pendingTxnId = null;
|
|
2749
2840
|
closeAllModals();
|
|
2750
2841
|
if (typeof window !== "undefined") {
|
|
2751
2842
|
const isLandingPage = window.location.pathname === "/" || window.location.pathname === "/signin";
|
|
@@ -2860,7 +2951,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2860
2951
|
wsc().on("provider:signup", onProviderSignup);
|
|
2861
2952
|
wsc().on("provider:signin", onProviderSignin);
|
|
2862
2953
|
wsc().on("user:txns", onUserTxns);
|
|
2863
|
-
wsc().on("user:txns:update",
|
|
2954
|
+
wsc().on("user:txns:update", onUserTxnsUpdate);
|
|
2864
2955
|
wsc().on("user:txns:create", onUserTxnsCreate);
|
|
2865
2956
|
wsc().on("sdk:sign", onSdkSign);
|
|
2866
2957
|
wsc().on("signin:corrupted", onSigninCorrupted);
|
|
@@ -2897,6 +2988,8 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2897
2988
|
signinTimeout = null;
|
|
2898
2989
|
}
|
|
2899
2990
|
hookState.isSigning = false;
|
|
2991
|
+
hookState.pendingSignin = null;
|
|
2992
|
+
hookState.pendingTxnId = null;
|
|
2900
2993
|
if (getUserTxnsInterval) {
|
|
2901
2994
|
clearInterval(getUserTxnsInterval);
|
|
2902
2995
|
getUserTxnsInterval = null;
|
|
@@ -2907,7 +3000,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2907
3000
|
w.off("provider:signup", onProviderSignup);
|
|
2908
3001
|
w.off("provider:signin", onProviderSignin);
|
|
2909
3002
|
w.off("user:txns", onUserTxns);
|
|
2910
|
-
w.off("user:txns:update",
|
|
3003
|
+
w.off("user:txns:update", onUserTxnsUpdate);
|
|
2911
3004
|
w.off("user:txns:create", onUserTxnsCreate);
|
|
2912
3005
|
w.off("sdk:sign", onSdkSign);
|
|
2913
3006
|
w.off("signin:corrupted", onSigninCorrupted);
|