@block-auth.io/core 0.4.1 → 0.4.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.cjs +149 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +149 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -947,6 +947,7 @@ var resetWeb3 = () => $web3.set({ ...initial6 });
|
|
|
947
947
|
var UI = "blockauth-ui";
|
|
948
948
|
var SESSION = "blockauth-session";
|
|
949
949
|
var EXTERNAL_SERVICE = "blockauth-connected";
|
|
950
|
+
var DEVICE_SESSION_ID = "blockauth-device-session-id";
|
|
950
951
|
var BLOCKAUTH_STORAGE_KEYS = [
|
|
951
952
|
SESSION,
|
|
952
953
|
UI,
|
|
@@ -1022,6 +1023,21 @@ var BrowserBase = class {
|
|
|
1022
1023
|
this.getExternalService();
|
|
1023
1024
|
}
|
|
1024
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1028
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1029
|
+
* server scope a signout to just this browser instead of every device
|
|
1030
|
+
* signed in with the same account.
|
|
1031
|
+
*/
|
|
1032
|
+
getDeviceSessionId() {
|
|
1033
|
+
const storage = getStorage();
|
|
1034
|
+
if (!storage) return "";
|
|
1035
|
+
const existing = storage.getItem(DEVICE_SESSION_ID);
|
|
1036
|
+
if (existing) return existing;
|
|
1037
|
+
const generated = typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
1038
|
+
storage.setItem(DEVICE_SESSION_ID, generated);
|
|
1039
|
+
return generated;
|
|
1040
|
+
}
|
|
1025
1041
|
/**
|
|
1026
1042
|
* Obtains the current UI configuration using localStorage as the
|
|
1027
1043
|
* source of truth, seeding the default if none is stored.
|
|
@@ -1337,13 +1353,14 @@ function rejectTxn(args) {
|
|
|
1337
1353
|
args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
|
|
1338
1354
|
}
|
|
1339
1355
|
function acceptTxn(args) {
|
|
1340
|
-
const { wsc, token, lifetime, providerSlug, userParams } = args;
|
|
1356
|
+
const { wsc, token, lifetime, providerSlug, userParams, sessionId } = args;
|
|
1341
1357
|
wsc.emit("provider:signin", {
|
|
1342
1358
|
token,
|
|
1343
1359
|
lifetime,
|
|
1344
1360
|
state: "approved",
|
|
1345
1361
|
providerSlug,
|
|
1346
|
-
userParams: userParams ?? {}
|
|
1362
|
+
userParams: userParams ?? {},
|
|
1363
|
+
sessionId
|
|
1347
1364
|
});
|
|
1348
1365
|
}
|
|
1349
1366
|
function reportTxn(args) {
|
|
@@ -1789,7 +1806,7 @@ function connectBlockAuth() {
|
|
|
1789
1806
|
|
|
1790
1807
|
// src/auth/logout.ts
|
|
1791
1808
|
async function performLogout(args) {
|
|
1792
|
-
const { wsc, token, apiKey } = args;
|
|
1809
|
+
const { wsc, token, apiKey, sessionId } = args;
|
|
1793
1810
|
await new Promise((resolve) => {
|
|
1794
1811
|
let done = false;
|
|
1795
1812
|
const finish = () => {
|
|
@@ -1799,7 +1816,7 @@ async function performLogout(args) {
|
|
|
1799
1816
|
}
|
|
1800
1817
|
};
|
|
1801
1818
|
wsc.ws?.once?.("signout", finish);
|
|
1802
|
-
wsc.emit("signout", { token, apiKey });
|
|
1819
|
+
wsc.emit("signout", { token, apiKey, sessionId });
|
|
1803
1820
|
setTimeout(finish, 1500);
|
|
1804
1821
|
});
|
|
1805
1822
|
if (typeof localStorage !== "undefined") {
|
|
@@ -2072,6 +2089,7 @@ function createBlockAuthClient(config3) {
|
|
|
2072
2089
|
}
|
|
2073
2090
|
if (config3.google) configureGoogle(config3.google);
|
|
2074
2091
|
if (config3.microsoft) configureMicrosoft(config3.microsoft);
|
|
2092
|
+
setApiKey(config3.apiKey);
|
|
2075
2093
|
function userToken() {
|
|
2076
2094
|
const s = browserBase.getSession();
|
|
2077
2095
|
const ext = browserBase.getExternalService();
|
|
@@ -2085,7 +2103,8 @@ function createBlockAuthClient(config3) {
|
|
|
2085
2103
|
$openModals,
|
|
2086
2104
|
$user
|
|
2087
2105
|
},
|
|
2088
|
-
|
|
2106
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
2107
|
+
apiKey: config3.apiKey,
|
|
2089
2108
|
connect: (...a) => provider.connect(...a),
|
|
2090
2109
|
disconnect: () => provider.disconnect(),
|
|
2091
2110
|
signin: () => provider.signin(),
|
|
@@ -2110,11 +2129,23 @@ function createBlockAuthClient(config3) {
|
|
|
2110
2129
|
signupGoogle: async (flow = "auth-code") => {
|
|
2111
2130
|
const res = await signInWithGoogle(flow);
|
|
2112
2131
|
const signature = res.code ?? res.accessToken;
|
|
2113
|
-
await emitOauthSignup({
|
|
2132
|
+
await emitOauthSignup({
|
|
2133
|
+
wsc: provider.wsc,
|
|
2134
|
+
providerSlug: "google",
|
|
2135
|
+
signature,
|
|
2136
|
+
mode: flow,
|
|
2137
|
+
getIp
|
|
2138
|
+
});
|
|
2114
2139
|
},
|
|
2115
2140
|
signupMicrosoft: async () => {
|
|
2116
2141
|
const res = await signInWithMicrosoft();
|
|
2117
|
-
await emitOauthSignup({
|
|
2142
|
+
await emitOauthSignup({
|
|
2143
|
+
wsc: provider.wsc,
|
|
2144
|
+
providerSlug: "microsoft",
|
|
2145
|
+
signature: res.accessToken,
|
|
2146
|
+
mode: "popup",
|
|
2147
|
+
getIp
|
|
2148
|
+
});
|
|
2118
2149
|
}
|
|
2119
2150
|
},
|
|
2120
2151
|
// signin transaction + logout
|
|
@@ -2125,18 +2156,29 @@ function createBlockAuthClient(config3) {
|
|
|
2125
2156
|
service: $ui.get().serviceName ?? "blockauth",
|
|
2126
2157
|
apiKey: config3.apiKey
|
|
2127
2158
|
}),
|
|
2128
|
-
cancelTxns: () => cancelTxns({
|
|
2159
|
+
cancelTxns: () => cancelTxns({
|
|
2160
|
+
wsc: provider.wsc,
|
|
2161
|
+
token: userToken()
|
|
2162
|
+
}),
|
|
2129
2163
|
// transaction accept/reject
|
|
2130
2164
|
txns: {
|
|
2131
|
-
reject: () => rejectTxn({
|
|
2165
|
+
reject: () => rejectTxn({
|
|
2166
|
+
wsc: provider.wsc,
|
|
2167
|
+
token: userToken()
|
|
2168
|
+
}),
|
|
2132
2169
|
accept: (opts) => acceptTxn({
|
|
2133
2170
|
wsc: provider.wsc,
|
|
2134
2171
|
token: userToken(),
|
|
2135
2172
|
lifetime: opts.lifetime,
|
|
2136
2173
|
providerSlug: $ui.get().serviceName ?? "blockauth",
|
|
2137
|
-
userParams: opts.userParams
|
|
2174
|
+
userParams: opts.userParams,
|
|
2175
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2138
2176
|
}),
|
|
2139
|
-
report: (slug) => reportTxn({
|
|
2177
|
+
report: (slug) => reportTxn({
|
|
2178
|
+
wsc: provider.wsc,
|
|
2179
|
+
token: userToken(),
|
|
2180
|
+
report: slug
|
|
2181
|
+
})
|
|
2140
2182
|
},
|
|
2141
2183
|
// account restore: resolves when the backend answers; expired
|
|
2142
2184
|
// restorations surface the restorationExpired modal, other failures
|
|
@@ -2145,7 +2187,12 @@ function createBlockAuthClient(config3) {
|
|
|
2145
2187
|
// full deletion-state teardown after the user confirms the
|
|
2146
2188
|
// restoration-expired modal.
|
|
2147
2189
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2148
|
-
logout: () => performLogout({
|
|
2190
|
+
logout: () => performLogout({
|
|
2191
|
+
wsc: provider.wsc,
|
|
2192
|
+
token: userToken(),
|
|
2193
|
+
apiKey: config3.apiKey,
|
|
2194
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2195
|
+
}),
|
|
2149
2196
|
// modals (stack — supports overlaying signin on top of signup)
|
|
2150
2197
|
openModal,
|
|
2151
2198
|
closeModal,
|
|
@@ -2166,6 +2213,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2166
2213
|
isUnsignTimeoutSet: false,
|
|
2167
2214
|
isSigned: false,
|
|
2168
2215
|
isSigning: false,
|
|
2216
|
+
isLoggingOut: false,
|
|
2169
2217
|
lastCheckedDid: null,
|
|
2170
2218
|
signinAttemptId: 0
|
|
2171
2219
|
};
|
|
@@ -2200,33 +2248,88 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2200
2248
|
await onError();
|
|
2201
2249
|
};
|
|
2202
2250
|
const _onLogout = async () => {
|
|
2251
|
+
if (hookState.isLoggingOut) return applyLocalLogout();
|
|
2252
|
+
hookState.isLoggingOut = true;
|
|
2203
2253
|
resetUser();
|
|
2254
|
+
if (typeof localStorage !== "undefined") {
|
|
2255
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2256
|
+
}
|
|
2204
2257
|
try {
|
|
2205
2258
|
if (provider && provider.wsc) {
|
|
2206
2259
|
const s = browserBase.getSession();
|
|
2207
2260
|
const ext = browserBase.getExternalService();
|
|
2208
2261
|
const _token = ext.service !== "blockauth" ? ext.token : s.token;
|
|
2209
|
-
const _apiKey = $ws.get().apiKey;
|
|
2262
|
+
const _apiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2263
|
+
const _sessionId = browserBase.getDeviceSessionId();
|
|
2210
2264
|
await new Promise((resolve) => {
|
|
2211
2265
|
const timeout = setTimeout(resolve, 3e3);
|
|
2212
2266
|
wsc().ws.once("signout", () => {
|
|
2213
2267
|
clearTimeout(timeout);
|
|
2214
2268
|
resolve();
|
|
2215
2269
|
});
|
|
2216
|
-
wsc().emit("signout", {
|
|
2270
|
+
wsc().emit("signout", {
|
|
2271
|
+
token: _token,
|
|
2272
|
+
apiKey: _apiKey,
|
|
2273
|
+
sessionId: _sessionId
|
|
2274
|
+
});
|
|
2217
2275
|
});
|
|
2218
2276
|
}
|
|
2219
2277
|
} catch {
|
|
2220
2278
|
}
|
|
2221
|
-
|
|
2222
|
-
|
|
2279
|
+
await applyLocalLogout();
|
|
2280
|
+
};
|
|
2281
|
+
let logoutInFlight = null;
|
|
2282
|
+
const applyLocalLogout = async () => {
|
|
2283
|
+
if (logoutInFlight) return logoutInFlight;
|
|
2284
|
+
logoutInFlight = (async () => {
|
|
2285
|
+
hookState.isLoggingOut = true;
|
|
2286
|
+
if (unsignTimeout) {
|
|
2287
|
+
clearTimeout(unsignTimeout);
|
|
2288
|
+
unsignTimeout = null;
|
|
2289
|
+
}
|
|
2290
|
+
hookState.isUnsignTimeoutSet = false;
|
|
2291
|
+
if (signinTimeout) {
|
|
2292
|
+
clearTimeout(signinTimeout);
|
|
2293
|
+
signinTimeout = null;
|
|
2294
|
+
}
|
|
2295
|
+
if (getUserTxnsInterval) {
|
|
2296
|
+
clearInterval(getUserTxnsInterval);
|
|
2297
|
+
getUserTxnsInterval = null;
|
|
2298
|
+
}
|
|
2299
|
+
hookState.isSigning = false;
|
|
2300
|
+
hookState.isSigned = false;
|
|
2301
|
+
setIsSigned(false);
|
|
2302
|
+
setIsSigning(false);
|
|
2303
|
+
setIsSigningLoader(false);
|
|
2304
|
+
setIsAcceptingTxn(false);
|
|
2305
|
+
setIsSignedFirstCheck(true);
|
|
2306
|
+
resetUser();
|
|
2307
|
+
if (typeof localStorage !== "undefined") {
|
|
2308
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2309
|
+
}
|
|
2310
|
+
const serviceName = browserBase.getExternalService().service;
|
|
2311
|
+
setIsServiceConnected(false);
|
|
2312
|
+
setWaitingForSuccessLogInSince(0);
|
|
2313
|
+
setServiceIcon(imgUrlByService(serviceName));
|
|
2314
|
+
setServiceName(serviceName);
|
|
2315
|
+
try {
|
|
2316
|
+
await onLogout();
|
|
2317
|
+
} finally {
|
|
2318
|
+
hookState.isLoggingOut = false;
|
|
2319
|
+
}
|
|
2320
|
+
})().finally(() => {
|
|
2321
|
+
logoutInFlight = null;
|
|
2322
|
+
});
|
|
2323
|
+
return logoutInFlight;
|
|
2324
|
+
};
|
|
2325
|
+
const onStorageLogout = (event) => {
|
|
2326
|
+
if (event.key !== "blockauth-logged-out" || event.newValue !== "true") {
|
|
2327
|
+
return;
|
|
2223
2328
|
}
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
setServiceName(serviceName);
|
|
2229
|
-
await onLogout();
|
|
2329
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2330
|
+
void applyLocalLogout().catch((err) => {
|
|
2331
|
+
console.error("[sdk] applyLocalLogout failed (storage event):", err);
|
|
2332
|
+
});
|
|
2230
2333
|
};
|
|
2231
2334
|
const deletion = createDeletionService({ provider });
|
|
2232
2335
|
const handleUserDeleted = (forAddress = null) => {
|
|
@@ -2503,6 +2606,21 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2503
2606
|
const onSdkSignRefresh = () => {
|
|
2504
2607
|
void isUserSignedIn();
|
|
2505
2608
|
};
|
|
2609
|
+
const onSdkSessionInvalidated = (data) => {
|
|
2610
|
+
const apiKey = data?.apiKey ?? data?.message?.apiKey;
|
|
2611
|
+
const clientApiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2612
|
+
if (!apiKey || !clientApiKey || apiKey !== clientApiKey) return;
|
|
2613
|
+
const sessionId = data?.sessionId ?? data?.message?.sessionId;
|
|
2614
|
+
if (sessionId != null && String(sessionId) !== String(browserBase.getDeviceSessionId()))
|
|
2615
|
+
return;
|
|
2616
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2617
|
+
void applyLocalLogout().catch((err) => {
|
|
2618
|
+
console.error(
|
|
2619
|
+
"[sdk] applyLocalLogout failed (session invalidated):",
|
|
2620
|
+
err
|
|
2621
|
+
);
|
|
2622
|
+
});
|
|
2623
|
+
};
|
|
2506
2624
|
const onSdkUserRolesChanged = async (data) => {
|
|
2507
2625
|
const { success, message } = data || {};
|
|
2508
2626
|
if (!success || !message?.address) return;
|
|
@@ -2558,7 +2676,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2558
2676
|
}
|
|
2559
2677
|
setDid(_did);
|
|
2560
2678
|
setToken(_token);
|
|
2561
|
-
setApiKey($ws.get().apiKey);
|
|
2679
|
+
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
2562
2680
|
setIsServiceConnected(externalService.connected);
|
|
2563
2681
|
setServiceName(externalService.service);
|
|
2564
2682
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
@@ -2597,8 +2715,12 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2597
2715
|
wsc().on("signin:unsigned", onSigninUnsigned);
|
|
2598
2716
|
wsc().on("signin:onDeletion", onSigninOnDeletion);
|
|
2599
2717
|
wsc().on("sdk:sign:refresh", onSdkSignRefresh);
|
|
2718
|
+
wsc().on("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2600
2719
|
wsc().on("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2601
2720
|
wsc().on("sdk:restore", onSdkRestore);
|
|
2721
|
+
if (typeof window !== "undefined") {
|
|
2722
|
+
window.addEventListener("storage", onStorageLogout);
|
|
2723
|
+
}
|
|
2602
2724
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
2603
2725
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
2604
2726
|
setReady(true);
|
|
@@ -2634,9 +2756,13 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2634
2756
|
w.off("signin:unsigned", onSigninUnsigned);
|
|
2635
2757
|
w.off("signin:onDeletion", onSigninOnDeletion);
|
|
2636
2758
|
w.off("sdk:sign:refresh", onSdkSignRefresh);
|
|
2759
|
+
w.off("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2637
2760
|
w.off("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2638
2761
|
w.off("sdk:restore", onSdkRestore);
|
|
2639
2762
|
}
|
|
2763
|
+
if (typeof window !== "undefined") {
|
|
2764
|
+
window.removeEventListener("storage", onStorageLogout);
|
|
2765
|
+
}
|
|
2640
2766
|
provider.disconnect();
|
|
2641
2767
|
};
|
|
2642
2768
|
const logout = async () => {
|