@block-auth.io/core 0.14.0 → 0.15.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 +151 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +151 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2234,6 +2234,8 @@ function loginWithPopup(options) {
|
|
|
2234
2234
|
}
|
|
2235
2235
|
|
|
2236
2236
|
// src/auth/authEndpoint.ts
|
|
2237
|
+
var DEFAULT_AUTH_ENDPOINT = "/auth/blockauth";
|
|
2238
|
+
var BACKEND_ROUTES_GUIDE = "https://docs.block-auth.io/sdk-integration-spa-backend/#backend-routes";
|
|
2237
2239
|
var AccountLinkError = class extends Error {
|
|
2238
2240
|
constructor(reason, status) {
|
|
2239
2241
|
super(`authEndpoint /link failed (${status})`);
|
|
@@ -2256,18 +2258,72 @@ function toUser(payload) {
|
|
|
2256
2258
|
}
|
|
2257
2259
|
function createAuthEndpoint(baseUrl) {
|
|
2258
2260
|
const base = baseUrl.replace(/\/+$/, "");
|
|
2259
|
-
const
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2261
|
+
const hinted = /* @__PURE__ */ new Set();
|
|
2262
|
+
const hint = (key, message) => {
|
|
2263
|
+
if (hinted.has(key)) return;
|
|
2264
|
+
hinted.add(key);
|
|
2265
|
+
console.error(
|
|
2266
|
+
`[Block-Auth] ${message} authEndpoint is "${base}". Guide: ${BACKEND_ROUTES_GUIDE}`
|
|
2267
|
+
);
|
|
2268
|
+
};
|
|
2269
|
+
const request = async (path, init) => {
|
|
2270
|
+
try {
|
|
2271
|
+
return await fetch(`${base}${path}`, {
|
|
2272
|
+
...init,
|
|
2273
|
+
credentials: "include",
|
|
2274
|
+
cache: "no-store",
|
|
2275
|
+
redirect: "error"
|
|
2276
|
+
});
|
|
2277
|
+
} catch (error) {
|
|
2278
|
+
if (error instanceof TypeError) {
|
|
2279
|
+
hint(
|
|
2280
|
+
`network ${path}`,
|
|
2281
|
+
`${init.method} ${base}${path} did not reach your backend. If it runs on another origin, allow this origin in its CORS with credentials, or serve it under this origin. It must answer without redirects.`
|
|
2282
|
+
);
|
|
2283
|
+
}
|
|
2284
|
+
throw error;
|
|
2285
|
+
}
|
|
2286
|
+
};
|
|
2287
|
+
const readJson = async (path, response) => {
|
|
2288
|
+
try {
|
|
2289
|
+
return await response.json();
|
|
2290
|
+
} catch (error) {
|
|
2291
|
+
hint(
|
|
2292
|
+
`json ${path}`,
|
|
2293
|
+
`${base}${path} answered ${response.status} without JSON. The request is probably served by your frontend (a SPA fallback or a dev server without a proxy to your backend).`
|
|
2294
|
+
);
|
|
2295
|
+
throw error;
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2265
2298
|
const postJson = (path, body) => request(path, {
|
|
2266
2299
|
method: "POST",
|
|
2267
2300
|
headers: { "Content-Type": "application/json" },
|
|
2268
2301
|
body: JSON.stringify(body)
|
|
2269
2302
|
});
|
|
2303
|
+
const readUser = async (path, response) => {
|
|
2304
|
+
const payload = await readJson(path, response);
|
|
2305
|
+
try {
|
|
2306
|
+
return toUser(payload);
|
|
2307
|
+
} catch (error) {
|
|
2308
|
+
hint(
|
|
2309
|
+
`user ${path}`,
|
|
2310
|
+
`${base}${path} must answer { address, did, roles } from the user Block-Auth returned.`
|
|
2311
|
+
);
|
|
2312
|
+
throw error;
|
|
2313
|
+
}
|
|
2314
|
+
};
|
|
2270
2315
|
const fail = (path, response) => {
|
|
2316
|
+
if (response.status === 404 || response.status === 405) {
|
|
2317
|
+
hint(
|
|
2318
|
+
`missing ${path}`,
|
|
2319
|
+
`${base}${path} answered ${response.status}: add the ${path} route to your backend.`
|
|
2320
|
+
);
|
|
2321
|
+
} else if (path === "/code" && response.status >= 500) {
|
|
2322
|
+
hint(
|
|
2323
|
+
`redeem ${response.status}`,
|
|
2324
|
+
`${base}/code answered ${response.status}: your backend could not redeem the code. Check its logs and the apiKey, apiSecret hash and redirectUri it sends to /api/auth/token.`
|
|
2325
|
+
);
|
|
2326
|
+
}
|
|
2271
2327
|
throw new Error(`authEndpoint ${path} failed (${response.status})`);
|
|
2272
2328
|
};
|
|
2273
2329
|
return {
|
|
@@ -2278,13 +2334,13 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2278
2334
|
redirectUri
|
|
2279
2335
|
});
|
|
2280
2336
|
if (!response.ok) fail("/code", response);
|
|
2281
|
-
return
|
|
2337
|
+
return readUser("/code", response);
|
|
2282
2338
|
},
|
|
2283
2339
|
me: async () => {
|
|
2284
2340
|
const response = await request("/me", { method: "GET" });
|
|
2285
2341
|
if (response.status === 401) return null;
|
|
2286
2342
|
if (!response.ok) fail("/me", response);
|
|
2287
|
-
return
|
|
2343
|
+
return readUser("/me", response);
|
|
2288
2344
|
},
|
|
2289
2345
|
logout: async () => {
|
|
2290
2346
|
const response = await postJson("/logout", {});
|
|
@@ -2294,7 +2350,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2294
2350
|
const response = await request("/link", { method: "GET" });
|
|
2295
2351
|
if (response.status === 401) return null;
|
|
2296
2352
|
if (!response.ok) fail("/link", response);
|
|
2297
|
-
const { linked } = await response
|
|
2353
|
+
const { linked } = await readJson("/link", response);
|
|
2298
2354
|
return linked === true;
|
|
2299
2355
|
},
|
|
2300
2356
|
link: async ({ code, codeVerifier, redirectUri }) => {
|
|
@@ -2310,6 +2366,56 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2310
2366
|
};
|
|
2311
2367
|
}
|
|
2312
2368
|
|
|
2369
|
+
// src/session/sessionWatch.ts
|
|
2370
|
+
var MAX_TIMER_MS = 2147483647;
|
|
2371
|
+
function createSessionWatch({
|
|
2372
|
+
check,
|
|
2373
|
+
onEnded,
|
|
2374
|
+
intervalMs,
|
|
2375
|
+
minGapMs = 5e3
|
|
2376
|
+
}) {
|
|
2377
|
+
let active = false;
|
|
2378
|
+
let timer = null;
|
|
2379
|
+
let running = false;
|
|
2380
|
+
let lastRun = 0;
|
|
2381
|
+
const run = async () => {
|
|
2382
|
+
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2383
|
+
running = true;
|
|
2384
|
+
lastRun = Date.now();
|
|
2385
|
+
try {
|
|
2386
|
+
const alive = await check();
|
|
2387
|
+
if (active && alive === false) {
|
|
2388
|
+
stop();
|
|
2389
|
+
onEnded();
|
|
2390
|
+
}
|
|
2391
|
+
} finally {
|
|
2392
|
+
running = false;
|
|
2393
|
+
}
|
|
2394
|
+
};
|
|
2395
|
+
const onVisible = () => {
|
|
2396
|
+
if (document.visibilityState === "visible") void run();
|
|
2397
|
+
};
|
|
2398
|
+
const onFocus = () => void run();
|
|
2399
|
+
function start() {
|
|
2400
|
+
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2401
|
+
return;
|
|
2402
|
+
active = true;
|
|
2403
|
+
lastRun = Date.now();
|
|
2404
|
+
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2405
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
2406
|
+
window.addEventListener("focus", onFocus);
|
|
2407
|
+
}
|
|
2408
|
+
function stop() {
|
|
2409
|
+
if (!active) return;
|
|
2410
|
+
active = false;
|
|
2411
|
+
if (timer !== null) clearInterval(timer);
|
|
2412
|
+
timer = null;
|
|
2413
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
2414
|
+
window.removeEventListener("focus", onFocus);
|
|
2415
|
+
}
|
|
2416
|
+
return { start, stop };
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2313
2419
|
// src/auth/logout.ts
|
|
2314
2420
|
async function performLogout(args) {
|
|
2315
2421
|
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
@@ -2591,7 +2697,19 @@ function forgetHostedUser() {
|
|
|
2591
2697
|
}
|
|
2592
2698
|
function createBlockAuthClient(config3) {
|
|
2593
2699
|
if (config3.locale) setLocale(config3.locale);
|
|
2594
|
-
const
|
|
2700
|
+
const authEndpointUrl = config3.authEndpoint === false ? null : config3.authEndpoint || (config3.apiUrl ? null : DEFAULT_AUTH_ENDPOINT);
|
|
2701
|
+
const authEndpoint = authEndpointUrl ? createAuthEndpoint(authEndpointUrl) : null;
|
|
2702
|
+
const isDefaultAuthEndpoint = Boolean(authEndpoint) && !config3.authEndpoint;
|
|
2703
|
+
const usesAuthEndpoint = (ownsAuthCode = false) => Boolean(authEndpoint) && !(ownsAuthCode && isDefaultAuthEndpoint);
|
|
2704
|
+
let sessionOnAuthEndpoint = Boolean(authEndpoint);
|
|
2705
|
+
const sessionWatch = authEndpoint ? createSessionWatch({
|
|
2706
|
+
check: () => authEndpoint.me().then((user) => user !== null).catch(() => null),
|
|
2707
|
+
onEnded: () => {
|
|
2708
|
+
forgetHostedUser();
|
|
2709
|
+
config3.onSessionEnded?.();
|
|
2710
|
+
},
|
|
2711
|
+
intervalMs: (config3.sessionCheckSeconds ?? 60) * 1e3
|
|
2712
|
+
}) : null;
|
|
2595
2713
|
const provider = new BlockAuthProvider(
|
|
2596
2714
|
config3.apiKey,
|
|
2597
2715
|
config3.apiSecret,
|
|
@@ -2685,17 +2803,25 @@ function createBlockAuthClient(config3) {
|
|
|
2685
2803
|
authorizeWithPopup: () => authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl }),
|
|
2686
2804
|
// After the backend redeemed the code: shows the user in the page's UI
|
|
2687
2805
|
// (profile dropdown, roles). The backend's answer is the source of truth.
|
|
2688
|
-
rememberHostedLogin: (user) => {
|
|
2806
|
+
rememberHostedLogin: (user, options = {}) => {
|
|
2689
2807
|
showHostedUser(user);
|
|
2808
|
+
sessionOnAuthEndpoint = usesAuthEndpoint(options.ownsAuthCode);
|
|
2809
|
+
if (sessionOnAuthEndpoint) sessionWatch?.start();
|
|
2810
|
+
},
|
|
2811
|
+
forgetHostedLogin: () => {
|
|
2812
|
+
sessionWatch?.stop();
|
|
2813
|
+
forgetHostedUser();
|
|
2690
2814
|
},
|
|
2691
|
-
forgetHostedLogin: () => forgetHostedUser(),
|
|
2692
2815
|
hasAuthEndpoint: Boolean(authEndpoint),
|
|
2816
|
+
usesAuthEndpoint,
|
|
2693
2817
|
loginWithAuthEndpoint: async () => {
|
|
2694
2818
|
if (!authEndpoint) throw new Error("authEndpoint not configured");
|
|
2695
2819
|
const user = await authEndpoint.redeem(
|
|
2696
2820
|
await authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl })
|
|
2697
2821
|
);
|
|
2698
2822
|
showHostedUser(user);
|
|
2823
|
+
sessionOnAuthEndpoint = true;
|
|
2824
|
+
sessionWatch?.start();
|
|
2699
2825
|
return user;
|
|
2700
2826
|
},
|
|
2701
2827
|
getAccountLinkStatus: async () => {
|
|
@@ -2711,8 +2837,14 @@ function createBlockAuthClient(config3) {
|
|
|
2711
2837
|
restoreHostedLogin: async () => {
|
|
2712
2838
|
if (!authEndpoint) return null;
|
|
2713
2839
|
const user = await authEndpoint.me();
|
|
2714
|
-
if (user)
|
|
2715
|
-
|
|
2840
|
+
if (user) {
|
|
2841
|
+
showHostedUser(user);
|
|
2842
|
+
sessionOnAuthEndpoint = true;
|
|
2843
|
+
sessionWatch?.start();
|
|
2844
|
+
} else {
|
|
2845
|
+
sessionWatch?.stop();
|
|
2846
|
+
forgetHostedUser();
|
|
2847
|
+
}
|
|
2716
2848
|
return user;
|
|
2717
2849
|
},
|
|
2718
2850
|
// oauth
|
|
@@ -2792,7 +2924,9 @@ function createBlockAuthClient(config3) {
|
|
|
2792
2924
|
// restoration-expired modal.
|
|
2793
2925
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2794
2926
|
logout: async () => {
|
|
2795
|
-
if (authEndpoint && !config3.apiUrl
|
|
2927
|
+
if (authEndpoint && !config3.apiUrl && sessionOnAuthEndpoint)
|
|
2928
|
+
await authEndpoint.logout();
|
|
2929
|
+
sessionWatch?.stop();
|
|
2796
2930
|
await performLogout({
|
|
2797
2931
|
wsc: provider.wsc,
|
|
2798
2932
|
token: userToken(),
|
|
@@ -2928,7 +3062,7 @@ function getDefaultPhoneCountry(languages) {
|
|
|
2928
3062
|
}
|
|
2929
3063
|
return DEFAULT_PHONE_COUNTRY;
|
|
2930
3064
|
}
|
|
2931
|
-
function parsePhoneValue(value, fallbackCountry =
|
|
3065
|
+
function parsePhoneValue(value, fallbackCountry = DEFAULT_PHONE_COUNTRY) {
|
|
2932
3066
|
const raw = (value ?? "").trim();
|
|
2933
3067
|
if (!raw) return { country: fallbackCountry, nationalNumber: "" };
|
|
2934
3068
|
const legacy = raw.match(LEGACY_PHONE_PATTERN);
|
|
@@ -2992,7 +3126,7 @@ function updatePhoneDraft(country, nationalNumber) {
|
|
|
2992
3126
|
function resolvePhoneDraft(value, draft, fallbackCountry) {
|
|
2993
3127
|
const current = value ?? "";
|
|
2994
3128
|
if (draft && draft.value === current) return draft;
|
|
2995
|
-
const country = draft?.country ?? fallbackCountry ??
|
|
3129
|
+
const country = draft?.country ?? fallbackCountry ?? DEFAULT_PHONE_COUNTRY;
|
|
2996
3130
|
const parsed = parsePhoneValue(current, country);
|
|
2997
3131
|
const keepCountry = !!draft && min.getCountryCallingCode(draft.country) === min.getCountryCallingCode(parsed.country);
|
|
2998
3132
|
return {
|