@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.d.cts
CHANGED
|
@@ -775,6 +775,8 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
775
775
|
$openModals: nanostores.WritableAtom<ModalName[]>;
|
|
776
776
|
$user: nanostores.MapStore<UserState>;
|
|
777
777
|
};
|
|
778
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
779
|
+
apiKey: string;
|
|
778
780
|
connect: (waitMs?: number | undefined, url?: string | undefined, retries?: number | undefined) => Promise<void>;
|
|
779
781
|
disconnect: () => Promise<void>;
|
|
780
782
|
signin: () => Promise<any>;
|
|
@@ -1519,6 +1521,13 @@ declare class BrowserBase {
|
|
|
1519
1521
|
reset(target?: 'all' | 'ui' | 'session' | 'external', { keepExtensionInstalled }?: {
|
|
1520
1522
|
keepExtensionInstalled?: boolean | undefined;
|
|
1521
1523
|
}): void;
|
|
1524
|
+
/**
|
|
1525
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1526
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1527
|
+
* server scope a signout to just this browser instead of every device
|
|
1528
|
+
* signed in with the same account.
|
|
1529
|
+
*/
|
|
1530
|
+
getDeviceSessionId(): string;
|
|
1522
1531
|
/**
|
|
1523
1532
|
* Obtains the current UI configuration using localStorage as the
|
|
1524
1533
|
* source of truth, seeding the default if none is stored.
|
package/dist/index.d.ts
CHANGED
|
@@ -775,6 +775,8 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
775
775
|
$openModals: nanostores.WritableAtom<ModalName[]>;
|
|
776
776
|
$user: nanostores.MapStore<UserState>;
|
|
777
777
|
};
|
|
778
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
779
|
+
apiKey: string;
|
|
778
780
|
connect: (waitMs?: number | undefined, url?: string | undefined, retries?: number | undefined) => Promise<void>;
|
|
779
781
|
disconnect: () => Promise<void>;
|
|
780
782
|
signin: () => Promise<any>;
|
|
@@ -1519,6 +1521,13 @@ declare class BrowserBase {
|
|
|
1519
1521
|
reset(target?: 'all' | 'ui' | 'session' | 'external', { keepExtensionInstalled }?: {
|
|
1520
1522
|
keepExtensionInstalled?: boolean | undefined;
|
|
1521
1523
|
}): void;
|
|
1524
|
+
/**
|
|
1525
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1526
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1527
|
+
* server scope a signout to just this browser instead of every device
|
|
1528
|
+
* signed in with the same account.
|
|
1529
|
+
*/
|
|
1530
|
+
getDeviceSessionId(): string;
|
|
1522
1531
|
/**
|
|
1523
1532
|
* Obtains the current UI configuration using localStorage as the
|
|
1524
1533
|
* source of truth, seeding the default if none is stored.
|
package/dist/index.js
CHANGED
|
@@ -939,6 +939,7 @@ var resetWeb3 = () => $web3.set({ ...initial6 });
|
|
|
939
939
|
var UI = "blockauth-ui";
|
|
940
940
|
var SESSION = "blockauth-session";
|
|
941
941
|
var EXTERNAL_SERVICE = "blockauth-connected";
|
|
942
|
+
var DEVICE_SESSION_ID = "blockauth-device-session-id";
|
|
942
943
|
var BLOCKAUTH_STORAGE_KEYS = [
|
|
943
944
|
SESSION,
|
|
944
945
|
UI,
|
|
@@ -1014,6 +1015,21 @@ var BrowserBase = class {
|
|
|
1014
1015
|
this.getExternalService();
|
|
1015
1016
|
}
|
|
1016
1017
|
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Stable id for this browser, generated once and persisted in localStorage
|
|
1020
|
+
* (every tab of this browser shares it, and it survives logout). Lets the
|
|
1021
|
+
* server scope a signout to just this browser instead of every device
|
|
1022
|
+
* signed in with the same account.
|
|
1023
|
+
*/
|
|
1024
|
+
getDeviceSessionId() {
|
|
1025
|
+
const storage = getStorage();
|
|
1026
|
+
if (!storage) return "";
|
|
1027
|
+
const existing = storage.getItem(DEVICE_SESSION_ID);
|
|
1028
|
+
if (existing) return existing;
|
|
1029
|
+
const generated = typeof crypto !== "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
1030
|
+
storage.setItem(DEVICE_SESSION_ID, generated);
|
|
1031
|
+
return generated;
|
|
1032
|
+
}
|
|
1017
1033
|
/**
|
|
1018
1034
|
* Obtains the current UI configuration using localStorage as the
|
|
1019
1035
|
* source of truth, seeding the default if none is stored.
|
|
@@ -1329,13 +1345,14 @@ function rejectTxn(args) {
|
|
|
1329
1345
|
args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
|
|
1330
1346
|
}
|
|
1331
1347
|
function acceptTxn(args) {
|
|
1332
|
-
const { wsc, token, lifetime, providerSlug, userParams } = args;
|
|
1348
|
+
const { wsc, token, lifetime, providerSlug, userParams, sessionId } = args;
|
|
1333
1349
|
wsc.emit("provider:signin", {
|
|
1334
1350
|
token,
|
|
1335
1351
|
lifetime,
|
|
1336
1352
|
state: "approved",
|
|
1337
1353
|
providerSlug,
|
|
1338
|
-
userParams: userParams ?? {}
|
|
1354
|
+
userParams: userParams ?? {},
|
|
1355
|
+
sessionId
|
|
1339
1356
|
});
|
|
1340
1357
|
}
|
|
1341
1358
|
function reportTxn(args) {
|
|
@@ -1781,7 +1798,7 @@ function connectBlockAuth() {
|
|
|
1781
1798
|
|
|
1782
1799
|
// src/auth/logout.ts
|
|
1783
1800
|
async function performLogout(args) {
|
|
1784
|
-
const { wsc, token, apiKey } = args;
|
|
1801
|
+
const { wsc, token, apiKey, sessionId } = args;
|
|
1785
1802
|
await new Promise((resolve) => {
|
|
1786
1803
|
let done = false;
|
|
1787
1804
|
const finish = () => {
|
|
@@ -1791,7 +1808,7 @@ async function performLogout(args) {
|
|
|
1791
1808
|
}
|
|
1792
1809
|
};
|
|
1793
1810
|
wsc.ws?.once?.("signout", finish);
|
|
1794
|
-
wsc.emit("signout", { token, apiKey });
|
|
1811
|
+
wsc.emit("signout", { token, apiKey, sessionId });
|
|
1795
1812
|
setTimeout(finish, 1500);
|
|
1796
1813
|
});
|
|
1797
1814
|
if (typeof localStorage !== "undefined") {
|
|
@@ -2064,6 +2081,7 @@ function createBlockAuthClient(config3) {
|
|
|
2064
2081
|
}
|
|
2065
2082
|
if (config3.google) configureGoogle(config3.google);
|
|
2066
2083
|
if (config3.microsoft) configureMicrosoft(config3.microsoft);
|
|
2084
|
+
setApiKey(config3.apiKey);
|
|
2067
2085
|
function userToken() {
|
|
2068
2086
|
const s = browserBase.getSession();
|
|
2069
2087
|
const ext = browserBase.getExternalService();
|
|
@@ -2077,7 +2095,8 @@ function createBlockAuthClient(config3) {
|
|
|
2077
2095
|
$openModals,
|
|
2078
2096
|
$user
|
|
2079
2097
|
},
|
|
2080
|
-
|
|
2098
|
+
/** App apiKey from client config (also seeded into $ws). */
|
|
2099
|
+
apiKey: config3.apiKey,
|
|
2081
2100
|
connect: (...a) => provider.connect(...a),
|
|
2082
2101
|
disconnect: () => provider.disconnect(),
|
|
2083
2102
|
signin: () => provider.signin(),
|
|
@@ -2102,11 +2121,23 @@ function createBlockAuthClient(config3) {
|
|
|
2102
2121
|
signupGoogle: async (flow = "auth-code") => {
|
|
2103
2122
|
const res = await signInWithGoogle(flow);
|
|
2104
2123
|
const signature = res.code ?? res.accessToken;
|
|
2105
|
-
await emitOauthSignup({
|
|
2124
|
+
await emitOauthSignup({
|
|
2125
|
+
wsc: provider.wsc,
|
|
2126
|
+
providerSlug: "google",
|
|
2127
|
+
signature,
|
|
2128
|
+
mode: flow,
|
|
2129
|
+
getIp
|
|
2130
|
+
});
|
|
2106
2131
|
},
|
|
2107
2132
|
signupMicrosoft: async () => {
|
|
2108
2133
|
const res = await signInWithMicrosoft();
|
|
2109
|
-
await emitOauthSignup({
|
|
2134
|
+
await emitOauthSignup({
|
|
2135
|
+
wsc: provider.wsc,
|
|
2136
|
+
providerSlug: "microsoft",
|
|
2137
|
+
signature: res.accessToken,
|
|
2138
|
+
mode: "popup",
|
|
2139
|
+
getIp
|
|
2140
|
+
});
|
|
2110
2141
|
}
|
|
2111
2142
|
},
|
|
2112
2143
|
// signin transaction + logout
|
|
@@ -2117,18 +2148,29 @@ function createBlockAuthClient(config3) {
|
|
|
2117
2148
|
service: $ui.get().serviceName ?? "blockauth",
|
|
2118
2149
|
apiKey: config3.apiKey
|
|
2119
2150
|
}),
|
|
2120
|
-
cancelTxns: () => cancelTxns({
|
|
2151
|
+
cancelTxns: () => cancelTxns({
|
|
2152
|
+
wsc: provider.wsc,
|
|
2153
|
+
token: userToken()
|
|
2154
|
+
}),
|
|
2121
2155
|
// transaction accept/reject
|
|
2122
2156
|
txns: {
|
|
2123
|
-
reject: () => rejectTxn({
|
|
2157
|
+
reject: () => rejectTxn({
|
|
2158
|
+
wsc: provider.wsc,
|
|
2159
|
+
token: userToken()
|
|
2160
|
+
}),
|
|
2124
2161
|
accept: (opts) => acceptTxn({
|
|
2125
2162
|
wsc: provider.wsc,
|
|
2126
2163
|
token: userToken(),
|
|
2127
2164
|
lifetime: opts.lifetime,
|
|
2128
2165
|
providerSlug: $ui.get().serviceName ?? "blockauth",
|
|
2129
|
-
userParams: opts.userParams
|
|
2166
|
+
userParams: opts.userParams,
|
|
2167
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2130
2168
|
}),
|
|
2131
|
-
report: (slug) => reportTxn({
|
|
2169
|
+
report: (slug) => reportTxn({
|
|
2170
|
+
wsc: provider.wsc,
|
|
2171
|
+
token: userToken(),
|
|
2172
|
+
report: slug
|
|
2173
|
+
})
|
|
2132
2174
|
},
|
|
2133
2175
|
// account restore: resolves when the backend answers; expired
|
|
2134
2176
|
// restorations surface the restorationExpired modal, other failures
|
|
@@ -2137,7 +2179,12 @@ function createBlockAuthClient(config3) {
|
|
|
2137
2179
|
// full deletion-state teardown after the user confirms the
|
|
2138
2180
|
// restoration-expired modal.
|
|
2139
2181
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2140
|
-
logout: () => performLogout({
|
|
2182
|
+
logout: () => performLogout({
|
|
2183
|
+
wsc: provider.wsc,
|
|
2184
|
+
token: userToken(),
|
|
2185
|
+
apiKey: config3.apiKey,
|
|
2186
|
+
sessionId: browserBase.getDeviceSessionId()
|
|
2187
|
+
}),
|
|
2141
2188
|
// modals (stack — supports overlaying signin on top of signup)
|
|
2142
2189
|
openModal,
|
|
2143
2190
|
closeModal,
|
|
@@ -2158,6 +2205,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2158
2205
|
isUnsignTimeoutSet: false,
|
|
2159
2206
|
isSigned: false,
|
|
2160
2207
|
isSigning: false,
|
|
2208
|
+
isLoggingOut: false,
|
|
2161
2209
|
lastCheckedDid: null,
|
|
2162
2210
|
signinAttemptId: 0
|
|
2163
2211
|
};
|
|
@@ -2192,33 +2240,88 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2192
2240
|
await onError();
|
|
2193
2241
|
};
|
|
2194
2242
|
const _onLogout = async () => {
|
|
2243
|
+
if (hookState.isLoggingOut) return applyLocalLogout();
|
|
2244
|
+
hookState.isLoggingOut = true;
|
|
2195
2245
|
resetUser();
|
|
2246
|
+
if (typeof localStorage !== "undefined") {
|
|
2247
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2248
|
+
}
|
|
2196
2249
|
try {
|
|
2197
2250
|
if (provider && provider.wsc) {
|
|
2198
2251
|
const s = browserBase.getSession();
|
|
2199
2252
|
const ext = browserBase.getExternalService();
|
|
2200
2253
|
const _token = ext.service !== "blockauth" ? ext.token : s.token;
|
|
2201
|
-
const _apiKey = $ws.get().apiKey;
|
|
2254
|
+
const _apiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2255
|
+
const _sessionId = browserBase.getDeviceSessionId();
|
|
2202
2256
|
await new Promise((resolve) => {
|
|
2203
2257
|
const timeout = setTimeout(resolve, 3e3);
|
|
2204
2258
|
wsc().ws.once("signout", () => {
|
|
2205
2259
|
clearTimeout(timeout);
|
|
2206
2260
|
resolve();
|
|
2207
2261
|
});
|
|
2208
|
-
wsc().emit("signout", {
|
|
2262
|
+
wsc().emit("signout", {
|
|
2263
|
+
token: _token,
|
|
2264
|
+
apiKey: _apiKey,
|
|
2265
|
+
sessionId: _sessionId
|
|
2266
|
+
});
|
|
2209
2267
|
});
|
|
2210
2268
|
}
|
|
2211
2269
|
} catch {
|
|
2212
2270
|
}
|
|
2213
|
-
|
|
2214
|
-
|
|
2271
|
+
await applyLocalLogout();
|
|
2272
|
+
};
|
|
2273
|
+
let logoutInFlight = null;
|
|
2274
|
+
const applyLocalLogout = async () => {
|
|
2275
|
+
if (logoutInFlight) return logoutInFlight;
|
|
2276
|
+
logoutInFlight = (async () => {
|
|
2277
|
+
hookState.isLoggingOut = true;
|
|
2278
|
+
if (unsignTimeout) {
|
|
2279
|
+
clearTimeout(unsignTimeout);
|
|
2280
|
+
unsignTimeout = null;
|
|
2281
|
+
}
|
|
2282
|
+
hookState.isUnsignTimeoutSet = false;
|
|
2283
|
+
if (signinTimeout) {
|
|
2284
|
+
clearTimeout(signinTimeout);
|
|
2285
|
+
signinTimeout = null;
|
|
2286
|
+
}
|
|
2287
|
+
if (getUserTxnsInterval) {
|
|
2288
|
+
clearInterval(getUserTxnsInterval);
|
|
2289
|
+
getUserTxnsInterval = null;
|
|
2290
|
+
}
|
|
2291
|
+
hookState.isSigning = false;
|
|
2292
|
+
hookState.isSigned = false;
|
|
2293
|
+
setIsSigned(false);
|
|
2294
|
+
setIsSigning(false);
|
|
2295
|
+
setIsSigningLoader(false);
|
|
2296
|
+
setIsAcceptingTxn(false);
|
|
2297
|
+
setIsSignedFirstCheck(true);
|
|
2298
|
+
resetUser();
|
|
2299
|
+
if (typeof localStorage !== "undefined") {
|
|
2300
|
+
localStorage.setItem("blockauth-logged-out", "true");
|
|
2301
|
+
}
|
|
2302
|
+
const serviceName = browserBase.getExternalService().service;
|
|
2303
|
+
setIsServiceConnected(false);
|
|
2304
|
+
setWaitingForSuccessLogInSince(0);
|
|
2305
|
+
setServiceIcon(imgUrlByService(serviceName));
|
|
2306
|
+
setServiceName(serviceName);
|
|
2307
|
+
try {
|
|
2308
|
+
await onLogout();
|
|
2309
|
+
} finally {
|
|
2310
|
+
hookState.isLoggingOut = false;
|
|
2311
|
+
}
|
|
2312
|
+
})().finally(() => {
|
|
2313
|
+
logoutInFlight = null;
|
|
2314
|
+
});
|
|
2315
|
+
return logoutInFlight;
|
|
2316
|
+
};
|
|
2317
|
+
const onStorageLogout = (event) => {
|
|
2318
|
+
if (event.key !== "blockauth-logged-out" || event.newValue !== "true") {
|
|
2319
|
+
return;
|
|
2215
2320
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
setServiceName(serviceName);
|
|
2221
|
-
await onLogout();
|
|
2321
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2322
|
+
void applyLocalLogout().catch((err) => {
|
|
2323
|
+
console.error("[sdk] applyLocalLogout failed (storage event):", err);
|
|
2324
|
+
});
|
|
2222
2325
|
};
|
|
2223
2326
|
const deletion = createDeletionService({ provider });
|
|
2224
2327
|
const handleUserDeleted = (forAddress = null) => {
|
|
@@ -2495,6 +2598,21 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2495
2598
|
const onSdkSignRefresh = () => {
|
|
2496
2599
|
void isUserSignedIn();
|
|
2497
2600
|
};
|
|
2601
|
+
const onSdkSessionInvalidated = (data) => {
|
|
2602
|
+
const apiKey = data?.apiKey ?? data?.message?.apiKey;
|
|
2603
|
+
const clientApiKey = $ws.get().apiKey ?? client.apiKey;
|
|
2604
|
+
if (!apiKey || !clientApiKey || apiKey !== clientApiKey) return;
|
|
2605
|
+
const sessionId = data?.sessionId ?? data?.message?.sessionId;
|
|
2606
|
+
if (sessionId != null && String(sessionId) !== String(browserBase.getDeviceSessionId()))
|
|
2607
|
+
return;
|
|
2608
|
+
if (hookState.isLoggingOut || !hookState.isSigned) return;
|
|
2609
|
+
void applyLocalLogout().catch((err) => {
|
|
2610
|
+
console.error(
|
|
2611
|
+
"[sdk] applyLocalLogout failed (session invalidated):",
|
|
2612
|
+
err
|
|
2613
|
+
);
|
|
2614
|
+
});
|
|
2615
|
+
};
|
|
2498
2616
|
const onSdkUserRolesChanged = async (data) => {
|
|
2499
2617
|
const { success, message } = data || {};
|
|
2500
2618
|
if (!success || !message?.address) return;
|
|
@@ -2550,7 +2668,7 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2550
2668
|
}
|
|
2551
2669
|
setDid(_did);
|
|
2552
2670
|
setToken(_token);
|
|
2553
|
-
setApiKey($ws.get().apiKey);
|
|
2671
|
+
setApiKey(client.apiKey ?? $ws.get().apiKey);
|
|
2554
2672
|
setIsServiceConnected(externalService.connected);
|
|
2555
2673
|
setServiceName(externalService.service);
|
|
2556
2674
|
setServiceIcon(imgUrlByService(externalService.service));
|
|
@@ -2589,8 +2707,12 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2589
2707
|
wsc().on("signin:unsigned", onSigninUnsigned);
|
|
2590
2708
|
wsc().on("signin:onDeletion", onSigninOnDeletion);
|
|
2591
2709
|
wsc().on("sdk:sign:refresh", onSdkSignRefresh);
|
|
2710
|
+
wsc().on("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2592
2711
|
wsc().on("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2593
2712
|
wsc().on("sdk:restore", onSdkRestore);
|
|
2713
|
+
if (typeof window !== "undefined") {
|
|
2714
|
+
window.addEventListener("storage", onStorageLogout);
|
|
2715
|
+
}
|
|
2594
2716
|
const { domain, signinCallbackUrl, config: config3 } = await provider.app();
|
|
2595
2717
|
setUiConfig({ domain, signinCallbackUrl, config: config3 });
|
|
2596
2718
|
setReady(true);
|
|
@@ -2626,9 +2748,13 @@ function createAuthRuntime(client, callbacks = {}) {
|
|
|
2626
2748
|
w.off("signin:unsigned", onSigninUnsigned);
|
|
2627
2749
|
w.off("signin:onDeletion", onSigninOnDeletion);
|
|
2628
2750
|
w.off("sdk:sign:refresh", onSdkSignRefresh);
|
|
2751
|
+
w.off("sdk:session:invalidated", onSdkSessionInvalidated);
|
|
2629
2752
|
w.off("sdk:user:roles:changed", onSdkUserRolesChanged);
|
|
2630
2753
|
w.off("sdk:restore", onSdkRestore);
|
|
2631
2754
|
}
|
|
2755
|
+
if (typeof window !== "undefined") {
|
|
2756
|
+
window.removeEventListener("storage", onStorageLogout);
|
|
2757
|
+
}
|
|
2632
2758
|
provider.disconnect();
|
|
2633
2759
|
};
|
|
2634
2760
|
const logout = async () => {
|