@bymax-one/nest-auth 1.0.3 → 1.0.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 +13 -0
- package/dist/server/index.cjs +42 -6
- package/dist/server/index.d.cts +26 -0
- package/dist/server/index.d.ts +26 -0
- package/dist/server/index.mjs +42 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.0.4] - 2026-05-25
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **`oauth.successRedirectUrl?: string` configuration option** ([`src/server/interfaces/auth-module-options.interface.ts`](src/server/interfaces/auth-module-options.interface.ts), [`src/server/oauth/oauth.controller.ts`](src/server/oauth/oauth.controller.ts)). When set, `GET /auth/oauth/:provider/callback` issues a `302` redirect to the configured URL after delivering tokens, instead of returning the JSON body that API/SPA consumers expect. Cookies are still set on the same response — the destination page lands fully authenticated. This closes a UX gap that left browser users on the JSON payload after a successful OAuth round-trip (every consumer was forced to reimplement the OAuth controller just to get a redirect). The option is opt-in and the legacy JSON-body contract is preserved when it is omitted. Aligns the lib with `passport.successRedirect`, `next-auth.callbackUrl`, and `auth0.returnTo`.
|
|
15
|
+
|
|
16
|
+
Validated at startup by [`src/server/config/resolved-options.ts`](src/server/config/resolved-options.ts) with three rules:
|
|
17
|
+
1. Must be a non-empty string when set.
|
|
18
|
+
2. Must use `https://` or be a same-origin path (`/...`) in production — HTTP rejected so the post-callback leg cannot strip cookie `Secure` guarantees.
|
|
19
|
+
3. Requires `tokenDelivery: 'cookie'` or `'both'` — `bearer` is rejected because the 302 would discard the JSON body that carries the access token.
|
|
20
|
+
|
|
21
|
+
Covered by 3 new unit tests in [`src/server/oauth/oauth.controller.spec.ts`](src/server/oauth/oauth.controller.spec.ts), 7 new validator tests in [`src/server/config/resolved-options.spec.ts`](src/server/config/resolved-options.spec.ts), and 2 new e2e scenarios in [`test/e2e/oauth-flow.e2e-spec.ts`](test/e2e/oauth-flow.e2e-spec.ts) (cookie-attached 302 to the configured URL + Set-Cookie headers on the redirect response).
|
|
22
|
+
|
|
10
23
|
## [1.0.3] - 2026-05-25
|
|
11
24
|
|
|
12
25
|
### Fixed
|
package/dist/server/index.cjs
CHANGED
|
@@ -142,6 +142,7 @@ function resolveOptions(userOptions) {
|
|
|
142
142
|
validatePasswordResetOtpLength(userOptions.passwordReset);
|
|
143
143
|
validatePasswordCostFactor(userOptions.password);
|
|
144
144
|
validateOAuthProviders(userOptions.oauth);
|
|
145
|
+
validateOAuthSuccessRedirectUrl(userOptions);
|
|
145
146
|
validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
|
|
146
147
|
validateRefreshGraceWindow(userOptions.jwt);
|
|
147
148
|
const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
|
|
@@ -304,7 +305,9 @@ var REQUIRED_OAUTH_FIELDS = [
|
|
|
304
305
|
];
|
|
305
306
|
function validateOAuthProviders(oauth) {
|
|
306
307
|
if (!oauth) return;
|
|
307
|
-
for (const [
|
|
308
|
+
for (const [providerOrField, rawConfig] of Object.entries(oauth)) {
|
|
309
|
+
if (providerOrField === "successRedirectUrl") continue;
|
|
310
|
+
const provider = providerOrField;
|
|
308
311
|
const config = rawConfig;
|
|
309
312
|
for (const field of REQUIRED_OAUTH_FIELDS) {
|
|
310
313
|
if (!config[field]) {
|
|
@@ -318,6 +321,22 @@ function validateOAuthProviders(oauth) {
|
|
|
318
321
|
}
|
|
319
322
|
}
|
|
320
323
|
__name(validateOAuthProviders, "validateOAuthProviders");
|
|
324
|
+
function validateOAuthSuccessRedirectUrl(userOptions) {
|
|
325
|
+
const url = userOptions.oauth?.successRedirectUrl;
|
|
326
|
+
if (url === void 0) return;
|
|
327
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
328
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must be a non-empty string when set.`);
|
|
329
|
+
}
|
|
330
|
+
const isProduction = process.env["NODE_ENV"] === "production";
|
|
331
|
+
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
332
|
+
if (isProduction && !isSafe) {
|
|
333
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (got: '${url}').`);
|
|
334
|
+
}
|
|
335
|
+
if (userOptions.tokenDelivery === "bearer") {
|
|
336
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl is set but tokenDelivery is 'bearer'. A redirect discards the JSON response body, so the access token would never reach the client. Use tokenDelivery: 'cookie' or 'both' when configuring a successRedirectUrl.`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
__name(validateOAuthSuccessRedirectUrl, "validateOAuthSuccessRedirectUrl");
|
|
321
340
|
function validateRefreshCookiePath(routePrefix, cookies) {
|
|
322
341
|
const prefix = routePrefix ?? DEFAULT_OPTIONS.routePrefix;
|
|
323
342
|
if (prefix !== "auth" && !cookies?.refreshCookiePath) {
|
|
@@ -7285,9 +7304,11 @@ var OAuthController = class {
|
|
|
7285
7304
|
}
|
|
7286
7305
|
oauthService;
|
|
7287
7306
|
tokenDelivery;
|
|
7288
|
-
|
|
7307
|
+
options;
|
|
7308
|
+
constructor(oauthService, tokenDelivery, options) {
|
|
7289
7309
|
this.oauthService = oauthService;
|
|
7290
7310
|
this.tokenDelivery = tokenDelivery;
|
|
7311
|
+
this.options = options;
|
|
7291
7312
|
}
|
|
7292
7313
|
// ---------------------------------------------------------------------------
|
|
7293
7314
|
// GET /oauth/:provider
|
|
@@ -7321,17 +7342,30 @@ var OAuthController = class {
|
|
|
7321
7342
|
* or oversized values are rejected before reaching the service layer or the
|
|
7322
7343
|
* token-exchange HTTP call.
|
|
7323
7344
|
*
|
|
7345
|
+
* If `oauth.successRedirectUrl` is configured, the response is a `302` redirect
|
|
7346
|
+
* to that URL with cookies already set in the same response. This is the
|
|
7347
|
+
* standard browser OAuth flow — without it the browser lands on the JSON
|
|
7348
|
+
* payload returned to API consumers. The redirect is issued by calling
|
|
7349
|
+
* `res.redirect()` and returning `undefined`, which Nest passes through
|
|
7350
|
+
* unchanged (passthrough mode is active).
|
|
7351
|
+
*
|
|
7324
7352
|
* @param provider - Provider name from the URL path (e.g. `'google'`).
|
|
7325
7353
|
* @param query - Validated query parameters (contains `code` and `state`).
|
|
7326
7354
|
* @param req - Incoming request (IP, User-Agent, and cookie context for token delivery).
|
|
7327
|
-
* @param res - Express response in passthrough mode (used for cookie delivery).
|
|
7328
|
-
* @returns
|
|
7355
|
+
* @param res - Express response in passthrough mode (used for cookie delivery and the optional redirect).
|
|
7356
|
+
* @returns The auth response shaped by `tokenDelivery`, or `undefined` when a `successRedirectUrl` redirect was issued.
|
|
7329
7357
|
*/
|
|
7330
7358
|
async callback(provider, query, req, res) {
|
|
7331
7359
|
const ip = (req.ip ?? "").slice(0, 64);
|
|
7332
7360
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
7333
7361
|
const result = await this.oauthService.handleCallback(provider, query.code, query.state, ip, userAgent, req.headers);
|
|
7334
|
-
|
|
7362
|
+
const body = this.tokenDelivery.deliverAuthResponse(res, result, req);
|
|
7363
|
+
const redirectUrl = this.options.oauth?.successRedirectUrl;
|
|
7364
|
+
if (redirectUrl !== void 0) {
|
|
7365
|
+
res.redirect(redirectUrl);
|
|
7366
|
+
return void 0;
|
|
7367
|
+
}
|
|
7368
|
+
return body;
|
|
7335
7369
|
}
|
|
7336
7370
|
};
|
|
7337
7371
|
_ts_decorate43([
|
|
@@ -7378,10 +7412,12 @@ OAuthController = _ts_decorate43([
|
|
|
7378
7412
|
forbidNonWhitelisted: true,
|
|
7379
7413
|
forbidUnknownValues: true
|
|
7380
7414
|
})),
|
|
7415
|
+
_ts_param24(2, common.Inject(BYMAX_AUTH_OPTIONS)),
|
|
7381
7416
|
_ts_metadata42("design:type", Function),
|
|
7382
7417
|
_ts_metadata42("design:paramtypes", [
|
|
7383
7418
|
typeof exports.OAuthService === "undefined" ? Object : exports.OAuthService,
|
|
7384
|
-
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService
|
|
7419
|
+
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService,
|
|
7420
|
+
typeof ResolvedOptions === "undefined" ? Object : ResolvedOptions
|
|
7385
7421
|
])
|
|
7386
7422
|
], OAuthController);
|
|
7387
7423
|
|
package/dist/server/index.d.cts
CHANGED
|
@@ -626,6 +626,32 @@ interface BymaxAuthModuleOptions {
|
|
|
626
626
|
* Each provider block is optional — only configured providers are registered.
|
|
627
627
|
*/
|
|
628
628
|
oauth?: {
|
|
629
|
+
/**
|
|
630
|
+
* URL the browser is redirected to after a successful OAuth callback.
|
|
631
|
+
*
|
|
632
|
+
* When set, the OAuth callback endpoint issues a `302` to this URL after
|
|
633
|
+
* delivering tokens (cookies are still set via the same response). When
|
|
634
|
+
* omitted, the callback returns a JSON body — appropriate for API/SPA
|
|
635
|
+
* consumers that XHR/fetch the callback URL, but it leaves browser users
|
|
636
|
+
* staring at a JSON page. Set this for any consumer where the OAuth flow
|
|
637
|
+
* is initiated by a full-page browser navigation.
|
|
638
|
+
*
|
|
639
|
+
* Requires `tokenDelivery: 'cookie'` or `'both'` — bearer-only delivery is
|
|
640
|
+
* incompatible with a redirect because the access token would be discarded
|
|
641
|
+
* in the redirect response body. The module throws at startup if the
|
|
642
|
+
* combination is misconfigured.
|
|
643
|
+
*
|
|
644
|
+
* In production (`NODE_ENV === 'production'`), the URL must use HTTPS or
|
|
645
|
+
* be a relative path (starts with `/`) — same posture as `callbackUrl`.
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```typescript
|
|
649
|
+
* successRedirectUrl: 'https://app.example.com/dashboard'
|
|
650
|
+
* // or, for same-origin deployments:
|
|
651
|
+
* successRedirectUrl: '/dashboard'
|
|
652
|
+
* ```
|
|
653
|
+
*/
|
|
654
|
+
successRedirectUrl?: string;
|
|
629
655
|
/**
|
|
630
656
|
* Google OAuth 2.0 configuration.
|
|
631
657
|
* All three fields are required to enable Google login.
|
package/dist/server/index.d.ts
CHANGED
|
@@ -626,6 +626,32 @@ interface BymaxAuthModuleOptions {
|
|
|
626
626
|
* Each provider block is optional — only configured providers are registered.
|
|
627
627
|
*/
|
|
628
628
|
oauth?: {
|
|
629
|
+
/**
|
|
630
|
+
* URL the browser is redirected to after a successful OAuth callback.
|
|
631
|
+
*
|
|
632
|
+
* When set, the OAuth callback endpoint issues a `302` to this URL after
|
|
633
|
+
* delivering tokens (cookies are still set via the same response). When
|
|
634
|
+
* omitted, the callback returns a JSON body — appropriate for API/SPA
|
|
635
|
+
* consumers that XHR/fetch the callback URL, but it leaves browser users
|
|
636
|
+
* staring at a JSON page. Set this for any consumer where the OAuth flow
|
|
637
|
+
* is initiated by a full-page browser navigation.
|
|
638
|
+
*
|
|
639
|
+
* Requires `tokenDelivery: 'cookie'` or `'both'` — bearer-only delivery is
|
|
640
|
+
* incompatible with a redirect because the access token would be discarded
|
|
641
|
+
* in the redirect response body. The module throws at startup if the
|
|
642
|
+
* combination is misconfigured.
|
|
643
|
+
*
|
|
644
|
+
* In production (`NODE_ENV === 'production'`), the URL must use HTTPS or
|
|
645
|
+
* be a relative path (starts with `/`) — same posture as `callbackUrl`.
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```typescript
|
|
649
|
+
* successRedirectUrl: 'https://app.example.com/dashboard'
|
|
650
|
+
* // or, for same-origin deployments:
|
|
651
|
+
* successRedirectUrl: '/dashboard'
|
|
652
|
+
* ```
|
|
653
|
+
*/
|
|
654
|
+
successRedirectUrl?: string;
|
|
629
655
|
/**
|
|
630
656
|
* Google OAuth 2.0 configuration.
|
|
631
657
|
* All three fields are required to enable Google login.
|
package/dist/server/index.mjs
CHANGED
|
@@ -140,6 +140,7 @@ function resolveOptions(userOptions) {
|
|
|
140
140
|
validatePasswordResetOtpLength(userOptions.passwordReset);
|
|
141
141
|
validatePasswordCostFactor(userOptions.password);
|
|
142
142
|
validateOAuthProviders(userOptions.oauth);
|
|
143
|
+
validateOAuthSuccessRedirectUrl(userOptions);
|
|
143
144
|
validateRefreshCookiePath(userOptions.routePrefix, userOptions.cookies);
|
|
144
145
|
validateRefreshGraceWindow(userOptions.jwt);
|
|
145
146
|
const { mfa: _mfa, ...userOptionsWithoutMfa } = userOptions;
|
|
@@ -302,7 +303,9 @@ var REQUIRED_OAUTH_FIELDS = [
|
|
|
302
303
|
];
|
|
303
304
|
function validateOAuthProviders(oauth) {
|
|
304
305
|
if (!oauth) return;
|
|
305
|
-
for (const [
|
|
306
|
+
for (const [providerOrField, rawConfig] of Object.entries(oauth)) {
|
|
307
|
+
if (providerOrField === "successRedirectUrl") continue;
|
|
308
|
+
const provider = providerOrField;
|
|
306
309
|
const config = rawConfig;
|
|
307
310
|
for (const field of REQUIRED_OAUTH_FIELDS) {
|
|
308
311
|
if (!config[field]) {
|
|
@@ -316,6 +319,22 @@ function validateOAuthProviders(oauth) {
|
|
|
316
319
|
}
|
|
317
320
|
}
|
|
318
321
|
__name(validateOAuthProviders, "validateOAuthProviders");
|
|
322
|
+
function validateOAuthSuccessRedirectUrl(userOptions) {
|
|
323
|
+
const url = userOptions.oauth?.successRedirectUrl;
|
|
324
|
+
if (url === void 0) return;
|
|
325
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
326
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must be a non-empty string when set.`);
|
|
327
|
+
}
|
|
328
|
+
const isProduction = process.env["NODE_ENV"] === "production";
|
|
329
|
+
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
330
|
+
if (isProduction && !isSafe) {
|
|
331
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (got: '${url}').`);
|
|
332
|
+
}
|
|
333
|
+
if (userOptions.tokenDelivery === "bearer") {
|
|
334
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl is set but tokenDelivery is 'bearer'. A redirect discards the JSON response body, so the access token would never reach the client. Use tokenDelivery: 'cookie' or 'both' when configuring a successRedirectUrl.`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
__name(validateOAuthSuccessRedirectUrl, "validateOAuthSuccessRedirectUrl");
|
|
319
338
|
function validateRefreshCookiePath(routePrefix, cookies) {
|
|
320
339
|
const prefix = routePrefix ?? DEFAULT_OPTIONS.routePrefix;
|
|
321
340
|
if (prefix !== "auth" && !cookies?.refreshCookiePath) {
|
|
@@ -7283,9 +7302,11 @@ var OAuthController = class {
|
|
|
7283
7302
|
}
|
|
7284
7303
|
oauthService;
|
|
7285
7304
|
tokenDelivery;
|
|
7286
|
-
|
|
7305
|
+
options;
|
|
7306
|
+
constructor(oauthService, tokenDelivery, options) {
|
|
7287
7307
|
this.oauthService = oauthService;
|
|
7288
7308
|
this.tokenDelivery = tokenDelivery;
|
|
7309
|
+
this.options = options;
|
|
7289
7310
|
}
|
|
7290
7311
|
// ---------------------------------------------------------------------------
|
|
7291
7312
|
// GET /oauth/:provider
|
|
@@ -7319,17 +7340,30 @@ var OAuthController = class {
|
|
|
7319
7340
|
* or oversized values are rejected before reaching the service layer or the
|
|
7320
7341
|
* token-exchange HTTP call.
|
|
7321
7342
|
*
|
|
7343
|
+
* If `oauth.successRedirectUrl` is configured, the response is a `302` redirect
|
|
7344
|
+
* to that URL with cookies already set in the same response. This is the
|
|
7345
|
+
* standard browser OAuth flow — without it the browser lands on the JSON
|
|
7346
|
+
* payload returned to API consumers. The redirect is issued by calling
|
|
7347
|
+
* `res.redirect()` and returning `undefined`, which Nest passes through
|
|
7348
|
+
* unchanged (passthrough mode is active).
|
|
7349
|
+
*
|
|
7322
7350
|
* @param provider - Provider name from the URL path (e.g. `'google'`).
|
|
7323
7351
|
* @param query - Validated query parameters (contains `code` and `state`).
|
|
7324
7352
|
* @param req - Incoming request (IP, User-Agent, and cookie context for token delivery).
|
|
7325
|
-
* @param res - Express response in passthrough mode (used for cookie delivery).
|
|
7326
|
-
* @returns
|
|
7353
|
+
* @param res - Express response in passthrough mode (used for cookie delivery and the optional redirect).
|
|
7354
|
+
* @returns The auth response shaped by `tokenDelivery`, or `undefined` when a `successRedirectUrl` redirect was issued.
|
|
7327
7355
|
*/
|
|
7328
7356
|
async callback(provider, query, req, res) {
|
|
7329
7357
|
const ip = (req.ip ?? "").slice(0, 64);
|
|
7330
7358
|
const userAgent = String(req.headers["user-agent"] ?? "").slice(0, 512);
|
|
7331
7359
|
const result = await this.oauthService.handleCallback(provider, query.code, query.state, ip, userAgent, req.headers);
|
|
7332
|
-
|
|
7360
|
+
const body = this.tokenDelivery.deliverAuthResponse(res, result, req);
|
|
7361
|
+
const redirectUrl = this.options.oauth?.successRedirectUrl;
|
|
7362
|
+
if (redirectUrl !== void 0) {
|
|
7363
|
+
res.redirect(redirectUrl);
|
|
7364
|
+
return void 0;
|
|
7365
|
+
}
|
|
7366
|
+
return body;
|
|
7333
7367
|
}
|
|
7334
7368
|
};
|
|
7335
7369
|
_ts_decorate43([
|
|
@@ -7376,10 +7410,12 @@ OAuthController = _ts_decorate43([
|
|
|
7376
7410
|
forbidNonWhitelisted: true,
|
|
7377
7411
|
forbidUnknownValues: true
|
|
7378
7412
|
})),
|
|
7413
|
+
_ts_param24(2, Inject(BYMAX_AUTH_OPTIONS)),
|
|
7379
7414
|
_ts_metadata42("design:type", Function),
|
|
7380
7415
|
_ts_metadata42("design:paramtypes", [
|
|
7381
7416
|
typeof OAuthService === "undefined" ? Object : OAuthService,
|
|
7382
|
-
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService
|
|
7417
|
+
typeof TokenDeliveryService === "undefined" ? Object : TokenDeliveryService,
|
|
7418
|
+
typeof ResolvedOptions === "undefined" ? Object : ResolvedOptions
|
|
7383
7419
|
])
|
|
7384
7420
|
], OAuthController);
|
|
7385
7421
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-auth",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Full-stack authentication and authorization package for NestJS, React and Next.js — JWT, MFA, OAuth, sessions, multi-tenant SaaS ready",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|