@bymax-one/nest-auth 1.4.2 → 1.4.4
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/CHANGELOG.md +1840 -1
- package/README.md +706 -74
- package/dist/client/index.cjs +108 -35
- package/dist/client/index.d.cts +90 -15
- package/dist/client/index.d.ts +90 -15
- package/dist/client/index.mjs +109 -36
- package/dist/server/index.cjs +1832 -441
- package/dist/server/index.d.cts +1051 -136
- package/dist/server/index.d.ts +1051 -136
- package/dist/server/index.mjs +1828 -443
- package/dist/shared/index.cjs +103 -10
- package/dist/shared/index.d.cts +128 -5
- package/dist/shared/index.d.ts +128 -5
- package/dist/shared/index.mjs +100 -11
- package/package.json +12 -6
package/dist/client/index.cjs
CHANGED
|
@@ -22,6 +22,28 @@ function shouldSkipRefreshOnUrl(url, suffixes) {
|
|
|
22
22
|
return false;
|
|
23
23
|
}
|
|
24
24
|
__name(shouldSkipRefreshOnUrl, "shouldSkipRefreshOnUrl");
|
|
25
|
+
var ERROR_BODY_READ_TIMEOUT_MS = 2e3;
|
|
26
|
+
async function isExpiredSessionResponse(response) {
|
|
27
|
+
const code = await readErrorCode(response);
|
|
28
|
+
return code === void 0 || code === shared.AUTH_ERROR_CODES.TOKEN_INVALID;
|
|
29
|
+
}
|
|
30
|
+
__name(isExpiredSessionResponse, "isExpiredSessionResponse");
|
|
31
|
+
async function readErrorCode(response) {
|
|
32
|
+
const clone = response.clone();
|
|
33
|
+
let timer;
|
|
34
|
+
const body = await Promise.race([
|
|
35
|
+
clone.json().catch(() => void 0),
|
|
36
|
+
new Promise((resolve) => {
|
|
37
|
+
timer = setTimeout(() => resolve(void 0), ERROR_BODY_READ_TIMEOUT_MS);
|
|
38
|
+
})
|
|
39
|
+
]);
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
if (typeof body !== "object" || body === null) return void 0;
|
|
42
|
+
const envelope = body;
|
|
43
|
+
const code = envelope.error?.code ?? envelope.code;
|
|
44
|
+
return typeof code === "string" ? code : void 0;
|
|
45
|
+
}
|
|
46
|
+
__name(readErrorCode, "readErrorCode");
|
|
25
47
|
function resolveRequestUrl(input, baseUrl) {
|
|
26
48
|
if (typeof input === "string") {
|
|
27
49
|
return baseUrl !== void 0 && !/^https?:\/\//i.test(input) ? `${baseUrl}${input}` : input;
|
|
@@ -32,31 +54,38 @@ function resolveRequestUrl(input, baseUrl) {
|
|
|
32
54
|
return input.url;
|
|
33
55
|
}
|
|
34
56
|
__name(resolveRequestUrl, "resolveRequestUrl");
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
57
|
+
function isUnsafeHeaderName(name) {
|
|
58
|
+
return name === "__proto__" || name === "constructor" || name === "prototype";
|
|
59
|
+
}
|
|
60
|
+
__name(isUnsafeHeaderName, "isUnsafeHeaderName");
|
|
61
|
+
function applyHeaders(target, source) {
|
|
62
|
+
if (source instanceof Headers) {
|
|
63
|
+
source.forEach((value, name) => {
|
|
64
|
+
if (isUnsafeHeaderName(name)) return;
|
|
65
|
+
target.set(name, value);
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
41
68
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
merged[name] = value;
|
|
47
|
-
}
|
|
48
|
-
return merged;
|
|
69
|
+
const entries = Array.isArray(source) ? source : Object.entries(source);
|
|
70
|
+
for (const [name, value] of entries) {
|
|
71
|
+
if (isUnsafeHeaderName(name.toLowerCase())) continue;
|
|
72
|
+
target.set(name, value);
|
|
49
73
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
74
|
+
}
|
|
75
|
+
__name(applyHeaders, "applyHeaders");
|
|
76
|
+
function buildDefaultHeaders(configured) {
|
|
77
|
+
const defaults = new Headers();
|
|
78
|
+
applyHeaders(defaults, DEFAULT_HEADERS);
|
|
79
|
+
if (configured !== void 0) {
|
|
80
|
+
applyHeaders(defaults, configured);
|
|
56
81
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
82
|
+
return defaults;
|
|
83
|
+
}
|
|
84
|
+
__name(buildDefaultHeaders, "buildDefaultHeaders");
|
|
85
|
+
function mergeHeaders(defaults, perRequest) {
|
|
86
|
+
const merged = new Headers(defaults);
|
|
87
|
+
if (perRequest !== void 0) {
|
|
88
|
+
applyHeaders(merged, perRequest);
|
|
60
89
|
}
|
|
61
90
|
return merged;
|
|
62
91
|
}
|
|
@@ -92,6 +121,12 @@ function attachTimeout(init, timeoutMs) {
|
|
|
92
121
|
};
|
|
93
122
|
}
|
|
94
123
|
__name(attachTimeout, "attachTimeout");
|
|
124
|
+
var TERMINAL_REFRESH_CODES = /* @__PURE__ */ new Set([
|
|
125
|
+
shared.AUTH_ERROR_CODES.ACCOUNT_INACTIVE,
|
|
126
|
+
shared.AUTH_ERROR_CODES.ACCOUNT_SUSPENDED,
|
|
127
|
+
shared.AUTH_ERROR_CODES.ACCOUNT_BANNED,
|
|
128
|
+
shared.AUTH_ERROR_CODES.PENDING_APPROVAL
|
|
129
|
+
]);
|
|
95
130
|
async function performRefresh(endpoint, credentials) {
|
|
96
131
|
try {
|
|
97
132
|
const response = await fetch(endpoint, {
|
|
@@ -101,14 +136,38 @@ async function performRefresh(endpoint, credentials) {
|
|
|
101
136
|
"Content-Type": "application/json"
|
|
102
137
|
}
|
|
103
138
|
});
|
|
104
|
-
const ok = response
|
|
105
|
-
await response
|
|
139
|
+
const { ok, status } = response;
|
|
140
|
+
const code = status === 403 ? await readErrorCode(response) : void 0;
|
|
141
|
+
void response.body?.cancel().catch(
|
|
106
142
|
/* istanbul ignore next */
|
|
107
143
|
() => void 0
|
|
108
144
|
);
|
|
109
|
-
return
|
|
145
|
+
if (ok) return {
|
|
146
|
+
ok: true
|
|
147
|
+
};
|
|
148
|
+
if (status === 401) return {
|
|
149
|
+
ok: false,
|
|
150
|
+
reason: "rejected",
|
|
151
|
+
status
|
|
152
|
+
};
|
|
153
|
+
if (code !== void 0 && TERMINAL_REFRESH_CODES.has(code)) {
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
reason: "rejected",
|
|
157
|
+
status
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
reason: "unavailable",
|
|
163
|
+
status
|
|
164
|
+
};
|
|
110
165
|
} catch {
|
|
111
|
-
return
|
|
166
|
+
return {
|
|
167
|
+
ok: false,
|
|
168
|
+
reason: "unreachable",
|
|
169
|
+
status: null
|
|
170
|
+
};
|
|
112
171
|
}
|
|
113
172
|
}
|
|
114
173
|
__name(performRefresh, "performRefresh");
|
|
@@ -116,13 +175,14 @@ function createAuthFetch(config = {}) {
|
|
|
116
175
|
const baseUrl = config.baseUrl;
|
|
117
176
|
const refreshEndpoint = config.refreshEndpoint ?? shared.AUTH_PROXY_ROUTES.clientRefresh;
|
|
118
177
|
const credentials = config.credentials ?? "include";
|
|
119
|
-
const defaultHeaders = config.defaultHeaders
|
|
120
|
-
...DEFAULT_HEADERS,
|
|
121
|
-
...config.defaultHeaders
|
|
122
|
-
} : DEFAULT_HEADERS;
|
|
178
|
+
const defaultHeaders = buildDefaultHeaders(config.defaultHeaders);
|
|
123
179
|
const onSessionExpired = config.onSessionExpired;
|
|
180
|
+
const onRefreshFailed = config.onRefreshFailed;
|
|
124
181
|
const timeoutMs = config.timeout ?? 3e4;
|
|
125
182
|
const skipRefreshSuffixes = shared.buildAuthRefreshSkipSuffixes(config.routePrefix);
|
|
183
|
+
const reportCallbackError = /* @__PURE__ */ __name((err) => {
|
|
184
|
+
console.warn("[nest-auth] onRefreshFailed callback threw:", err);
|
|
185
|
+
}, "reportCallbackError");
|
|
126
186
|
let inFlightRefresh = null;
|
|
127
187
|
function getOrStartRefresh() {
|
|
128
188
|
if (inFlightRefresh !== null) {
|
|
@@ -156,8 +216,18 @@ function createAuthFetch(config = {}) {
|
|
|
156
216
|
if (response.status !== 401 || shouldSkipRefreshOnUrl(url, skipRefreshSuffixes)) {
|
|
157
217
|
return response;
|
|
158
218
|
}
|
|
159
|
-
|
|
160
|
-
|
|
219
|
+
if (!await isExpiredSessionResponse(response)) {
|
|
220
|
+
return response;
|
|
221
|
+
}
|
|
222
|
+
const outcome = await getOrStartRefresh();
|
|
223
|
+
if (!outcome.ok) {
|
|
224
|
+
try {
|
|
225
|
+
void Promise.resolve(onRefreshFailed?.(outcome)).catch(reportCallbackError);
|
|
226
|
+
} catch (err) {
|
|
227
|
+
reportCallbackError(err);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!outcome.ok && outcome.reason === "rejected") {
|
|
161
231
|
try {
|
|
162
232
|
onSessionExpired?.();
|
|
163
233
|
} catch (err) {
|
|
@@ -165,6 +235,7 @@ function createAuthFetch(config = {}) {
|
|
|
165
235
|
}
|
|
166
236
|
return response;
|
|
167
237
|
}
|
|
238
|
+
if (!outcome.ok) return response;
|
|
168
239
|
const retryAttempt = attachTimeout(initBase, timeoutMs);
|
|
169
240
|
try {
|
|
170
241
|
return await fetch(targetForFetch, retryAttempt.init);
|
|
@@ -234,7 +305,7 @@ async function expectNoContent(response) {
|
|
|
234
305
|
const text = await response.text();
|
|
235
306
|
throwAuthError(response, text);
|
|
236
307
|
}
|
|
237
|
-
|
|
308
|
+
void response.body?.cancel().catch(
|
|
238
309
|
/* istanbul ignore next */
|
|
239
310
|
() => void 0
|
|
240
311
|
);
|
|
@@ -365,15 +436,17 @@ function createAuthClient(config) {
|
|
|
365
436
|
async forgotPassword(email, tenantId) {
|
|
366
437
|
return postNoContent(shared.AUTH_ROUTES.password.forgotPassword, {
|
|
367
438
|
email,
|
|
368
|
-
tenantId
|
|
439
|
+
...tenantId !== void 0 ? {
|
|
440
|
+
tenantId
|
|
441
|
+
} : {}
|
|
369
442
|
});
|
|
370
443
|
},
|
|
371
444
|
async resetPassword(input) {
|
|
372
445
|
const payload = {
|
|
373
446
|
email: input.email,
|
|
374
|
-
tenantId: input.tenantId,
|
|
375
447
|
newPassword: input.newPassword
|
|
376
448
|
};
|
|
449
|
+
if (input.tenantId !== void 0) payload["tenantId"] = input.tenantId;
|
|
377
450
|
if (input.token !== void 0) payload["token"] = input.token;
|
|
378
451
|
if (input.otp !== void 0) payload["otp"] = input.otp;
|
|
379
452
|
if (input.verifiedToken !== void 0) payload["verifiedToken"] = input.verifiedToken;
|
package/dist/client/index.d.cts
CHANGED
|
@@ -65,6 +65,25 @@ interface AuthFetchConfig {
|
|
|
65
65
|
* rejects, so consumers can rely on side effects having happened.
|
|
66
66
|
*/
|
|
67
67
|
onSessionExpired?: () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Callback invoked when a refresh attempt did not produce a session, whatever the reason.
|
|
70
|
+
*
|
|
71
|
+
* Fires for every failure, including the one that also triggers {@link onSessionExpired} — and
|
|
72
|
+
* before it, so a consumer sees the reason first. This is what makes the answer to *why* usable
|
|
73
|
+
* rather than merely internal: a rate limit deserves "retrying in a moment", a dropped
|
|
74
|
+
* connection deserves "you appear to be offline", and only a refused credential deserves the
|
|
75
|
+
* sign-in screen.
|
|
76
|
+
*
|
|
77
|
+
* Errors are swallowed and reported through `console.warn`, on the same reasoning as
|
|
78
|
+
* {@link onSessionExpired}: a broken consumer callback must not mask the underlying response.
|
|
79
|
+
* That covers an `async` callback too — the signature is `=> void`, which TypeScript lets an
|
|
80
|
+
* async function satisfy, and its rejection is handled rather than left to surface as an
|
|
81
|
+
* unhandled rejection in your app. It is not awaited: this is a notification, and the
|
|
82
|
+
* sign-out decision does not wait behind a consumer's network call.
|
|
83
|
+
*
|
|
84
|
+
* @param failure - Why the attempt failed, and the status behind it when there was one.
|
|
85
|
+
*/
|
|
86
|
+
onRefreshFailed?: (failure: RefreshFailure) => void;
|
|
68
87
|
/**
|
|
69
88
|
* Per-request timeout in milliseconds.
|
|
70
89
|
*
|
|
@@ -102,6 +121,46 @@ interface AuthFetchConfig {
|
|
|
102
121
|
* 401 response.
|
|
103
122
|
*/
|
|
104
123
|
type AuthFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
124
|
+
/**
|
|
125
|
+
* Why a refresh attempt did not produce a new session.
|
|
126
|
+
*
|
|
127
|
+
* Three answers, not a boolean, because the caller acts differently on each and a boolean forces
|
|
128
|
+
* it to guess. Measured by a consumer against a real browser round: a rate-limited refresh answers
|
|
129
|
+
* `429`, the boolean made that identical to `401`, and the caller signed the user out of a session
|
|
130
|
+
* whose credential was still valid.
|
|
131
|
+
*
|
|
132
|
+
* - `rejected` — a 401, or a 403 whose error code names a terminal account state. The server
|
|
133
|
+
* looked at the credential and refused it. The session is over, and `status` is `401` or `403`.
|
|
134
|
+
* - `unavailable` — it answered, but not with a session: a 429, a 5xx, a 403 from an origin guard,
|
|
135
|
+
* a 404 from a mistyped `routePrefix`. None of those is a statement about the credential.
|
|
136
|
+
* - `unreachable` — no answer at all: offline, DNS, CORS, an aborted request.
|
|
137
|
+
*
|
|
138
|
+
* 403 is decided by the error CODE rather than the status, because the route answers it for two
|
|
139
|
+
* unrelated reasons. `TrustedOriginGuard` covers `/refresh` and answers `auth.untrusted_origin`
|
|
140
|
+
* with 403 — reading that as expiry would sign out every user of a deployment with a wrong
|
|
141
|
+
* `trustedOrigins`. But `refresh` also revokes every session before rethrowing a blocked-account
|
|
142
|
+
* status, and there a 403 genuinely IS the end of the session. This wrapper reads the code
|
|
143
|
+
* itself: it drains the refresh body, and the caller receives the original resource response, so
|
|
144
|
+
* there is nothing left for it to consult.
|
|
145
|
+
*/
|
|
146
|
+
type RefreshFailureReason = 'rejected' | 'unavailable' | 'unreachable';
|
|
147
|
+
/**
|
|
148
|
+
* The result of one refresh attempt.
|
|
149
|
+
*
|
|
150
|
+
* A discriminated union rather than a boolean with a `429` carve-out. The carve-out was offered
|
|
151
|
+
* and declined by the consumer who found the defect, on the grounds that it reproduces the same
|
|
152
|
+
* shape for whichever status turns out to matter next — the refresh has to report WHY it failed,
|
|
153
|
+
* not whether.
|
|
154
|
+
*/
|
|
155
|
+
interface RefreshFailure {
|
|
156
|
+
readonly ok: false;
|
|
157
|
+
readonly reason: RefreshFailureReason;
|
|
158
|
+
/** HTTP status the server answered with, or `null` when there was no answer. */
|
|
159
|
+
readonly status: number | null;
|
|
160
|
+
}
|
|
161
|
+
type RefreshOutcome = {
|
|
162
|
+
readonly ok: true;
|
|
163
|
+
} | RefreshFailure;
|
|
105
164
|
/**
|
|
106
165
|
* Create a configured fetch wrapper with cookie- and refresh-aware
|
|
107
166
|
* behavior baked in.
|
|
@@ -172,24 +231,32 @@ interface RegisterInput {
|
|
|
172
231
|
password: string;
|
|
173
232
|
/** Display name. */
|
|
174
233
|
name: string;
|
|
175
|
-
/**
|
|
176
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Tenant identifier scoping the new account.
|
|
236
|
+
*
|
|
237
|
+
* Optional here because whether it is required is the DEPLOYMENT's answer, not this type's:
|
|
238
|
+
* the server requires it when no `tenantIdResolver` is configured and **refuses** it when one
|
|
239
|
+
* is, answering `auth.validation` with a `tenantId` field detail in both directions. A static
|
|
240
|
+
* shape cannot be right for both, and a type that guessed would contradict the server with more
|
|
241
|
+
* authority than the server has. Send it, or do not, according to the deployment you talk to.
|
|
242
|
+
*/
|
|
243
|
+
tenantId?: string;
|
|
177
244
|
}
|
|
178
245
|
/**
|
|
179
246
|
* Payload accepted by {@link AuthClient.login}.
|
|
180
|
-
*
|
|
181
|
-
* `tenantId` is required because the server-side `LoginDto` enforces it
|
|
182
|
-
* with `@IsNotEmpty()`. Multi-tenant deployments use it to scope the
|
|
183
|
-
* credential lookup; single-tenant deployments must still supply a
|
|
184
|
-
* stable identifier (commonly `'default'`).
|
|
185
247
|
*/
|
|
186
248
|
interface LoginInput {
|
|
187
249
|
/** User's primary email address. */
|
|
188
250
|
email: string;
|
|
189
251
|
/** Plaintext password — server hashes immediately on receipt. */
|
|
190
252
|
password: string;
|
|
191
|
-
/**
|
|
192
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Tenant identifier the login attempt is scoped to.
|
|
255
|
+
*
|
|
256
|
+
* Optional for the reason {@link RegisterInput.tenantId} gives: whether it is required is the
|
|
257
|
+
* deployment's answer, not this type's.
|
|
258
|
+
*/
|
|
259
|
+
tenantId?: string;
|
|
193
260
|
}
|
|
194
261
|
/**
|
|
195
262
|
* Fields shared by every reset-password flow.
|
|
@@ -201,8 +268,13 @@ interface LoginInput {
|
|
|
201
268
|
interface ResetPasswordBase {
|
|
202
269
|
/** Email address of the account being reset. */
|
|
203
270
|
email: string;
|
|
204
|
-
/**
|
|
205
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Tenant the account belongs to.
|
|
273
|
+
*
|
|
274
|
+
* Optional for the reason {@link RegisterInput.tenantId} gives: whether it is required is the
|
|
275
|
+
* deployment's answer, not this type's.
|
|
276
|
+
*/
|
|
277
|
+
tenantId?: string;
|
|
206
278
|
/** New plaintext password — server hashes immediately. Min 8 chars, max 128. */
|
|
207
279
|
newPassword: string;
|
|
208
280
|
}
|
|
@@ -265,10 +337,13 @@ interface AuthClient {
|
|
|
265
337
|
/**
|
|
266
338
|
* Initiate a password reset. The server returns 200 regardless of
|
|
267
339
|
* whether the email is registered — the response carries no signal
|
|
268
|
-
* either way (anti-enumeration design).
|
|
269
|
-
*
|
|
340
|
+
* either way (anti-enumeration design).
|
|
341
|
+
*
|
|
342
|
+
* `tenantId` is optional for the reason {@link RegisterInput.tenantId} gives. It stays a
|
|
343
|
+
* positional parameter rather than moving into an object so the widening breaks no caller:
|
|
344
|
+
* it is last, so every existing two-argument call still compiles.
|
|
270
345
|
*/
|
|
271
|
-
forgotPassword(email: string, tenantId
|
|
346
|
+
forgotPassword(email: string, tenantId?: string): Promise<void>;
|
|
272
347
|
/**
|
|
273
348
|
* Submit a new password using one of the three reset flows:
|
|
274
349
|
* token, OTP, or verified-token. See {@link ResetPasswordInput}
|
|
@@ -298,4 +373,4 @@ interface AuthClient {
|
|
|
298
373
|
*/
|
|
299
374
|
declare function createAuthClient(config: AuthClientConfig): AuthClient;
|
|
300
375
|
|
|
301
|
-
export { type AuthClient, type AuthClientConfig, type AuthFetch, type AuthFetchConfig, type LoginInput, type RegisterInput, type ResetPasswordInput, createAuthClient, createAuthFetch };
|
|
376
|
+
export { type AuthClient, type AuthClientConfig, type AuthFetch, type AuthFetchConfig, type LoginInput, type RefreshFailure, type RefreshFailureReason, type RefreshOutcome, type RegisterInput, type ResetPasswordInput, createAuthClient, createAuthFetch };
|
package/dist/client/index.d.ts
CHANGED
|
@@ -65,6 +65,25 @@ interface AuthFetchConfig {
|
|
|
65
65
|
* rejects, so consumers can rely on side effects having happened.
|
|
66
66
|
*/
|
|
67
67
|
onSessionExpired?: () => void;
|
|
68
|
+
/**
|
|
69
|
+
* Callback invoked when a refresh attempt did not produce a session, whatever the reason.
|
|
70
|
+
*
|
|
71
|
+
* Fires for every failure, including the one that also triggers {@link onSessionExpired} — and
|
|
72
|
+
* before it, so a consumer sees the reason first. This is what makes the answer to *why* usable
|
|
73
|
+
* rather than merely internal: a rate limit deserves "retrying in a moment", a dropped
|
|
74
|
+
* connection deserves "you appear to be offline", and only a refused credential deserves the
|
|
75
|
+
* sign-in screen.
|
|
76
|
+
*
|
|
77
|
+
* Errors are swallowed and reported through `console.warn`, on the same reasoning as
|
|
78
|
+
* {@link onSessionExpired}: a broken consumer callback must not mask the underlying response.
|
|
79
|
+
* That covers an `async` callback too — the signature is `=> void`, which TypeScript lets an
|
|
80
|
+
* async function satisfy, and its rejection is handled rather than left to surface as an
|
|
81
|
+
* unhandled rejection in your app. It is not awaited: this is a notification, and the
|
|
82
|
+
* sign-out decision does not wait behind a consumer's network call.
|
|
83
|
+
*
|
|
84
|
+
* @param failure - Why the attempt failed, and the status behind it when there was one.
|
|
85
|
+
*/
|
|
86
|
+
onRefreshFailed?: (failure: RefreshFailure) => void;
|
|
68
87
|
/**
|
|
69
88
|
* Per-request timeout in milliseconds.
|
|
70
89
|
*
|
|
@@ -102,6 +121,46 @@ interface AuthFetchConfig {
|
|
|
102
121
|
* 401 response.
|
|
103
122
|
*/
|
|
104
123
|
type AuthFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
124
|
+
/**
|
|
125
|
+
* Why a refresh attempt did not produce a new session.
|
|
126
|
+
*
|
|
127
|
+
* Three answers, not a boolean, because the caller acts differently on each and a boolean forces
|
|
128
|
+
* it to guess. Measured by a consumer against a real browser round: a rate-limited refresh answers
|
|
129
|
+
* `429`, the boolean made that identical to `401`, and the caller signed the user out of a session
|
|
130
|
+
* whose credential was still valid.
|
|
131
|
+
*
|
|
132
|
+
* - `rejected` — a 401, or a 403 whose error code names a terminal account state. The server
|
|
133
|
+
* looked at the credential and refused it. The session is over, and `status` is `401` or `403`.
|
|
134
|
+
* - `unavailable` — it answered, but not with a session: a 429, a 5xx, a 403 from an origin guard,
|
|
135
|
+
* a 404 from a mistyped `routePrefix`. None of those is a statement about the credential.
|
|
136
|
+
* - `unreachable` — no answer at all: offline, DNS, CORS, an aborted request.
|
|
137
|
+
*
|
|
138
|
+
* 403 is decided by the error CODE rather than the status, because the route answers it for two
|
|
139
|
+
* unrelated reasons. `TrustedOriginGuard` covers `/refresh` and answers `auth.untrusted_origin`
|
|
140
|
+
* with 403 — reading that as expiry would sign out every user of a deployment with a wrong
|
|
141
|
+
* `trustedOrigins`. But `refresh` also revokes every session before rethrowing a blocked-account
|
|
142
|
+
* status, and there a 403 genuinely IS the end of the session. This wrapper reads the code
|
|
143
|
+
* itself: it drains the refresh body, and the caller receives the original resource response, so
|
|
144
|
+
* there is nothing left for it to consult.
|
|
145
|
+
*/
|
|
146
|
+
type RefreshFailureReason = 'rejected' | 'unavailable' | 'unreachable';
|
|
147
|
+
/**
|
|
148
|
+
* The result of one refresh attempt.
|
|
149
|
+
*
|
|
150
|
+
* A discriminated union rather than a boolean with a `429` carve-out. The carve-out was offered
|
|
151
|
+
* and declined by the consumer who found the defect, on the grounds that it reproduces the same
|
|
152
|
+
* shape for whichever status turns out to matter next — the refresh has to report WHY it failed,
|
|
153
|
+
* not whether.
|
|
154
|
+
*/
|
|
155
|
+
interface RefreshFailure {
|
|
156
|
+
readonly ok: false;
|
|
157
|
+
readonly reason: RefreshFailureReason;
|
|
158
|
+
/** HTTP status the server answered with, or `null` when there was no answer. */
|
|
159
|
+
readonly status: number | null;
|
|
160
|
+
}
|
|
161
|
+
type RefreshOutcome = {
|
|
162
|
+
readonly ok: true;
|
|
163
|
+
} | RefreshFailure;
|
|
105
164
|
/**
|
|
106
165
|
* Create a configured fetch wrapper with cookie- and refresh-aware
|
|
107
166
|
* behavior baked in.
|
|
@@ -172,24 +231,32 @@ interface RegisterInput {
|
|
|
172
231
|
password: string;
|
|
173
232
|
/** Display name. */
|
|
174
233
|
name: string;
|
|
175
|
-
/**
|
|
176
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Tenant identifier scoping the new account.
|
|
236
|
+
*
|
|
237
|
+
* Optional here because whether it is required is the DEPLOYMENT's answer, not this type's:
|
|
238
|
+
* the server requires it when no `tenantIdResolver` is configured and **refuses** it when one
|
|
239
|
+
* is, answering `auth.validation` with a `tenantId` field detail in both directions. A static
|
|
240
|
+
* shape cannot be right for both, and a type that guessed would contradict the server with more
|
|
241
|
+
* authority than the server has. Send it, or do not, according to the deployment you talk to.
|
|
242
|
+
*/
|
|
243
|
+
tenantId?: string;
|
|
177
244
|
}
|
|
178
245
|
/**
|
|
179
246
|
* Payload accepted by {@link AuthClient.login}.
|
|
180
|
-
*
|
|
181
|
-
* `tenantId` is required because the server-side `LoginDto` enforces it
|
|
182
|
-
* with `@IsNotEmpty()`. Multi-tenant deployments use it to scope the
|
|
183
|
-
* credential lookup; single-tenant deployments must still supply a
|
|
184
|
-
* stable identifier (commonly `'default'`).
|
|
185
247
|
*/
|
|
186
248
|
interface LoginInput {
|
|
187
249
|
/** User's primary email address. */
|
|
188
250
|
email: string;
|
|
189
251
|
/** Plaintext password — server hashes immediately on receipt. */
|
|
190
252
|
password: string;
|
|
191
|
-
/**
|
|
192
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Tenant identifier the login attempt is scoped to.
|
|
255
|
+
*
|
|
256
|
+
* Optional for the reason {@link RegisterInput.tenantId} gives: whether it is required is the
|
|
257
|
+
* deployment's answer, not this type's.
|
|
258
|
+
*/
|
|
259
|
+
tenantId?: string;
|
|
193
260
|
}
|
|
194
261
|
/**
|
|
195
262
|
* Fields shared by every reset-password flow.
|
|
@@ -201,8 +268,13 @@ interface LoginInput {
|
|
|
201
268
|
interface ResetPasswordBase {
|
|
202
269
|
/** Email address of the account being reset. */
|
|
203
270
|
email: string;
|
|
204
|
-
/**
|
|
205
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Tenant the account belongs to.
|
|
273
|
+
*
|
|
274
|
+
* Optional for the reason {@link RegisterInput.tenantId} gives: whether it is required is the
|
|
275
|
+
* deployment's answer, not this type's.
|
|
276
|
+
*/
|
|
277
|
+
tenantId?: string;
|
|
206
278
|
/** New plaintext password — server hashes immediately. Min 8 chars, max 128. */
|
|
207
279
|
newPassword: string;
|
|
208
280
|
}
|
|
@@ -265,10 +337,13 @@ interface AuthClient {
|
|
|
265
337
|
/**
|
|
266
338
|
* Initiate a password reset. The server returns 200 regardless of
|
|
267
339
|
* whether the email is registered — the response carries no signal
|
|
268
|
-
* either way (anti-enumeration design).
|
|
269
|
-
*
|
|
340
|
+
* either way (anti-enumeration design).
|
|
341
|
+
*
|
|
342
|
+
* `tenantId` is optional for the reason {@link RegisterInput.tenantId} gives. It stays a
|
|
343
|
+
* positional parameter rather than moving into an object so the widening breaks no caller:
|
|
344
|
+
* it is last, so every existing two-argument call still compiles.
|
|
270
345
|
*/
|
|
271
|
-
forgotPassword(email: string, tenantId
|
|
346
|
+
forgotPassword(email: string, tenantId?: string): Promise<void>;
|
|
272
347
|
/**
|
|
273
348
|
* Submit a new password using one of the three reset flows:
|
|
274
349
|
* token, OTP, or verified-token. See {@link ResetPasswordInput}
|
|
@@ -298,4 +373,4 @@ interface AuthClient {
|
|
|
298
373
|
*/
|
|
299
374
|
declare function createAuthClient(config: AuthClientConfig): AuthClient;
|
|
300
375
|
|
|
301
|
-
export { type AuthClient, type AuthClientConfig, type AuthFetch, type AuthFetchConfig, type LoginInput, type RegisterInput, type ResetPasswordInput, createAuthClient, createAuthFetch };
|
|
376
|
+
export { type AuthClient, type AuthClientConfig, type AuthFetch, type AuthFetchConfig, type LoginInput, type RefreshFailure, type RefreshFailureReason, type RefreshOutcome, type RegisterInput, type ResetPasswordInput, createAuthClient, createAuthFetch };
|