@djangocfg/api 2.1.619 → 2.1.622
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/auth.cjs +441 -106
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts +80 -2
- package/dist/auth.d.ts +80 -2
- package/dist/auth.mjs +396 -63
- package/dist/auth.mjs.map +1 -1
- package/dist/clients.cjs +43 -6
- package/dist/clients.cjs.map +1 -1
- package/dist/clients.mjs +43 -6
- package/dist/clients.mjs.map +1 -1
- package/dist/hooks.cjs +43 -6
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.mjs +43 -6
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.cjs +264 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +264 -40
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/_api/generated/helpers/auth.ts +33 -6
- package/src/auth/__tests__/authState.test.ts +46 -0
- package/src/auth/__tests__/formatAuthError.test.ts +7 -0
- package/src/auth/__tests__/sharedContext.test.ts +16 -0
- package/src/auth/__tests__/trace.test.ts +38 -0
- package/src/auth/context/AccountsContext.tsx +6 -2
- package/src/auth/context/AuthContext.tsx +63 -10
- package/src/auth/context/sharedContext.ts +16 -0
- package/src/auth/context/types.ts +2 -0
- package/src/auth/hooks/useCfgRouter.ts +31 -0
- package/src/auth/hooks/useOAuthLogin.ts +3 -0
- package/src/auth/hooks/useTwoFactorVerify.ts +2 -0
- package/src/auth/index.ts +3 -0
- package/src/auth/trace.ts +253 -0
- package/src/auth/utils/authState.ts +39 -0
- package/src/auth/utils/errors.ts +5 -2
- package/src/auth/utils/index.ts +1 -0
- package/src/index.ts +3 -0
package/dist/hooks.cjs
CHANGED
|
@@ -1287,6 +1287,29 @@ var REFRESH_KEY = "cfg.auth.refresh_token";
|
|
|
1287
1287
|
var API_KEY_KEY = "cfg.auth.api_key";
|
|
1288
1288
|
var storage = true ? cfgStorage : createStorage2("cfg");
|
|
1289
1289
|
var isBrowser = typeof window !== "undefined";
|
|
1290
|
+
function trace(ev, fields) {
|
|
1291
|
+
try {
|
|
1292
|
+
const g = globalThis;
|
|
1293
|
+
const sink = g.__cfgAuthTrace;
|
|
1294
|
+
if (sink) {
|
|
1295
|
+
sink(ev, fields);
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
const queue = g.__cfgAuthTraceQ ?? [];
|
|
1299
|
+
g.__cfgAuthTraceQ = queue;
|
|
1300
|
+
if (queue.length < 100) queue.push([Date.now(), ev, fields]);
|
|
1301
|
+
} catch {
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
__name(trace, "trace");
|
|
1305
|
+
function pathOf(url) {
|
|
1306
|
+
try {
|
|
1307
|
+
return new URL(url, "http://x").pathname;
|
|
1308
|
+
} catch {
|
|
1309
|
+
return "";
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
__name(pathOf, "pathOf");
|
|
1290
1313
|
function secondsUntilExp(token) {
|
|
1291
1314
|
const exp = jwtExpMs(token);
|
|
1292
1315
|
return exp === null ? void 0 : Math.max(1, Math.floor((exp - Date.now()) / 1e3));
|
|
@@ -1446,7 +1469,10 @@ __name(scheduleExpiryFlip, "scheduleExpiryFlip");
|
|
|
1446
1469
|
function notifySessionChanged() {
|
|
1447
1470
|
const next = computeSnapshot();
|
|
1448
1471
|
const changed = next.status !== _snapshot.status || next.accessExpiresAt !== _snapshot.accessExpiresAt;
|
|
1449
|
-
if (changed)
|
|
1472
|
+
if (changed) {
|
|
1473
|
+
_snapshot = next;
|
|
1474
|
+
trace("store.snapshot", { status: next.status });
|
|
1475
|
+
}
|
|
1450
1476
|
scheduleExpiryFlip();
|
|
1451
1477
|
if (!changed) return;
|
|
1452
1478
|
for (const listener of Array.from(_sessionListeners)) {
|
|
@@ -1529,6 +1555,7 @@ var auth = {
|
|
|
1529
1555
|
return _storage.get(ACCESS_KEY);
|
|
1530
1556
|
},
|
|
1531
1557
|
setToken(token) {
|
|
1558
|
+
trace("store.setToken", { present: token !== null });
|
|
1532
1559
|
_storage.set(ACCESS_KEY, token);
|
|
1533
1560
|
notifySessionChanged();
|
|
1534
1561
|
},
|
|
@@ -1536,10 +1563,12 @@ var auth = {
|
|
|
1536
1563
|
return _storage.get(REFRESH_KEY);
|
|
1537
1564
|
},
|
|
1538
1565
|
setRefreshToken(token) {
|
|
1566
|
+
trace("store.setRefreshToken", { present: token !== null });
|
|
1539
1567
|
_storage.set(REFRESH_KEY, token);
|
|
1540
1568
|
notifySessionChanged();
|
|
1541
1569
|
},
|
|
1542
1570
|
clearTokens() {
|
|
1571
|
+
trace("store.clearTokens");
|
|
1543
1572
|
for (const mode of ["localStorage", "sessionStorage", "cookie"]) {
|
|
1544
1573
|
const backend = backendFor(mode);
|
|
1545
1574
|
backend.set(ACCESS_KEY, null);
|
|
@@ -1562,6 +1591,7 @@ var auth = {
|
|
|
1562
1591
|
* rotation); `refresh: null` explicitly drops it.
|
|
1563
1592
|
*/
|
|
1564
1593
|
setSession(tokens) {
|
|
1594
|
+
trace("store.setSession", { refresh: tokens.refresh === void 0 ? "kept" : tokens.refresh === null ? "dropped" : "set" });
|
|
1565
1595
|
_storage.set(ACCESS_KEY, tokens.access);
|
|
1566
1596
|
if (tokens.refresh !== void 0) _storage.set(REFRESH_KEY, tokens.refresh);
|
|
1567
1597
|
notifySessionChanged();
|
|
@@ -1700,7 +1730,11 @@ async function tryRefresh() {
|
|
|
1700
1730
|
const refresh = auth.getRefreshToken();
|
|
1701
1731
|
if (!refresh) return null;
|
|
1702
1732
|
const result = await _refreshHandler(refresh);
|
|
1703
|
-
if (!result?.access)
|
|
1733
|
+
if (!result?.access) {
|
|
1734
|
+
trace("refresh", { ok: false, why: "no-access" });
|
|
1735
|
+
return null;
|
|
1736
|
+
}
|
|
1737
|
+
trace("refresh", { ok: true, rotated: Boolean(result.refresh) });
|
|
1704
1738
|
auth.setToken(result.access);
|
|
1705
1739
|
if (result.refresh) auth.setRefreshToken(result.refresh);
|
|
1706
1740
|
return result.access;
|
|
@@ -1713,6 +1747,7 @@ async function tryRefresh() {
|
|
|
1713
1747
|
}
|
|
1714
1748
|
return await runRefresh();
|
|
1715
1749
|
} catch {
|
|
1750
|
+
trace("refresh", { ok: false, why: "threw" });
|
|
1716
1751
|
return null;
|
|
1717
1752
|
} finally {
|
|
1718
1753
|
_refreshInflight = null;
|
|
@@ -1721,7 +1756,8 @@ async function tryRefresh() {
|
|
|
1721
1756
|
return _refreshInflight;
|
|
1722
1757
|
}
|
|
1723
1758
|
__name(tryRefresh, "tryRefresh");
|
|
1724
|
-
function expireSession(response) {
|
|
1759
|
+
function expireSession(response, why) {
|
|
1760
|
+
trace("store.expire", { why, status: response.status, path: pathOf(response.url), handlers: _sessionExpiredHandlers.size });
|
|
1725
1761
|
auth.clearTokens();
|
|
1726
1762
|
for (const cb of Array.from(_sessionExpiredHandlers)) {
|
|
1727
1763
|
try {
|
|
@@ -1864,15 +1900,16 @@ function installAuthOnClient(client2) {
|
|
|
1864
1900
|
client2.interceptors.response.use(async (response, request) => {
|
|
1865
1901
|
if (response.status !== 401) return response;
|
|
1866
1902
|
if (/\/accounts\/(otp|oauth|login|register|token)\b/.test(request.url)) {
|
|
1903
|
+
trace("401.passthrough", { path: pathOf(request.url) });
|
|
1867
1904
|
return response;
|
|
1868
1905
|
}
|
|
1869
1906
|
if (request.headers.get(RETRY_MARKER)) {
|
|
1870
|
-
expireSession(response);
|
|
1907
|
+
expireSession(response, "retry-marker");
|
|
1871
1908
|
return response;
|
|
1872
1909
|
}
|
|
1873
1910
|
const newToken = await tryRefresh();
|
|
1874
1911
|
if (!newToken) {
|
|
1875
|
-
expireSession(response);
|
|
1912
|
+
expireSession(response, "refresh-failed");
|
|
1876
1913
|
return response;
|
|
1877
1914
|
}
|
|
1878
1915
|
const retry = request.clone();
|
|
@@ -1884,7 +1921,7 @@ function installAuthOnClient(client2) {
|
|
|
1884
1921
|
}
|
|
1885
1922
|
try {
|
|
1886
1923
|
const retried = await fetch(retry);
|
|
1887
|
-
if (retried.status === 401) expireSession(retried);
|
|
1924
|
+
if (retried.status === 401) expireSession(retried, "retry-401");
|
|
1888
1925
|
return retried;
|
|
1889
1926
|
} catch {
|
|
1890
1927
|
return response;
|