@bymax-one/nest-auth 1.0.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/CHANGELOG.md +369 -0
- package/LICENSE +21 -0
- package/README.md +886 -0
- package/dist/client/index.cjs +347 -0
- package/dist/client/index.d.cts +301 -0
- package/dist/client/index.d.ts +301 -0
- package/dist/client/index.mjs +341 -0
- package/dist/nextjs/index.cjs +1103 -0
- package/dist/nextjs/index.d.cts +1025 -0
- package/dist/nextjs/index.d.ts +1025 -0
- package/dist/nextjs/index.mjs +1083 -0
- package/dist/react/index.cjs +304 -0
- package/dist/react/index.d.cts +342 -0
- package/dist/react/index.d.ts +342 -0
- package/dist/react/index.mjs +295 -0
- package/dist/server/index.cjs +8039 -0
- package/dist/server/index.d.cts +5092 -0
- package/dist/server/index.d.ts +5092 -0
- package/dist/server/index.mjs +7996 -0
- package/dist/shared/index.cjs +240 -0
- package/dist/shared/index.d.cts +736 -0
- package/dist/shared/index.d.ts +736 -0
- package/dist/shared/index.mjs +223 -0
- package/package.json +195 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default cookie names and path used by the @bymax-one/nest-auth server.
|
|
3
|
+
*
|
|
4
|
+
* These literals are the same defaults declared in
|
|
5
|
+
* `src/server/config/default-options.ts` under the `cookies` group. They
|
|
6
|
+
* are duplicated in the shared subpath so that browser and edge-runtime
|
|
7
|
+
* clients can reference them without pulling in any server-side runtime.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* If a consumer overrides cookie names on the server (via
|
|
11
|
+
* `BymaxAuthModule.forRoot({ cookies: { ... } })`), client code MUST be
|
|
12
|
+
* configured with the matching values — these constants reflect only the
|
|
13
|
+
* library defaults.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Default name of the HttpOnly cookie that carries the JWT access token.
|
|
17
|
+
*
|
|
18
|
+
* Mirrors `cookies.accessTokenName` in the server's `DEFAULT_OPTIONS`.
|
|
19
|
+
*/
|
|
20
|
+
declare const AUTH_ACCESS_COOKIE_NAME: "access_token";
|
|
21
|
+
/**
|
|
22
|
+
* Default name of the HttpOnly cookie that carries the opaque refresh token.
|
|
23
|
+
*
|
|
24
|
+
* Mirrors `cookies.refreshTokenName` in the server's `DEFAULT_OPTIONS`.
|
|
25
|
+
*/
|
|
26
|
+
declare const AUTH_REFRESH_COOKIE_NAME: "refresh_token";
|
|
27
|
+
/**
|
|
28
|
+
* Default name of the non-HttpOnly cookie used as a hint to the frontend
|
|
29
|
+
* that a session likely exists.
|
|
30
|
+
*
|
|
31
|
+
* The actual access token is HttpOnly and not readable from JavaScript;
|
|
32
|
+
* this companion flag lets the client decide whether to attempt a session
|
|
33
|
+
* bootstrap before issuing requests. Mirrors `cookies.sessionSignalName`
|
|
34
|
+
* in the server's `DEFAULT_OPTIONS`.
|
|
35
|
+
*/
|
|
36
|
+
declare const AUTH_HAS_SESSION_COOKIE_NAME: "has_session";
|
|
37
|
+
/**
|
|
38
|
+
* Default path scope of the refresh-token cookie.
|
|
39
|
+
*
|
|
40
|
+
* Restricting the refresh cookie to the auth route prefix prevents the
|
|
41
|
+
* browser from attaching it to every request, which reduces the attack
|
|
42
|
+
* surface for token exfiltration via cross-site request forgery and side
|
|
43
|
+
* channels. Mirrors `cookies.refreshCookiePath` in the server's
|
|
44
|
+
* `DEFAULT_OPTIONS`.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Two server options influence the correct value:
|
|
48
|
+
* - `cookies.refreshCookiePath` — direct override of the cookie scope.
|
|
49
|
+
* - `routePrefix` — when changed (default `'auth'`), the refresh cookie
|
|
50
|
+
* path must be updated to match (e.g. setting `routePrefix: 'api/auth'`
|
|
51
|
+
* requires `cookies.refreshCookiePath: '/api/auth'`), otherwise the
|
|
52
|
+
* browser will not attach the cookie to refresh requests and every
|
|
53
|
+
* refresh will fail.
|
|
54
|
+
*/
|
|
55
|
+
declare const AUTH_REFRESH_COOKIE_PATH: "/auth";
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Stable machine-readable error codes returned by the
|
|
59
|
+
* @bymax-one/nest-auth server, mirrored here for the shared subpath.
|
|
60
|
+
*
|
|
61
|
+
* Re-declared (not re-exported from `../../server`) so that this entry
|
|
62
|
+
* point stays free of any server-side runtime imports — the shared
|
|
63
|
+
* subpath must remain importable in browser and edge runtimes.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* These literals MUST stay byte-for-byte identical to
|
|
67
|
+
* `AUTH_ERROR_CODES` in `src/server/errors/auth-error-codes.ts`. Any
|
|
68
|
+
* change there must be reflected here, or the client will branch on the
|
|
69
|
+
* wrong code at runtime.
|
|
70
|
+
*/
|
|
71
|
+
declare const AUTH_ERROR_CODES: {
|
|
72
|
+
readonly INVALID_CREDENTIALS: "auth.invalid_credentials";
|
|
73
|
+
readonly ACCOUNT_LOCKED: "auth.account_locked";
|
|
74
|
+
readonly ACCOUNT_INACTIVE: "auth.account_inactive";
|
|
75
|
+
readonly ACCOUNT_SUSPENDED: "auth.account_suspended";
|
|
76
|
+
readonly ACCOUNT_BANNED: "auth.account_banned";
|
|
77
|
+
readonly PENDING_APPROVAL: "auth.pending_approval";
|
|
78
|
+
readonly TOKEN_EXPIRED: "auth.token_expired";
|
|
79
|
+
readonly TOKEN_REVOKED: "auth.token_revoked";
|
|
80
|
+
readonly TOKEN_INVALID: "auth.token_invalid";
|
|
81
|
+
readonly REFRESH_TOKEN_INVALID: "auth.refresh_token_invalid";
|
|
82
|
+
readonly SESSION_EXPIRED: "auth.session_expired";
|
|
83
|
+
readonly SESSION_LIMIT_REACHED: "auth.session_limit_reached";
|
|
84
|
+
readonly SESSION_NOT_FOUND: "auth.session_not_found";
|
|
85
|
+
readonly EMAIL_ALREADY_EXISTS: "auth.email_already_exists";
|
|
86
|
+
readonly EMAIL_NOT_VERIFIED: "auth.email_not_verified";
|
|
87
|
+
readonly MFA_REQUIRED: "auth.mfa_required";
|
|
88
|
+
readonly MFA_INVALID_CODE: "auth.mfa_invalid_code";
|
|
89
|
+
readonly MFA_ALREADY_ENABLED: "auth.mfa_already_enabled";
|
|
90
|
+
readonly MFA_NOT_ENABLED: "auth.mfa_not_enabled";
|
|
91
|
+
readonly MFA_SETUP_REQUIRED: "auth.mfa_setup_required";
|
|
92
|
+
readonly MFA_TEMP_TOKEN_INVALID: "auth.mfa_temp_token_invalid";
|
|
93
|
+
readonly RECOVERY_CODE_INVALID: "auth.recovery_code_invalid";
|
|
94
|
+
readonly PASSWORD_TOO_WEAK: "auth.password_too_weak";
|
|
95
|
+
readonly PASSWORD_RESET_TOKEN_INVALID: "auth.password_reset_token_invalid";
|
|
96
|
+
readonly PASSWORD_RESET_TOKEN_EXPIRED: "auth.password_reset_token_expired";
|
|
97
|
+
readonly OTP_INVALID: "auth.otp_invalid";
|
|
98
|
+
readonly OTP_EXPIRED: "auth.otp_expired";
|
|
99
|
+
readonly OTP_MAX_ATTEMPTS: "auth.otp_max_attempts";
|
|
100
|
+
readonly INSUFFICIENT_ROLE: "auth.insufficient_role";
|
|
101
|
+
readonly FORBIDDEN: "auth.forbidden";
|
|
102
|
+
readonly INVALID_INVITATION_TOKEN: "auth.invalid_invitation_token";
|
|
103
|
+
readonly OAUTH_FAILED: "auth.oauth_failed";
|
|
104
|
+
readonly OAUTH_EMAIL_MISMATCH: "auth.oauth_email_mismatch";
|
|
105
|
+
readonly PLATFORM_AUTH_REQUIRED: "auth.platform_auth_required";
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* String literal union of every `AUTH_ERROR_CODES` value.
|
|
109
|
+
*
|
|
110
|
+
* Use this type to constrain client-side error-code matchers (switch
|
|
111
|
+
* statements, lookup tables) so that an unrecognized code is caught at
|
|
112
|
+
* compile time.
|
|
113
|
+
*/
|
|
114
|
+
type AuthErrorCode = (typeof AUTH_ERROR_CODES)[keyof typeof AUTH_ERROR_CODES];
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* HTTP route paths exposed by the @bymax-one/nest-auth server controllers.
|
|
118
|
+
*
|
|
119
|
+
* All paths are relative to the configured `routePrefix` (default
|
|
120
|
+
* `'auth'`). The defaults below assume the prefix-less paths so that
|
|
121
|
+
* client code can compose a full URL as
|
|
122
|
+
* `${baseUrl}/${routePrefix}/${AUTH_ROUTES.dashboard.login}`.
|
|
123
|
+
*
|
|
124
|
+
* @remarks
|
|
125
|
+
* These constants exist so server routes and client request URLs stay
|
|
126
|
+
* in sync. Mirrors the `@Controller`/`@Post`/`@Get` declarations in
|
|
127
|
+
* `src/server/controllers/*.ts`. Update both sides together if a route
|
|
128
|
+
* is renamed.
|
|
129
|
+
*/
|
|
130
|
+
/**
|
|
131
|
+
* Dashboard (tenant user) authentication routes.
|
|
132
|
+
*
|
|
133
|
+
* Mounted at the top of the auth route prefix
|
|
134
|
+
* (`src/server/controllers/auth.controller.ts`).
|
|
135
|
+
*/
|
|
136
|
+
declare const AUTH_DASHBOARD_ROUTES: {
|
|
137
|
+
/** POST — register a new local (email + password) user. */
|
|
138
|
+
readonly register: "register";
|
|
139
|
+
/** POST — exchange credentials for an access token (or MFA challenge). */
|
|
140
|
+
readonly login: "login";
|
|
141
|
+
/** POST — revoke the current access and refresh tokens. */
|
|
142
|
+
readonly logout: "logout";
|
|
143
|
+
/** POST — rotate a valid refresh token for a fresh access token. */
|
|
144
|
+
readonly refresh: "refresh";
|
|
145
|
+
/** GET — return the currently authenticated user. */
|
|
146
|
+
readonly me: "me";
|
|
147
|
+
/** POST — submit the email-verification token returned by the verification email. */
|
|
148
|
+
readonly verifyEmail: "verify-email";
|
|
149
|
+
/** POST — request a fresh email-verification token. */
|
|
150
|
+
readonly resendVerification: "resend-verification";
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* MFA (TOTP) routes.
|
|
154
|
+
*
|
|
155
|
+
* Mounted under `mfa/` (`src/server/controllers/mfa.controller.ts`).
|
|
156
|
+
*/
|
|
157
|
+
declare const AUTH_MFA_ROUTES: {
|
|
158
|
+
/** POST — begin TOTP enrollment and return the secret + provisioning URI. */
|
|
159
|
+
readonly setup: "mfa/setup";
|
|
160
|
+
/** POST — confirm TOTP enrollment by submitting the first valid code. */
|
|
161
|
+
readonly verifyEnable: "mfa/verify-enable";
|
|
162
|
+
/** POST — exchange an MFA temp token + OTP for a full access token. */
|
|
163
|
+
readonly challenge: "mfa/challenge";
|
|
164
|
+
/** POST — disable MFA after re-authenticating with a fresh OTP. */
|
|
165
|
+
readonly disable: "mfa/disable";
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Password reset routes.
|
|
169
|
+
*
|
|
170
|
+
* Mounted under `password/`
|
|
171
|
+
* (`src/server/controllers/password-reset.controller.ts`).
|
|
172
|
+
*/
|
|
173
|
+
declare const AUTH_PASSWORD_ROUTES: {
|
|
174
|
+
/** POST — request a password reset (email lookup is anti-enumeration). */
|
|
175
|
+
readonly forgotPassword: "password/forgot-password";
|
|
176
|
+
/** POST — submit a new password using the reset token. */
|
|
177
|
+
readonly resetPassword: "password/reset-password";
|
|
178
|
+
/** POST — verify the reset OTP (when `passwordReset.method = 'otp'`). */
|
|
179
|
+
readonly verifyOtp: "password/verify-otp";
|
|
180
|
+
/** POST — request a fresh OTP after the previous one expired. */
|
|
181
|
+
readonly resendOtp: "password/resend-otp";
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Platform administrator routes.
|
|
185
|
+
*
|
|
186
|
+
* Mounted under `platform/`
|
|
187
|
+
* (`src/server/controllers/platform-auth.controller.ts`).
|
|
188
|
+
*/
|
|
189
|
+
declare const AUTH_PLATFORM_ROUTES: {
|
|
190
|
+
/** POST — exchange platform admin credentials for an access token. */
|
|
191
|
+
readonly login: "platform/login";
|
|
192
|
+
/** POST — exchange an MFA temp token + OTP for a platform access token. */
|
|
193
|
+
readonly mfaChallenge: "platform/mfa/challenge";
|
|
194
|
+
/** GET — return the currently authenticated platform administrator. */
|
|
195
|
+
readonly me: "platform/me";
|
|
196
|
+
/** POST — revoke the platform access and refresh tokens. */
|
|
197
|
+
readonly logout: "platform/logout";
|
|
198
|
+
/** POST — rotate a platform refresh token for a fresh access token. */
|
|
199
|
+
readonly refresh: "platform/refresh";
|
|
200
|
+
/** DELETE — revoke every active session for the current platform admin. */
|
|
201
|
+
readonly revokeAllSessions: "platform/sessions";
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Session management routes (`controllers.sessions: true`).
|
|
205
|
+
*
|
|
206
|
+
* Mounted under `sessions/`
|
|
207
|
+
* (`src/server/controllers/session.controller.ts`).
|
|
208
|
+
*/
|
|
209
|
+
declare const AUTH_SESSION_ROUTES: {
|
|
210
|
+
/** GET — list every active session for the current user. */
|
|
211
|
+
readonly list: "sessions";
|
|
212
|
+
/** DELETE — revoke every session belonging to the current user. */
|
|
213
|
+
readonly revokeAll: "sessions/all";
|
|
214
|
+
/**
|
|
215
|
+
* DELETE — revoke a single session by id. The `:id` placeholder must be
|
|
216
|
+
* substituted with the session identifier before the request is issued.
|
|
217
|
+
*/
|
|
218
|
+
readonly revokeOne: "sessions/:id";
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Invitation routes (`controllers.invitations: true`).
|
|
222
|
+
*
|
|
223
|
+
* Mounted under `invitations/`
|
|
224
|
+
* (`src/server/controllers/invitation.controller.ts`).
|
|
225
|
+
*/
|
|
226
|
+
declare const AUTH_INVITATION_ROUTES: {
|
|
227
|
+
/** POST — create a new invitation (admin-only). */
|
|
228
|
+
readonly create: "invitations";
|
|
229
|
+
/** POST — accept an invitation token and register the recipient. */
|
|
230
|
+
readonly accept: "invitations/accept";
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* Aggregated route map grouped by domain.
|
|
234
|
+
*
|
|
235
|
+
* Provides a single import point for client code that needs to reach
|
|
236
|
+
* multiple route families.
|
|
237
|
+
*/
|
|
238
|
+
declare const AUTH_ROUTES: {
|
|
239
|
+
readonly dashboard: {
|
|
240
|
+
/** POST — register a new local (email + password) user. */
|
|
241
|
+
readonly register: "register";
|
|
242
|
+
/** POST — exchange credentials for an access token (or MFA challenge). */
|
|
243
|
+
readonly login: "login";
|
|
244
|
+
/** POST — revoke the current access and refresh tokens. */
|
|
245
|
+
readonly logout: "logout";
|
|
246
|
+
/** POST — rotate a valid refresh token for a fresh access token. */
|
|
247
|
+
readonly refresh: "refresh";
|
|
248
|
+
/** GET — return the currently authenticated user. */
|
|
249
|
+
readonly me: "me";
|
|
250
|
+
/** POST — submit the email-verification token returned by the verification email. */
|
|
251
|
+
readonly verifyEmail: "verify-email";
|
|
252
|
+
/** POST — request a fresh email-verification token. */
|
|
253
|
+
readonly resendVerification: "resend-verification";
|
|
254
|
+
};
|
|
255
|
+
readonly mfa: {
|
|
256
|
+
/** POST — begin TOTP enrollment and return the secret + provisioning URI. */
|
|
257
|
+
readonly setup: "mfa/setup";
|
|
258
|
+
/** POST — confirm TOTP enrollment by submitting the first valid code. */
|
|
259
|
+
readonly verifyEnable: "mfa/verify-enable";
|
|
260
|
+
/** POST — exchange an MFA temp token + OTP for a full access token. */
|
|
261
|
+
readonly challenge: "mfa/challenge";
|
|
262
|
+
/** POST — disable MFA after re-authenticating with a fresh OTP. */
|
|
263
|
+
readonly disable: "mfa/disable";
|
|
264
|
+
};
|
|
265
|
+
readonly password: {
|
|
266
|
+
/** POST — request a password reset (email lookup is anti-enumeration). */
|
|
267
|
+
readonly forgotPassword: "password/forgot-password";
|
|
268
|
+
/** POST — submit a new password using the reset token. */
|
|
269
|
+
readonly resetPassword: "password/reset-password";
|
|
270
|
+
/** POST — verify the reset OTP (when `passwordReset.method = 'otp'`). */
|
|
271
|
+
readonly verifyOtp: "password/verify-otp";
|
|
272
|
+
/** POST — request a fresh OTP after the previous one expired. */
|
|
273
|
+
readonly resendOtp: "password/resend-otp";
|
|
274
|
+
};
|
|
275
|
+
readonly platform: {
|
|
276
|
+
/** POST — exchange platform admin credentials for an access token. */
|
|
277
|
+
readonly login: "platform/login";
|
|
278
|
+
/** POST — exchange an MFA temp token + OTP for a platform access token. */
|
|
279
|
+
readonly mfaChallenge: "platform/mfa/challenge";
|
|
280
|
+
/** GET — return the currently authenticated platform administrator. */
|
|
281
|
+
readonly me: "platform/me";
|
|
282
|
+
/** POST — revoke the platform access and refresh tokens. */
|
|
283
|
+
readonly logout: "platform/logout";
|
|
284
|
+
/** POST — rotate a platform refresh token for a fresh access token. */
|
|
285
|
+
readonly refresh: "platform/refresh";
|
|
286
|
+
/** DELETE — revoke every active session for the current platform admin. */
|
|
287
|
+
readonly revokeAllSessions: "platform/sessions";
|
|
288
|
+
};
|
|
289
|
+
readonly sessions: {
|
|
290
|
+
/** GET — list every active session for the current user. */
|
|
291
|
+
readonly list: "sessions";
|
|
292
|
+
/** DELETE — revoke every session belonging to the current user. */
|
|
293
|
+
readonly revokeAll: "sessions/all";
|
|
294
|
+
/**
|
|
295
|
+
* DELETE — revoke a single session by id. The `:id` placeholder must be
|
|
296
|
+
* substituted with the session identifier before the request is issued.
|
|
297
|
+
*/
|
|
298
|
+
readonly revokeOne: "sessions/:id";
|
|
299
|
+
};
|
|
300
|
+
readonly invitations: {
|
|
301
|
+
/** POST — create a new invitation (admin-only). */
|
|
302
|
+
readonly create: "invitations";
|
|
303
|
+
/** POST — accept an invitation token and register the recipient. */
|
|
304
|
+
readonly accept: "invitations/accept";
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* Default Next.js proxy endpoints used by `createAuthClient` and the
|
|
309
|
+
* Next.js helper handlers in the `nextjs` subpath.
|
|
310
|
+
*
|
|
311
|
+
* These are the front-channel proxy routes a Next.js application is
|
|
312
|
+
* expected to expose under `app/api/auth/*`. They translate browser
|
|
313
|
+
* requests into upstream NestJS calls while keeping cookies HttpOnly.
|
|
314
|
+
*/
|
|
315
|
+
declare const AUTH_PROXY_ROUTES: {
|
|
316
|
+
/** POST — same-domain refresh entry point (server-to-server). */
|
|
317
|
+
readonly clientRefresh: "/api/auth/client-refresh";
|
|
318
|
+
/** GET — invisible refresh used by the proxy to reissue tokens. */
|
|
319
|
+
readonly silentRefresh: "/api/auth/silent-refresh";
|
|
320
|
+
/** POST — Next.js logout proxy (clears cookies + revokes server-side). */
|
|
321
|
+
readonly logout: "/api/auth/logout";
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Build the pathname-suffix skip list that `createAuthFetch` uses to
|
|
325
|
+
* decide whether a 401 from a given URL should trigger a refresh.
|
|
326
|
+
*
|
|
327
|
+
* Parameterized by `routePrefix` so that deployments using a non-default
|
|
328
|
+
* mount point (e.g. `'authentication'`, `'api/v1/auth'`) get a skip list
|
|
329
|
+
* that actually matches their URLs. The suffixes combine the prefix
|
|
330
|
+
* with every known auth-issuing controller path plus the proxy refresh
|
|
331
|
+
* endpoints (which never carry the server prefix).
|
|
332
|
+
*
|
|
333
|
+
* @param routePrefix - The NestJS `routePrefix` in effect for the
|
|
334
|
+
* consumer's deployment. Leading/trailing slashes are normalized.
|
|
335
|
+
* Default is `'auth'` when omitted.
|
|
336
|
+
*/
|
|
337
|
+
declare function buildAuthRefreshSkipSuffixes(routePrefix?: string): readonly string[];
|
|
338
|
+
/**
|
|
339
|
+
* Default pathname-suffix skip list for the canonical `'auth'` prefix.
|
|
340
|
+
*
|
|
341
|
+
* Exported for backwards compatibility and for consumers who know they
|
|
342
|
+
* use the default prefix. Non-default deployments should call
|
|
343
|
+
* {@link buildAuthRefreshSkipSuffixes} directly or pass `routePrefix`
|
|
344
|
+
* to `createAuthFetch`/`createAuthClient`.
|
|
345
|
+
*/
|
|
346
|
+
declare const AUTH_REFRESH_SKIP_PATH_SUFFIXES: readonly string[];
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Client-safe representation of an authenticated user for @bymax-one/nest-auth.
|
|
350
|
+
*
|
|
351
|
+
* This is a deliberately narrow projection of the server-side `AuthUser`
|
|
352
|
+
* interface. It contains only the fields a frontend application needs to
|
|
353
|
+
* render UI, gate features, and personalize the experience. Credential
|
|
354
|
+
* material (`passwordHash`), MFA secrets (`mfaSecret`, `mfaRecoveryCodes`),
|
|
355
|
+
* and OAuth bookkeeping fields are intentionally omitted so that a
|
|
356
|
+
* misconfigured server endpoint cannot accidentally leak them through this
|
|
357
|
+
* type.
|
|
358
|
+
*
|
|
359
|
+
* @remarks
|
|
360
|
+
* The shape is structurally compatible with `Pick<AuthUser, ...>` from the
|
|
361
|
+
* server subpath. The shared subpath redeclares the type (instead of
|
|
362
|
+
* re-exporting from `../../server`) to keep this entry point free of any
|
|
363
|
+
* server-side runtime dependencies — it must remain importable in browser
|
|
364
|
+
* and edge runtimes.
|
|
365
|
+
*/
|
|
366
|
+
interface AuthUserClient {
|
|
367
|
+
/** Unique internal identifier for the user (UUID or similar). */
|
|
368
|
+
id: string;
|
|
369
|
+
/** User's primary email address. */
|
|
370
|
+
email: string;
|
|
371
|
+
/** Display name of the user. */
|
|
372
|
+
name: string;
|
|
373
|
+
/** Authorization role within the tenant (application-defined). */
|
|
374
|
+
role: string;
|
|
375
|
+
/** Tenant identifier that scopes the user to a specific organization. */
|
|
376
|
+
tenantId: string;
|
|
377
|
+
/** Account lifecycle status (e.g. `'active'`, `'suspended'`). */
|
|
378
|
+
status: string;
|
|
379
|
+
/** Whether Time-based One-Time Password (TOTP) MFA is currently enabled. */
|
|
380
|
+
mfaEnabled: boolean;
|
|
381
|
+
/**
|
|
382
|
+
* URL of the user's avatar image, when available.
|
|
383
|
+
* Optional because not every consumer application stores avatars.
|
|
384
|
+
*/
|
|
385
|
+
avatarUrl?: string;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Client-safe representation of an authenticated platform administrator.
|
|
389
|
+
*
|
|
390
|
+
* Mirrors `AuthUserClient` but reflects the differences in the platform
|
|
391
|
+
* domain: there is no `tenantId` (platform admins live above tenants) and an
|
|
392
|
+
* optional `platformId` for multi-platform deployments.
|
|
393
|
+
*/
|
|
394
|
+
interface AuthPlatformUserClient {
|
|
395
|
+
/** Unique internal identifier for the platform administrator. */
|
|
396
|
+
id: string;
|
|
397
|
+
/** Platform administrator's primary email address. */
|
|
398
|
+
email: string;
|
|
399
|
+
/** Display name of the platform administrator. */
|
|
400
|
+
name: string;
|
|
401
|
+
/** Authorization role within the platform layer (application-defined). */
|
|
402
|
+
role: string;
|
|
403
|
+
/** Account lifecycle status (e.g. `'active'`, `'suspended'`). */
|
|
404
|
+
status: string;
|
|
405
|
+
/** Whether Time-based One-Time Password (TOTP) MFA is currently enabled. */
|
|
406
|
+
mfaEnabled: boolean;
|
|
407
|
+
/**
|
|
408
|
+
* URL of the administrator's avatar image, when available.
|
|
409
|
+
* Optional because not every consumer application stores avatars.
|
|
410
|
+
*/
|
|
411
|
+
avatarUrl?: string;
|
|
412
|
+
/**
|
|
413
|
+
* Logical platform identifier for multi-platform deployments.
|
|
414
|
+
* Absent in single-platform configurations.
|
|
415
|
+
*/
|
|
416
|
+
platformId?: string;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Result returned by the client after a successful login or token refresh.
|
|
421
|
+
*
|
|
422
|
+
* @remarks
|
|
423
|
+
* `accessToken` is included so that bearer-token consumers (mobile apps,
|
|
424
|
+
* non-cookie environments) can surface it directly. When the server is
|
|
425
|
+
* configured for cookie delivery (`tokenDelivery: 'cookie'`), the access
|
|
426
|
+
* token is set as an HttpOnly cookie by the server and this field is an
|
|
427
|
+
* empty string — clients should rely on the cookie in that mode.
|
|
428
|
+
*/
|
|
429
|
+
interface AuthResult {
|
|
430
|
+
/** The authenticated user (credential and secret fields omitted). */
|
|
431
|
+
user: AuthUserClient;
|
|
432
|
+
/**
|
|
433
|
+
* Short-lived JWT access token, or empty string when the server is
|
|
434
|
+
* configured for HttpOnly cookie delivery.
|
|
435
|
+
*
|
|
436
|
+
* @warning
|
|
437
|
+
* In bearer mode this value is a credential. Hold it **in memory only**
|
|
438
|
+
* (module-scoped variable, React state, or equivalent). Never write it to
|
|
439
|
+
* `localStorage`, `sessionStorage`, `IndexedDB`, or any persistent store —
|
|
440
|
+
* any same-origin script (including third-party widgets) can read those,
|
|
441
|
+
* so persisting the token turns any XSS into full account takeover for
|
|
442
|
+
* the token's remaining TTL. Never place it in a URL or log output.
|
|
443
|
+
*/
|
|
444
|
+
accessToken: string;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Result returned by the client after a successful platform admin login or refresh.
|
|
448
|
+
*
|
|
449
|
+
* @remarks
|
|
450
|
+
* Uses the `admin` field (vs `user` in {@link AuthResult}) so that callers
|
|
451
|
+
* handling both flows can branch reliably on the discriminating field name.
|
|
452
|
+
*/
|
|
453
|
+
interface PlatformAuthResult {
|
|
454
|
+
/** The authenticated platform administrator (credential and secret fields omitted). */
|
|
455
|
+
admin: AuthPlatformUserClient;
|
|
456
|
+
/**
|
|
457
|
+
* Short-lived JWT access token, or empty string when the server is
|
|
458
|
+
* configured for HttpOnly cookie delivery.
|
|
459
|
+
*
|
|
460
|
+
* @warning
|
|
461
|
+
* In bearer mode this value is a credential. Hold it **in memory only**
|
|
462
|
+
* (module-scoped variable, React state, or equivalent). Never write it to
|
|
463
|
+
* `localStorage`, `sessionStorage`, `IndexedDB`, or any persistent store —
|
|
464
|
+
* any same-origin script (including third-party widgets) can read those,
|
|
465
|
+
* so persisting the token turns any XSS into full account takeover for
|
|
466
|
+
* the token's remaining TTL. Never place it in a URL or log output.
|
|
467
|
+
*/
|
|
468
|
+
accessToken: string;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Result returned when authentication requires MFA verification.
|
|
472
|
+
*
|
|
473
|
+
* The `mfaRequired: true` literal type enables reliable type narrowing in
|
|
474
|
+
* union return types so that callers can branch on a single discriminant:
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```typescript
|
|
478
|
+
* const result = await authClient.login(email, password)
|
|
479
|
+
* if ('mfaRequired' in result) {
|
|
480
|
+
* // result is MfaChallengeResult here
|
|
481
|
+
* } else {
|
|
482
|
+
* // result is AuthResult here
|
|
483
|
+
* }
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
486
|
+
interface MfaChallengeResult {
|
|
487
|
+
/** Literal `true` discriminant — enables type narrowing in union return types. */
|
|
488
|
+
mfaRequired: true;
|
|
489
|
+
/**
|
|
490
|
+
* Short-lived MFA challenge token (default 5-minute TTL).
|
|
491
|
+
* The client exchanges this token plus the user's OTP for a full access token.
|
|
492
|
+
*
|
|
493
|
+
* @remarks
|
|
494
|
+
* Treat this value as a credential. Never log it, persist it to disk, or
|
|
495
|
+
* place it in a URL — exposure enables MFA bypass within the token's
|
|
496
|
+
* lifetime. Hold it only in memory between the login response and the
|
|
497
|
+
* subsequent MFA verification call.
|
|
498
|
+
*/
|
|
499
|
+
mfaTempToken: string;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Discriminated union of all possible login responses.
|
|
503
|
+
*
|
|
504
|
+
* Use this type as the return type of any login method that may either
|
|
505
|
+
* complete the auth flow or escalate to an MFA challenge step.
|
|
506
|
+
*/
|
|
507
|
+
type LoginResult = AuthResult | MfaChallengeResult;
|
|
508
|
+
/**
|
|
509
|
+
* Discriminated union of all possible platform admin login responses.
|
|
510
|
+
*/
|
|
511
|
+
type PlatformLoginResult = PlatformAuthResult | MfaChallengeResult;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Discriminated error-code type carried by `AuthErrorResponse` and
|
|
515
|
+
* `AuthClientError`.
|
|
516
|
+
*
|
|
517
|
+
* Narrows to a known {@link AuthErrorCode} whenever the server emits an
|
|
518
|
+
* `AuthException`; falls back to an arbitrary `string` when the response
|
|
519
|
+
* comes from a non-auth NestJS exception (e.g. a `ValidationPipe` 400)
|
|
520
|
+
* that still ships a `code` field. Consumers can branch exhaustively on
|
|
521
|
+
* `AuthErrorCode` values and treat the `string` arm as the escape hatch.
|
|
522
|
+
*/
|
|
523
|
+
type AuthResponseCode = AuthErrorCode | (string & {});
|
|
524
|
+
/**
|
|
525
|
+
* Shape of the error response body returned by the @bymax-one/nest-auth server.
|
|
526
|
+
*
|
|
527
|
+
* @remarks
|
|
528
|
+
* Mirrors the JSON body emitted by `AuthException` after NestJS's exception
|
|
529
|
+
* filter serializes it. The `code` field uses the `auth.<domain>_<action>`
|
|
530
|
+
* convention defined by `AUTH_ERROR_CODES` and is the recommended branch
|
|
531
|
+
* point for client-side error handling — `message` is meant for end-user
|
|
532
|
+
* display and may be localized in the future.
|
|
533
|
+
*/
|
|
534
|
+
interface AuthErrorResponse {
|
|
535
|
+
/** End-user-facing message (may be localized server-side in future versions). */
|
|
536
|
+
message: string;
|
|
537
|
+
/** HTTP status text (NestJS convention, e.g. `'Unauthorized'`). */
|
|
538
|
+
error: string;
|
|
539
|
+
/** HTTP status code (e.g. `401`, `403`). */
|
|
540
|
+
statusCode: number;
|
|
541
|
+
/**
|
|
542
|
+
* Stable machine-readable error code from `AUTH_ERROR_CODES`
|
|
543
|
+
* (e.g. `'auth.invalid_credentials'`).
|
|
544
|
+
*
|
|
545
|
+
* Optional because a NestJS-level exception thrown outside the auth flow
|
|
546
|
+
* (e.g. a `ValidationPipe` 400) may not carry it.
|
|
547
|
+
*/
|
|
548
|
+
code?: AuthResponseCode;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Error thrown by the auth client when an HTTP response is not 2xx.
|
|
552
|
+
*
|
|
553
|
+
* @remarks
|
|
554
|
+
* Carries the parsed error body alongside the HTTP status so consumers can
|
|
555
|
+
* branch on a stable `code` (preferred) or fall back to `status` for generic
|
|
556
|
+
* cases. Defined as a class (not a plain interface) so `instanceof` checks
|
|
557
|
+
* work in user code.
|
|
558
|
+
*/
|
|
559
|
+
declare class AuthClientError extends Error {
|
|
560
|
+
/** The HTTP status code returned by the server. */
|
|
561
|
+
readonly status: number;
|
|
562
|
+
/**
|
|
563
|
+
* The stable machine-readable error code from `AUTH_ERROR_CODES`,
|
|
564
|
+
* when the server provided one.
|
|
565
|
+
*/
|
|
566
|
+
readonly code: AuthResponseCode | undefined;
|
|
567
|
+
/** The full parsed error body, if the response carried JSON. */
|
|
568
|
+
readonly body: AuthErrorResponse | undefined;
|
|
569
|
+
constructor(message: string, status: number, body?: AuthErrorResponse);
|
|
570
|
+
/**
|
|
571
|
+
* Strips potentially sensitive request echoes from structured-logger output.
|
|
572
|
+
*
|
|
573
|
+
* Some servers reflect submitted DTO fields inside `ValidationPipe` 400
|
|
574
|
+
* responses; without this override, `JSON.stringify(error)` would persist
|
|
575
|
+
* those fields verbatim into application logs. The serialized form keeps
|
|
576
|
+
* only the fields a log consumer actually needs to triage an auth failure.
|
|
577
|
+
*/
|
|
578
|
+
toJSON(): {
|
|
579
|
+
name: string;
|
|
580
|
+
message: string;
|
|
581
|
+
status: number;
|
|
582
|
+
code: AuthResponseCode | undefined;
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Shared JWT payload type definitions for @bymax-one/nest-auth.
|
|
588
|
+
*
|
|
589
|
+
* Redeclared here (instead of re-exported from `../../server`) so that the
|
|
590
|
+
* shared subpath stays free of any server-side runtime imports. The shapes
|
|
591
|
+
* are kept structurally compatible with the server-side definitions in
|
|
592
|
+
* `src/server/interfaces/jwt-payload.interface.ts` — any change made there
|
|
593
|
+
* must be reflected here.
|
|
594
|
+
*
|
|
595
|
+
* @remarks
|
|
596
|
+
* These interfaces describe the shape of a **decoded and verified** token.
|
|
597
|
+
* `iat` and `exp` are populated automatically by the JWT signing layer on
|
|
598
|
+
* the server; they are not part of the signing payload.
|
|
599
|
+
*/
|
|
600
|
+
/**
|
|
601
|
+
* JWT payload for dashboard (tenant) user access tokens.
|
|
602
|
+
*
|
|
603
|
+
* The literal `type: 'dashboard'` discriminant prevents accidental
|
|
604
|
+
* acceptance of platform tokens in dashboard-scoped checks and vice-versa.
|
|
605
|
+
*/
|
|
606
|
+
interface DashboardJwtPayload {
|
|
607
|
+
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
608
|
+
jti: string;
|
|
609
|
+
/** Subject — the authenticated user's internal identifier. */
|
|
610
|
+
sub: string;
|
|
611
|
+
/** Tenant the user belongs to; used for data isolation. */
|
|
612
|
+
tenantId: string;
|
|
613
|
+
/** User's assigned role within the tenant (e.g. `'admin'`, `'member'`). */
|
|
614
|
+
role: string;
|
|
615
|
+
/** Discriminant literal — always `'dashboard'`. Prevents token type confusion. */
|
|
616
|
+
type: 'dashboard';
|
|
617
|
+
/** Snapshot of the user's account status at token issuance time. */
|
|
618
|
+
status: string;
|
|
619
|
+
/** Whether MFA (TOTP) is currently enabled on the user's account. */
|
|
620
|
+
mfaEnabled: boolean;
|
|
621
|
+
/** Whether the user completed MFA verification before this token was issued. */
|
|
622
|
+
mfaVerified: boolean;
|
|
623
|
+
/** Issued-at timestamp (Unix seconds). */
|
|
624
|
+
iat: number;
|
|
625
|
+
/** Expiration timestamp (Unix seconds). */
|
|
626
|
+
exp: number;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* JWT payload for platform administrator access tokens.
|
|
630
|
+
*
|
|
631
|
+
* Platform admins operate outside tenant boundaries, so this payload omits
|
|
632
|
+
* `tenantId` and `status`. The literal `type: 'platform'` discriminant
|
|
633
|
+
* ensures platform tokens are never accepted by dashboard-scoped checks.
|
|
634
|
+
*/
|
|
635
|
+
interface PlatformJwtPayload {
|
|
636
|
+
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
637
|
+
jti: string;
|
|
638
|
+
/** Subject — the platform administrator's internal identifier. */
|
|
639
|
+
sub: string;
|
|
640
|
+
/** Administrator's platform-level role (e.g. `'super_admin'`, `'support'`). */
|
|
641
|
+
role: string;
|
|
642
|
+
/** Discriminant literal — always `'platform'`. Prevents token type confusion. */
|
|
643
|
+
type: 'platform';
|
|
644
|
+
/** Whether MFA (TOTP) is currently enabled on the admin's account. */
|
|
645
|
+
mfaEnabled: boolean;
|
|
646
|
+
/** Whether the admin completed MFA verification before this token was issued. */
|
|
647
|
+
mfaVerified: boolean;
|
|
648
|
+
/** Issued-at timestamp (Unix seconds). */
|
|
649
|
+
iat: number;
|
|
650
|
+
/** Expiration timestamp (Unix seconds). */
|
|
651
|
+
exp: number;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Short-lived JWT payload issued during an MFA challenge step.
|
|
655
|
+
*
|
|
656
|
+
* Grants no resource access on its own — exchanged for a full
|
|
657
|
+
* {@link DashboardJwtPayload} or {@link PlatformJwtPayload} token after the
|
|
658
|
+
* user supplies a valid OTP. Default lifetime is 5 minutes.
|
|
659
|
+
*/
|
|
660
|
+
interface MfaTempPayload {
|
|
661
|
+
/** Unique JWT ID (UUID v4) for single-use enforcement and revocation. */
|
|
662
|
+
jti: string;
|
|
663
|
+
/** Subject — the user or admin identifier pending MFA completion. */
|
|
664
|
+
sub: string;
|
|
665
|
+
/** Discriminant literal — always `'mfa_challenge'`. */
|
|
666
|
+
type: 'mfa_challenge';
|
|
667
|
+
/** Indicates which authentication context triggered the MFA challenge. */
|
|
668
|
+
context: 'dashboard' | 'platform';
|
|
669
|
+
/** Issued-at timestamp (Unix seconds). */
|
|
670
|
+
iat: number;
|
|
671
|
+
/** Expiration timestamp (Unix seconds). */
|
|
672
|
+
exp: number;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Discriminated union of every JWT payload type accepted by the auth system.
|
|
676
|
+
*/
|
|
677
|
+
type AuthJwtPayload = DashboardJwtPayload | PlatformJwtPayload | MfaTempPayload;
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Shared cookie- and configuration-related type definitions.
|
|
681
|
+
*
|
|
682
|
+
* These types describe naming and shape contracts shared between server and
|
|
683
|
+
* client. They contain no runtime values — pure type-only declarations that
|
|
684
|
+
* compile away.
|
|
685
|
+
*/
|
|
686
|
+
/**
|
|
687
|
+
* Set of cookie names used by the auth system.
|
|
688
|
+
*
|
|
689
|
+
* The defaults live in `AUTH_ACCESS_COOKIE_NAME`, `AUTH_REFRESH_COOKIE_NAME`,
|
|
690
|
+
* and `AUTH_HAS_SESSION_COOKIE_NAME`. Consumers that override the cookie
|
|
691
|
+
* names on the server (via `BymaxAuthModule.forRoot({ cookies: { ... } })`)
|
|
692
|
+
* must surface the same names to the client through this shape so both
|
|
693
|
+
* sides stay in sync.
|
|
694
|
+
*/
|
|
695
|
+
interface AuthCookieNames {
|
|
696
|
+
/**
|
|
697
|
+
* Name of the HttpOnly cookie that carries the JWT access token.
|
|
698
|
+
* Default: `'access_token'`.
|
|
699
|
+
*/
|
|
700
|
+
accessTokenName: string;
|
|
701
|
+
/**
|
|
702
|
+
* Name of the HttpOnly cookie that carries the opaque refresh token.
|
|
703
|
+
* Default: `'refresh_token'`.
|
|
704
|
+
*/
|
|
705
|
+
refreshTokenName: string;
|
|
706
|
+
/**
|
|
707
|
+
* Name of the non-HttpOnly cookie used as a hint to the frontend that a
|
|
708
|
+
* session likely exists (the actual access token is HttpOnly and not
|
|
709
|
+
* readable by JavaScript). Default: `'has_session'`.
|
|
710
|
+
*/
|
|
711
|
+
sessionSignalName: string;
|
|
712
|
+
/**
|
|
713
|
+
* Path scope of the refresh-token cookie. Restricting it to the auth
|
|
714
|
+
* route prefix prevents the cookie from being sent on every request.
|
|
715
|
+
* Default: `'/auth'`.
|
|
716
|
+
*/
|
|
717
|
+
refreshCookiePath: string;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Discriminated authentication context.
|
|
721
|
+
*
|
|
722
|
+
* Used by client code that may operate against either the dashboard or the
|
|
723
|
+
* platform admin endpoints. Modeled as a string literal union so that both
|
|
724
|
+
* library and consumer code can branch exhaustively.
|
|
725
|
+
*/
|
|
726
|
+
type AuthContextKind = 'dashboard' | 'platform';
|
|
727
|
+
/**
|
|
728
|
+
* Token delivery strategy in use on the server.
|
|
729
|
+
*
|
|
730
|
+
* - `cookie`: tokens are set as HttpOnly cookies; the client never reads them.
|
|
731
|
+
* - `bearer`: tokens are returned in the response body; the client sends
|
|
732
|
+
* them in the `Authorization: Bearer <token>` header.
|
|
733
|
+
*/
|
|
734
|
+
type TokenDeliveryMode = 'cookie' | 'bearer';
|
|
735
|
+
|
|
736
|
+
export { AUTH_ACCESS_COOKIE_NAME, AUTH_DASHBOARD_ROUTES, AUTH_ERROR_CODES, AUTH_HAS_SESSION_COOKIE_NAME, AUTH_INVITATION_ROUTES, AUTH_MFA_ROUTES, AUTH_PASSWORD_ROUTES, AUTH_PLATFORM_ROUTES, AUTH_PROXY_ROUTES, AUTH_REFRESH_COOKIE_NAME, AUTH_REFRESH_COOKIE_PATH, AUTH_REFRESH_SKIP_PATH_SUFFIXES, AUTH_ROUTES, AUTH_SESSION_ROUTES, AuthClientError, type AuthContextKind, type AuthCookieNames, type AuthErrorCode, type AuthErrorResponse, type AuthJwtPayload, type AuthPlatformUserClient, type AuthResponseCode, type AuthResult, type AuthUserClient, type DashboardJwtPayload, type LoginResult, type MfaChallengeResult, type MfaTempPayload, type PlatformAuthResult, type PlatformJwtPayload, type PlatformLoginResult, type TokenDeliveryMode, buildAuthRefreshSkipSuffixes };
|