@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.d.cts
CHANGED
|
@@ -852,7 +852,9 @@ interface BlockAuthClientConfig {
|
|
|
852
852
|
* deja apiSecret vacío para que el secreto nunca llegue al cliente. */
|
|
853
853
|
browserToken?: string;
|
|
854
854
|
wsUrl?: string;
|
|
855
|
-
authEndpoint?: string;
|
|
855
|
+
authEndpoint?: string | false;
|
|
856
|
+
sessionCheckSeconds?: number;
|
|
857
|
+
onSessionEnded?: () => void;
|
|
856
858
|
/** blockauth-api base url, first-party apps only (its CORS admits no other
|
|
857
859
|
* origin). With it, the session lives in the httpOnly cookie
|
|
858
860
|
* (POST /auth/session, POST /auth/logout, GET /auth/ws-ticket). Without
|
|
@@ -1221,9 +1223,12 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
1221
1223
|
connectBlockAuth: () => void;
|
|
1222
1224
|
loginWithPopup: () => Promise<PopupLoginResult>;
|
|
1223
1225
|
authorizeWithPopup: () => Promise<PopupAuthCode>;
|
|
1224
|
-
rememberHostedLogin: (user: HostedLoginUser
|
|
1226
|
+
rememberHostedLogin: (user: HostedLoginUser, options?: {
|
|
1227
|
+
ownsAuthCode?: boolean;
|
|
1228
|
+
}) => void;
|
|
1225
1229
|
forgetHostedLogin: () => void;
|
|
1226
1230
|
hasAuthEndpoint: boolean;
|
|
1231
|
+
usesAuthEndpoint: (ownsAuthCode?: boolean) => boolean;
|
|
1227
1232
|
loginWithAuthEndpoint: () => Promise<HostedLoginUser>;
|
|
1228
1233
|
getAccountLinkStatus: () => Promise<boolean | null>;
|
|
1229
1234
|
linkWithAuthEndpoint: () => Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -852,7 +852,9 @@ interface BlockAuthClientConfig {
|
|
|
852
852
|
* deja apiSecret vacío para que el secreto nunca llegue al cliente. */
|
|
853
853
|
browserToken?: string;
|
|
854
854
|
wsUrl?: string;
|
|
855
|
-
authEndpoint?: string;
|
|
855
|
+
authEndpoint?: string | false;
|
|
856
|
+
sessionCheckSeconds?: number;
|
|
857
|
+
onSessionEnded?: () => void;
|
|
856
858
|
/** blockauth-api base url, first-party apps only (its CORS admits no other
|
|
857
859
|
* origin). With it, the session lives in the httpOnly cookie
|
|
858
860
|
* (POST /auth/session, POST /auth/logout, GET /auth/ws-ticket). Without
|
|
@@ -1221,9 +1223,12 @@ declare function createBlockAuthClient(config: BlockAuthClientConfig): {
|
|
|
1221
1223
|
connectBlockAuth: () => void;
|
|
1222
1224
|
loginWithPopup: () => Promise<PopupLoginResult>;
|
|
1223
1225
|
authorizeWithPopup: () => Promise<PopupAuthCode>;
|
|
1224
|
-
rememberHostedLogin: (user: HostedLoginUser
|
|
1226
|
+
rememberHostedLogin: (user: HostedLoginUser, options?: {
|
|
1227
|
+
ownsAuthCode?: boolean;
|
|
1228
|
+
}) => void;
|
|
1225
1229
|
forgetHostedLogin: () => void;
|
|
1226
1230
|
hasAuthEndpoint: boolean;
|
|
1231
|
+
usesAuthEndpoint: (ownsAuthCode?: boolean) => boolean;
|
|
1227
1232
|
loginWithAuthEndpoint: () => Promise<HostedLoginUser>;
|
|
1228
1233
|
getAccountLinkStatus: () => Promise<boolean | null>;
|
|
1229
1234
|
linkWithAuthEndpoint: () => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -2226,6 +2226,8 @@ function loginWithPopup(options) {
|
|
|
2226
2226
|
}
|
|
2227
2227
|
|
|
2228
2228
|
// src/auth/authEndpoint.ts
|
|
2229
|
+
var DEFAULT_AUTH_ENDPOINT = "/auth/blockauth";
|
|
2230
|
+
var BACKEND_ROUTES_GUIDE = "https://docs.block-auth.io/sdk-integration-spa-backend/#backend-routes";
|
|
2229
2231
|
var AccountLinkError = class extends Error {
|
|
2230
2232
|
constructor(reason, status) {
|
|
2231
2233
|
super(`authEndpoint /link failed (${status})`);
|
|
@@ -2248,18 +2250,72 @@ function toUser(payload) {
|
|
|
2248
2250
|
}
|
|
2249
2251
|
function createAuthEndpoint(baseUrl) {
|
|
2250
2252
|
const base = baseUrl.replace(/\/+$/, "");
|
|
2251
|
-
const
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2253
|
+
const hinted = /* @__PURE__ */ new Set();
|
|
2254
|
+
const hint = (key, message) => {
|
|
2255
|
+
if (hinted.has(key)) return;
|
|
2256
|
+
hinted.add(key);
|
|
2257
|
+
console.error(
|
|
2258
|
+
`[Block-Auth] ${message} authEndpoint is "${base}". Guide: ${BACKEND_ROUTES_GUIDE}`
|
|
2259
|
+
);
|
|
2260
|
+
};
|
|
2261
|
+
const request = async (path, init) => {
|
|
2262
|
+
try {
|
|
2263
|
+
return await fetch(`${base}${path}`, {
|
|
2264
|
+
...init,
|
|
2265
|
+
credentials: "include",
|
|
2266
|
+
cache: "no-store",
|
|
2267
|
+
redirect: "error"
|
|
2268
|
+
});
|
|
2269
|
+
} catch (error) {
|
|
2270
|
+
if (error instanceof TypeError) {
|
|
2271
|
+
hint(
|
|
2272
|
+
`network ${path}`,
|
|
2273
|
+
`${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.`
|
|
2274
|
+
);
|
|
2275
|
+
}
|
|
2276
|
+
throw error;
|
|
2277
|
+
}
|
|
2278
|
+
};
|
|
2279
|
+
const readJson = async (path, response) => {
|
|
2280
|
+
try {
|
|
2281
|
+
return await response.json();
|
|
2282
|
+
} catch (error) {
|
|
2283
|
+
hint(
|
|
2284
|
+
`json ${path}`,
|
|
2285
|
+
`${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).`
|
|
2286
|
+
);
|
|
2287
|
+
throw error;
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2257
2290
|
const postJson = (path, body) => request(path, {
|
|
2258
2291
|
method: "POST",
|
|
2259
2292
|
headers: { "Content-Type": "application/json" },
|
|
2260
2293
|
body: JSON.stringify(body)
|
|
2261
2294
|
});
|
|
2295
|
+
const readUser = async (path, response) => {
|
|
2296
|
+
const payload = await readJson(path, response);
|
|
2297
|
+
try {
|
|
2298
|
+
return toUser(payload);
|
|
2299
|
+
} catch (error) {
|
|
2300
|
+
hint(
|
|
2301
|
+
`user ${path}`,
|
|
2302
|
+
`${base}${path} must answer { address, did, roles } from the user Block-Auth returned.`
|
|
2303
|
+
);
|
|
2304
|
+
throw error;
|
|
2305
|
+
}
|
|
2306
|
+
};
|
|
2262
2307
|
const fail = (path, response) => {
|
|
2308
|
+
if (response.status === 404 || response.status === 405) {
|
|
2309
|
+
hint(
|
|
2310
|
+
`missing ${path}`,
|
|
2311
|
+
`${base}${path} answered ${response.status}: add the ${path} route to your backend.`
|
|
2312
|
+
);
|
|
2313
|
+
} else if (path === "/code" && response.status >= 500) {
|
|
2314
|
+
hint(
|
|
2315
|
+
`redeem ${response.status}`,
|
|
2316
|
+
`${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.`
|
|
2317
|
+
);
|
|
2318
|
+
}
|
|
2263
2319
|
throw new Error(`authEndpoint ${path} failed (${response.status})`);
|
|
2264
2320
|
};
|
|
2265
2321
|
return {
|
|
@@ -2270,13 +2326,13 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2270
2326
|
redirectUri
|
|
2271
2327
|
});
|
|
2272
2328
|
if (!response.ok) fail("/code", response);
|
|
2273
|
-
return
|
|
2329
|
+
return readUser("/code", response);
|
|
2274
2330
|
},
|
|
2275
2331
|
me: async () => {
|
|
2276
2332
|
const response = await request("/me", { method: "GET" });
|
|
2277
2333
|
if (response.status === 401) return null;
|
|
2278
2334
|
if (!response.ok) fail("/me", response);
|
|
2279
|
-
return
|
|
2335
|
+
return readUser("/me", response);
|
|
2280
2336
|
},
|
|
2281
2337
|
logout: async () => {
|
|
2282
2338
|
const response = await postJson("/logout", {});
|
|
@@ -2286,7 +2342,7 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2286
2342
|
const response = await request("/link", { method: "GET" });
|
|
2287
2343
|
if (response.status === 401) return null;
|
|
2288
2344
|
if (!response.ok) fail("/link", response);
|
|
2289
|
-
const { linked } = await response
|
|
2345
|
+
const { linked } = await readJson("/link", response);
|
|
2290
2346
|
return linked === true;
|
|
2291
2347
|
},
|
|
2292
2348
|
link: async ({ code, codeVerifier, redirectUri }) => {
|
|
@@ -2302,6 +2358,56 @@ function createAuthEndpoint(baseUrl) {
|
|
|
2302
2358
|
};
|
|
2303
2359
|
}
|
|
2304
2360
|
|
|
2361
|
+
// src/session/sessionWatch.ts
|
|
2362
|
+
var MAX_TIMER_MS = 2147483647;
|
|
2363
|
+
function createSessionWatch({
|
|
2364
|
+
check,
|
|
2365
|
+
onEnded,
|
|
2366
|
+
intervalMs,
|
|
2367
|
+
minGapMs = 5e3
|
|
2368
|
+
}) {
|
|
2369
|
+
let active = false;
|
|
2370
|
+
let timer = null;
|
|
2371
|
+
let running = false;
|
|
2372
|
+
let lastRun = 0;
|
|
2373
|
+
const run = async () => {
|
|
2374
|
+
if (!active || running || Date.now() - lastRun < minGapMs) return;
|
|
2375
|
+
running = true;
|
|
2376
|
+
lastRun = Date.now();
|
|
2377
|
+
try {
|
|
2378
|
+
const alive = await check();
|
|
2379
|
+
if (active && alive === false) {
|
|
2380
|
+
stop();
|
|
2381
|
+
onEnded();
|
|
2382
|
+
}
|
|
2383
|
+
} finally {
|
|
2384
|
+
running = false;
|
|
2385
|
+
}
|
|
2386
|
+
};
|
|
2387
|
+
const onVisible = () => {
|
|
2388
|
+
if (document.visibilityState === "visible") void run();
|
|
2389
|
+
};
|
|
2390
|
+
const onFocus = () => void run();
|
|
2391
|
+
function start() {
|
|
2392
|
+
if (active || !Number.isFinite(intervalMs) || intervalMs <= 0 || typeof window === "undefined")
|
|
2393
|
+
return;
|
|
2394
|
+
active = true;
|
|
2395
|
+
lastRun = Date.now();
|
|
2396
|
+
timer = setInterval(() => void run(), Math.min(intervalMs, MAX_TIMER_MS));
|
|
2397
|
+
document.addEventListener("visibilitychange", onVisible);
|
|
2398
|
+
window.addEventListener("focus", onFocus);
|
|
2399
|
+
}
|
|
2400
|
+
function stop() {
|
|
2401
|
+
if (!active) return;
|
|
2402
|
+
active = false;
|
|
2403
|
+
if (timer !== null) clearInterval(timer);
|
|
2404
|
+
timer = null;
|
|
2405
|
+
document.removeEventListener("visibilitychange", onVisible);
|
|
2406
|
+
window.removeEventListener("focus", onFocus);
|
|
2407
|
+
}
|
|
2408
|
+
return { start, stop };
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2305
2411
|
// src/auth/logout.ts
|
|
2306
2412
|
async function performLogout(args) {
|
|
2307
2413
|
const { wsc, token, apiKey, sessionId, apiUrl } = args;
|
|
@@ -2583,7 +2689,19 @@ function forgetHostedUser() {
|
|
|
2583
2689
|
}
|
|
2584
2690
|
function createBlockAuthClient(config3) {
|
|
2585
2691
|
if (config3.locale) setLocale(config3.locale);
|
|
2586
|
-
const
|
|
2692
|
+
const authEndpointUrl = config3.authEndpoint === false ? null : config3.authEndpoint || (config3.apiUrl ? null : DEFAULT_AUTH_ENDPOINT);
|
|
2693
|
+
const authEndpoint = authEndpointUrl ? createAuthEndpoint(authEndpointUrl) : null;
|
|
2694
|
+
const isDefaultAuthEndpoint = Boolean(authEndpoint) && !config3.authEndpoint;
|
|
2695
|
+
const usesAuthEndpoint = (ownsAuthCode = false) => Boolean(authEndpoint) && !(ownsAuthCode && isDefaultAuthEndpoint);
|
|
2696
|
+
let sessionOnAuthEndpoint = Boolean(authEndpoint);
|
|
2697
|
+
const sessionWatch = authEndpoint ? createSessionWatch({
|
|
2698
|
+
check: () => authEndpoint.me().then((user) => user !== null).catch(() => null),
|
|
2699
|
+
onEnded: () => {
|
|
2700
|
+
forgetHostedUser();
|
|
2701
|
+
config3.onSessionEnded?.();
|
|
2702
|
+
},
|
|
2703
|
+
intervalMs: (config3.sessionCheckSeconds ?? 60) * 1e3
|
|
2704
|
+
}) : null;
|
|
2587
2705
|
const provider = new BlockAuthProvider(
|
|
2588
2706
|
config3.apiKey,
|
|
2589
2707
|
config3.apiSecret,
|
|
@@ -2677,17 +2795,25 @@ function createBlockAuthClient(config3) {
|
|
|
2677
2795
|
authorizeWithPopup: () => authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl }),
|
|
2678
2796
|
// After the backend redeemed the code: shows the user in the page's UI
|
|
2679
2797
|
// (profile dropdown, roles). The backend's answer is the source of truth.
|
|
2680
|
-
rememberHostedLogin: (user) => {
|
|
2798
|
+
rememberHostedLogin: (user, options = {}) => {
|
|
2681
2799
|
showHostedUser(user);
|
|
2800
|
+
sessionOnAuthEndpoint = usesAuthEndpoint(options.ownsAuthCode);
|
|
2801
|
+
if (sessionOnAuthEndpoint) sessionWatch?.start();
|
|
2802
|
+
},
|
|
2803
|
+
forgetHostedLogin: () => {
|
|
2804
|
+
sessionWatch?.stop();
|
|
2805
|
+
forgetHostedUser();
|
|
2682
2806
|
},
|
|
2683
|
-
forgetHostedLogin: () => forgetHostedUser(),
|
|
2684
2807
|
hasAuthEndpoint: Boolean(authEndpoint),
|
|
2808
|
+
usesAuthEndpoint,
|
|
2685
2809
|
loginWithAuthEndpoint: async () => {
|
|
2686
2810
|
if (!authEndpoint) throw new Error("authEndpoint not configured");
|
|
2687
2811
|
const user = await authEndpoint.redeem(
|
|
2688
2812
|
await authorizeWithPopup({ apiKey: config3.apiKey, wsUrl: config3.wsUrl })
|
|
2689
2813
|
);
|
|
2690
2814
|
showHostedUser(user);
|
|
2815
|
+
sessionOnAuthEndpoint = true;
|
|
2816
|
+
sessionWatch?.start();
|
|
2691
2817
|
return user;
|
|
2692
2818
|
},
|
|
2693
2819
|
getAccountLinkStatus: async () => {
|
|
@@ -2703,8 +2829,14 @@ function createBlockAuthClient(config3) {
|
|
|
2703
2829
|
restoreHostedLogin: async () => {
|
|
2704
2830
|
if (!authEndpoint) return null;
|
|
2705
2831
|
const user = await authEndpoint.me();
|
|
2706
|
-
if (user)
|
|
2707
|
-
|
|
2832
|
+
if (user) {
|
|
2833
|
+
showHostedUser(user);
|
|
2834
|
+
sessionOnAuthEndpoint = true;
|
|
2835
|
+
sessionWatch?.start();
|
|
2836
|
+
} else {
|
|
2837
|
+
sessionWatch?.stop();
|
|
2838
|
+
forgetHostedUser();
|
|
2839
|
+
}
|
|
2708
2840
|
return user;
|
|
2709
2841
|
},
|
|
2710
2842
|
// oauth
|
|
@@ -2784,7 +2916,9 @@ function createBlockAuthClient(config3) {
|
|
|
2784
2916
|
// restoration-expired modal.
|
|
2785
2917
|
finalizeRestorationExpired: () => finalizeAfterRestorationExpired(),
|
|
2786
2918
|
logout: async () => {
|
|
2787
|
-
if (authEndpoint && !config3.apiUrl
|
|
2919
|
+
if (authEndpoint && !config3.apiUrl && sessionOnAuthEndpoint)
|
|
2920
|
+
await authEndpoint.logout();
|
|
2921
|
+
sessionWatch?.stop();
|
|
2788
2922
|
await performLogout({
|
|
2789
2923
|
wsc: provider.wsc,
|
|
2790
2924
|
token: userToken(),
|
|
@@ -2920,7 +3054,7 @@ function getDefaultPhoneCountry(languages) {
|
|
|
2920
3054
|
}
|
|
2921
3055
|
return DEFAULT_PHONE_COUNTRY;
|
|
2922
3056
|
}
|
|
2923
|
-
function parsePhoneValue(value, fallbackCountry =
|
|
3057
|
+
function parsePhoneValue(value, fallbackCountry = DEFAULT_PHONE_COUNTRY) {
|
|
2924
3058
|
const raw = (value ?? "").trim();
|
|
2925
3059
|
if (!raw) return { country: fallbackCountry, nationalNumber: "" };
|
|
2926
3060
|
const legacy = raw.match(LEGACY_PHONE_PATTERN);
|
|
@@ -2984,7 +3118,7 @@ function updatePhoneDraft(country, nationalNumber) {
|
|
|
2984
3118
|
function resolvePhoneDraft(value, draft, fallbackCountry) {
|
|
2985
3119
|
const current = value ?? "";
|
|
2986
3120
|
if (draft && draft.value === current) return draft;
|
|
2987
|
-
const country = draft?.country ?? fallbackCountry ??
|
|
3121
|
+
const country = draft?.country ?? fallbackCountry ?? DEFAULT_PHONE_COUNTRY;
|
|
2988
3122
|
const parsed = parsePhoneValue(current, country);
|
|
2989
3123
|
const keepCountry = !!draft && getCountryCallingCode(draft.country) === getCountryCallingCode(parsed.country);
|
|
2990
3124
|
return {
|