@block-auth.io/core 0.10.0 → 0.11.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 +109 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -6
- package/dist/index.d.ts +12 -6
- package/dist/index.js +109 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -57,21 +57,26 @@ async function clearSessionCookie(apiUrl) {
|
|
|
57
57
|
return false;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
async function
|
|
61
|
-
if (typeof fetch === "undefined") return null;
|
|
60
|
+
async function requestWsTicket(apiUrl) {
|
|
61
|
+
if (typeof fetch === "undefined") return { ticket: null, refused: false };
|
|
62
62
|
try {
|
|
63
63
|
const response = await fetch(buildApiUrl("/auth/ws-ticket", apiUrl), {
|
|
64
64
|
method: "GET",
|
|
65
65
|
credentials: "include"
|
|
66
66
|
});
|
|
67
|
-
if (!response.ok)
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
return { ticket: null, refused: response.status === 401 };
|
|
69
|
+
}
|
|
68
70
|
const data = await response.json();
|
|
69
|
-
return data?.ticket || null;
|
|
71
|
+
return { ticket: data?.ticket || null, refused: false };
|
|
70
72
|
} catch (error) {
|
|
71
73
|
console.warn("[block-auth] failed to fetch ws ticket", error);
|
|
72
|
-
return null;
|
|
74
|
+
return { ticket: null, refused: false };
|
|
73
75
|
}
|
|
74
76
|
}
|
|
77
|
+
async function fetchWsTicket(apiUrl) {
|
|
78
|
+
return (await requestWsTicket(apiUrl)).ticket;
|
|
79
|
+
}
|
|
75
80
|
var DEFAULT_WS_URL = "wss://wss.block-auth.io";
|
|
76
81
|
function buildWsServerApiUrl(path, wsUrl) {
|
|
77
82
|
const url = new URL(wsUrl || DEFAULT_WS_URL);
|
|
@@ -220,13 +225,17 @@ var WsClient = class {
|
|
|
220
225
|
* requestSignin -> user:txns:create -> sign) - this avoids that.
|
|
221
226
|
* No-op if there is no open connection yet (connect() will pick up a
|
|
222
227
|
* ticket on its own once one exists).
|
|
223
|
-
* @returns
|
|
228
|
+
* @returns 'renewed' when a ticket was sent, 'refused' when the API answered
|
|
229
|
+
* 401 (no session behind the cookie), 'unavailable' otherwise (not
|
|
230
|
+
* connected yet, or a transient failure).
|
|
224
231
|
*/
|
|
225
232
|
async refreshAuth() {
|
|
226
|
-
if (!this.isConnected()) return;
|
|
227
|
-
const ticket = await
|
|
228
|
-
if (
|
|
233
|
+
if (!this.isConnected()) return "unavailable";
|
|
234
|
+
const { ticket, refused } = await requestWsTicket(this._apiUrl);
|
|
235
|
+
if (refused) return "refused";
|
|
236
|
+
if (!ticket) return "unavailable";
|
|
229
237
|
this._ws.emit("auth:refresh", ticket);
|
|
238
|
+
return "renewed";
|
|
230
239
|
}
|
|
231
240
|
/**
|
|
232
241
|
* Allow you to subscribe to an event, and
|
|
@@ -2861,6 +2870,29 @@ var undoSolicitedValue = (form, item) => ({
|
|
|
2861
2870
|
shared: (item.value ?? "").trim() ? form.shared : omitKey(form.shared, item.slug)
|
|
2862
2871
|
});
|
|
2863
2872
|
|
|
2873
|
+
// src/session/sessionExpired.ts
|
|
2874
|
+
var USER_SESSION_REQUIRED = "USER_SESSION_REQUIRED";
|
|
2875
|
+
var SESSION_EXPIRED_MARK = "blockauth-session-expired";
|
|
2876
|
+
function markSessionExpired() {
|
|
2877
|
+
try {
|
|
2878
|
+
sessionStorage.setItem(SESSION_EXPIRED_MARK, "1");
|
|
2879
|
+
} catch {
|
|
2880
|
+
}
|
|
2881
|
+
}
|
|
2882
|
+
function hasSessionExpiredMark() {
|
|
2883
|
+
try {
|
|
2884
|
+
return sessionStorage.getItem(SESSION_EXPIRED_MARK) === "1";
|
|
2885
|
+
} catch {
|
|
2886
|
+
return false;
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
function clearSessionExpiredMark() {
|
|
2890
|
+
try {
|
|
2891
|
+
sessionStorage.removeItem(SESSION_EXPIRED_MARK);
|
|
2892
|
+
} catch {
|
|
2893
|
+
}
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2864
2896
|
// src/runtime/authRuntime.ts
|
|
2865
2897
|
var SIGNIN_TIMEOUT_MS = 12e4;
|
|
2866
2898
|
var USER_TXNS_POLL_MS = 1500;
|
|
@@ -3031,6 +3063,45 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3031
3063
|
});
|
|
3032
3064
|
return logoutInFlight;
|
|
3033
3065
|
};
|
|
3066
|
+
const reactToLostSession = (isLoggingIn) => {
|
|
3067
|
+
if (browserBase.getExternalService().service === "blockauth") {
|
|
3068
|
+
if (isLoggingIn) {
|
|
3069
|
+
markSessionExpired();
|
|
3070
|
+
setIsSigningLoader(false);
|
|
3071
|
+
awaitOtpInExtension();
|
|
3072
|
+
} else if (!hasSessionExpiredMark()) {
|
|
3073
|
+
markSessionExpired();
|
|
3074
|
+
void _onError();
|
|
3075
|
+
}
|
|
3076
|
+
return;
|
|
3077
|
+
}
|
|
3078
|
+
void applyLocalLogout().then(() => {
|
|
3079
|
+
if (!isLoggingIn) return;
|
|
3080
|
+
closeModal("signin");
|
|
3081
|
+
openModal("signup");
|
|
3082
|
+
}).catch((err) => {
|
|
3083
|
+
console.error("[sdk] applyLocalLogout failed (session required):", err);
|
|
3084
|
+
});
|
|
3085
|
+
};
|
|
3086
|
+
let stopped = false;
|
|
3087
|
+
let sessionBridge = Promise.resolve();
|
|
3088
|
+
let sessionCheck = null;
|
|
3089
|
+
const canLoseSession = () => !hookState.isLoggingOut && !$ui.get().isOtpRequired && ($ui.get().isServiceConnected || hookState.isSigned);
|
|
3090
|
+
const onAnyServerEvent = (_event, data) => {
|
|
3091
|
+
if (data?.code !== USER_SESSION_REQUIRED || !canLoseSession()) return;
|
|
3092
|
+
if (sessionCheck) return;
|
|
3093
|
+
sessionCheck = (async () => {
|
|
3094
|
+
await sessionBridge.catch(() => {
|
|
3095
|
+
});
|
|
3096
|
+
const result = await wsc().refreshAuth();
|
|
3097
|
+
if (result !== "refused" || stopped || !canLoseSession()) return;
|
|
3098
|
+
reactToLostSession($openModals.get().includes("signin"));
|
|
3099
|
+
})().catch((err) => {
|
|
3100
|
+
console.error("[sdk] session check failed:", err);
|
|
3101
|
+
}).finally(() => {
|
|
3102
|
+
sessionCheck = null;
|
|
3103
|
+
});
|
|
3104
|
+
};
|
|
3034
3105
|
const onStorageLogout = (event) => {
|
|
3035
3106
|
if (event.key !== "blockauth-logged-out" || event.newValue !== "true") {
|
|
3036
3107
|
return;
|
|
@@ -3041,9 +3112,18 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3041
3112
|
});
|
|
3042
3113
|
};
|
|
3043
3114
|
let sessionBridged = false;
|
|
3044
|
-
const onExtensionSessionBridged = () => {
|
|
3115
|
+
const onExtensionSessionBridged = async () => {
|
|
3045
3116
|
sessionBridged = true;
|
|
3046
|
-
|
|
3117
|
+
const result = await wsc().refreshAuth();
|
|
3118
|
+
if (result === "refused" || !hasSessionExpiredMark()) return;
|
|
3119
|
+
clearSessionExpiredMark();
|
|
3120
|
+
const awaitedSession = $ui.get().isOtpRequired && !hookState.isSigning;
|
|
3121
|
+
if (awaitedSession) setIsOtpRequired(false);
|
|
3122
|
+
if (awaitedSession && $openModals.get().includes("signin")) {
|
|
3123
|
+
client.requestSignin();
|
|
3124
|
+
return;
|
|
3125
|
+
}
|
|
3126
|
+
if ($ws.get().ready) void isUserSignedIn();
|
|
3047
3127
|
};
|
|
3048
3128
|
const deletion = createDeletionService({ provider });
|
|
3049
3129
|
const handleUserDeleted = (forAddress = null) => {
|
|
@@ -3067,7 +3147,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3067
3147
|
if (service === "blockauth" || prevDid && prevDid.toLowerCase() !== String(did).toLowerCase()) {
|
|
3068
3148
|
browserBase.clearBrowserState({ keepExtensionInstalled: keepInstalled });
|
|
3069
3149
|
}
|
|
3070
|
-
|
|
3150
|
+
sessionBridge = browserBase.setSession({
|
|
3071
3151
|
...result,
|
|
3072
3152
|
serviceName: service,
|
|
3073
3153
|
apiUrl: client.apiUrl
|
|
@@ -3302,6 +3382,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3302
3382
|
setToken(_token);
|
|
3303
3383
|
setIsSigned(success);
|
|
3304
3384
|
hookState.isSigned = success;
|
|
3385
|
+
if (success) clearSessionExpiredMark();
|
|
3305
3386
|
if (success && hookState.isUnsignTimeoutSet && message?.signedExpiredAt) {
|
|
3306
3387
|
rearmUnsignTimeout(message.signedExpiredAt, _did);
|
|
3307
3388
|
}
|
|
@@ -3460,6 +3541,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3460
3541
|
}
|
|
3461
3542
|
};
|
|
3462
3543
|
const start = async () => {
|
|
3544
|
+
stopped = false;
|
|
3463
3545
|
const s = browserBase.getSession();
|
|
3464
3546
|
const externalService = browserBase.getExternalService();
|
|
3465
3547
|
let _token = s.token;
|
|
@@ -3520,15 +3602,21 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3520
3602
|
wsc().on("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
3521
3603
|
wsc().on("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
3522
3604
|
wsc().on("sdk:restore", onSdkRestore);
|
|
3605
|
+
wsc().ws?.onAny(onAnyServerEvent);
|
|
3523
3606
|
if (typeof window !== "undefined") {
|
|
3524
3607
|
window.addEventListener("storage", onStorageLogout);
|
|
3525
3608
|
}
|
|
3526
3609
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
3527
3610
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
3528
3611
|
setReady(true);
|
|
3612
|
+
if (externalService.service === "blockauth" && hasSessionExpiredMark()) {
|
|
3613
|
+
setIsSignedFirstCheck(true);
|
|
3614
|
+
return;
|
|
3615
|
+
}
|
|
3529
3616
|
await isUserSignedIn();
|
|
3530
3617
|
};
|
|
3531
3618
|
const stop = () => {
|
|
3619
|
+
stopped = true;
|
|
3532
3620
|
if (unsignTimeout) {
|
|
3533
3621
|
clearTimeout(unsignTimeout);
|
|
3534
3622
|
unsignTimeout = null;
|
|
@@ -3563,6 +3651,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
3563
3651
|
w.off("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
3564
3652
|
w.off("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
3565
3653
|
w.off("sdk:restore", onSdkRestore);
|
|
3654
|
+
w.ws?.offAny(onAnyServerEvent);
|
|
3566
3655
|
}
|
|
3567
3656
|
if (typeof window !== "undefined") {
|
|
3568
3657
|
window.removeEventListener("storage", onStorageLogout);
|
|
@@ -3700,13 +3789,17 @@ var TEXTS = {
|
|
|
3700
3789
|
es: "No se encontraron transacciones",
|
|
3701
3790
|
en: "No transactions found"
|
|
3702
3791
|
},
|
|
3792
|
+
otpExpiredTitle: {
|
|
3793
|
+
es: "Verificaci\xF3n caducada",
|
|
3794
|
+
en: "Verification expired"
|
|
3795
|
+
},
|
|
3703
3796
|
otpExpiredExtension: {
|
|
3704
|
-
es: "
|
|
3705
|
-
en: "
|
|
3797
|
+
es: "Introduce tu c\xF3digo en la extensi\xF3n de Block-Auth para aprobar este acceso.",
|
|
3798
|
+
en: "Enter your code in the Block-Auth extension to approve this access."
|
|
3706
3799
|
},
|
|
3707
3800
|
otpExpiredApp: {
|
|
3708
|
-
es: "
|
|
3709
|
-
en: "
|
|
3801
|
+
es: "Introduce tu c\xF3digo en la app de Block-Auth para aprobar este acceso.",
|
|
3802
|
+
en: "Enter your code in the Block-Auth app to approve this access."
|
|
3710
3803
|
},
|
|
3711
3804
|
openExtension: { es: "Abrir extensi\xF3n", en: "Open extension" },
|
|
3712
3805
|
noAppKeyFound: {
|