@fonderie/auth 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -2,8 +2,8 @@ export { IMfaChallenge, ISession, IUser } from './types.cjs';
2
2
  import { IFonderieModule, IFonderieApp } from '@fonderie/core';
3
3
  import { IStoreAdapter } from '@fonderie/store';
4
4
  import { EventBus } from '@fonderie/events';
5
- import { I as IAuthConfig } from './session-CLD1WPJs.cjs';
6
- export { A as AUTH_CONFIG_KEYS, a as AuthMessageKey, b as IAuthRuntimeConfig, c as IAuthSecrets, M as MESSAGE_KEYS, w as withSession } from './session-CLD1WPJs.cjs';
5
+ import { I as IAuthConfig } from './session-BGjjwz5_.cjs';
6
+ export { A as AUTH_CONFIG_KEYS, a as AuthMessageKey, b as IAuthRuntimeConfig, c as IAuthSecrets, M as MESSAGE_KEYS, w as withSession } from './session-BGjjwz5_.cjs';
7
7
  export { IUserDTO, toUserDTO } from './dtos/user.cjs';
8
8
  export { requireAuth, validate } from '@fonderie/core/middlewares';
9
9
  import { z } from 'zod';
package/dist/index.d.ts CHANGED
@@ -2,8 +2,8 @@ export { IMfaChallenge, ISession, IUser } from './types.js';
2
2
  import { IFonderieModule, IFonderieApp } from '@fonderie/core';
3
3
  import { IStoreAdapter } from '@fonderie/store';
4
4
  import { EventBus } from '@fonderie/events';
5
- import { I as IAuthConfig } from './session-CLD1WPJs.js';
6
- export { A as AUTH_CONFIG_KEYS, a as AuthMessageKey, b as IAuthRuntimeConfig, c as IAuthSecrets, M as MESSAGE_KEYS, w as withSession } from './session-CLD1WPJs.js';
5
+ import { I as IAuthConfig } from './session-BGjjwz5_.js';
6
+ export { A as AUTH_CONFIG_KEYS, a as AuthMessageKey, b as IAuthRuntimeConfig, c as IAuthSecrets, M as MESSAGE_KEYS, w as withSession } from './session-BGjjwz5_.js';
7
7
  export { IUserDTO, toUserDTO } from './dtos/user.js';
8
8
  export { requireAuth, validate } from '@fonderie/core/middlewares';
9
9
  import { z } from 'zod';
package/dist/index.js CHANGED
@@ -113,6 +113,26 @@ var changePasswordSchema = z.object({
113
113
  });
114
114
  var mfaTokenSchema = z.object({ token: z.string().trim().min(6).max(64) });
115
115
 
116
+ // src/services/cookies.ts
117
+ function secureAttr(config) {
118
+ const secure = config.secureCookies ?? process.env["NODE_ENV"] === "production";
119
+ return secure ? "; Secure" : "";
120
+ }
121
+ function tokenPairCookies(accessToken, refreshToken, config) {
122
+ const secure = secureAttr(config);
123
+ return [
124
+ `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/${secure}`,
125
+ `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh${secure}`
126
+ ].join(", ");
127
+ }
128
+ function clearedTokenCookies(config) {
129
+ const secure = secureAttr(config);
130
+ return [
131
+ `access_token=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0${secure}`,
132
+ `refresh_token=; HttpOnly; SameSite=Strict; Path=/auth/refresh; Max-Age=0${secure}`
133
+ ].join(", ");
134
+ }
135
+
116
136
  // src/controllers/mfa.controller.ts
117
137
  import QRCode from "qrcode";
118
138
  import { setApiResponse as setApiResponse2, HTTP as HTTP2 } from "@fonderie/core";
@@ -697,10 +717,7 @@ function mfaController(store, config, issuer, bus) {
697
717
  {
698
718
  status: 200,
699
719
  headers: {
700
- "Set-Cookie": [
701
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
702
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
703
- ].join(", ")
720
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
704
721
  }
705
722
  }
706
723
  );
@@ -1034,10 +1051,7 @@ function authController(store, config, bus) {
1034
1051
  {
1035
1052
  status: 201,
1036
1053
  headers: {
1037
- "Set-Cookie": [
1038
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1039
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1040
- ].join(", ")
1054
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1041
1055
  }
1042
1056
  }
1043
1057
  );
@@ -1099,10 +1113,7 @@ function authController(store, config, bus) {
1099
1113
  {
1100
1114
  status: 202,
1101
1115
  headers: {
1102
- "Set-Cookie": [
1103
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1104
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1105
- ].join(", ")
1116
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1106
1117
  }
1107
1118
  }
1108
1119
  );
@@ -1161,10 +1172,7 @@ function authController(store, config, bus) {
1161
1172
  {
1162
1173
  status: 200,
1163
1174
  headers: {
1164
- "Set-Cookie": [
1165
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1166
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1167
- ].join(", ")
1175
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1168
1176
  }
1169
1177
  }
1170
1178
  );
@@ -1207,10 +1215,7 @@ function authController(store, config, bus) {
1207
1215
  {
1208
1216
  status: 202,
1209
1217
  headers: {
1210
- "Set-Cookie": [
1211
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1212
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1213
- ].join(", ")
1218
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1214
1219
  }
1215
1220
  }
1216
1221
  );
@@ -1231,10 +1236,7 @@ function authController(store, config, bus) {
1231
1236
  {
1232
1237
  status: 200,
1233
1238
  headers: {
1234
- "Set-Cookie": [
1235
- "access_token=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0",
1236
- "refresh_token=; HttpOnly; SameSite=Strict; Path=/auth/refresh; Max-Age=0"
1237
- ].join(", ")
1239
+ "Set-Cookie": clearedTokenCookies(config)
1238
1240
  }
1239
1241
  }
1240
1242
  );
@@ -1275,10 +1277,7 @@ function authController(store, config, bus) {
1275
1277
  {
1276
1278
  status: 200,
1277
1279
  headers: {
1278
- "Set-Cookie": [
1279
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1280
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1281
- ].join(", ")
1280
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1282
1281
  }
1283
1282
  }
1284
1283
  );
@@ -1306,12 +1305,9 @@ function authController(store, config, bus) {
1306
1305
  const remaining = checkCooldown(await passwordReset.findLastSentAt(user.id), cooldown);
1307
1306
  if (remaining > 0) {
1308
1307
  return setApiResponse3(
1309
- HTTP3.TOO_MANY_REQUESTS,
1310
- "VERIFICATION_COOLDOWN",
1311
- "Please wait before requesting a new password reset code.",
1312
- {
1313
- retryAfter: Math.ceil(remaining / 1e3)
1314
- }
1308
+ HTTP3.OK,
1309
+ "PASSWORD_RESET_EMAIL_SENT",
1310
+ "Password reset email sent (if account exists)."
1315
1311
  );
1316
1312
  }
1317
1313
  const pin = randomInt(1e5, 1e6).toString();
@@ -1430,10 +1426,7 @@ function authController(store, config, bus) {
1430
1426
  {
1431
1427
  status: 200,
1432
1428
  headers: {
1433
- "Set-Cookie": [
1434
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1435
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1436
- ].join(", ")
1429
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1437
1430
  }
1438
1431
  }
1439
1432
  );
@@ -1550,7 +1543,7 @@ function normalizePhone2(phone2) {
1550
1543
  function isValidPhone2(phone2) {
1551
1544
  return typeof phone2 === "string" && /^\+?[1-9]\d{6,14}$/.test(normalizePhone2(phone2));
1552
1545
  }
1553
- function userController(store, bus) {
1546
+ function userController(store, config, bus) {
1554
1547
  const users = new UserModel(store);
1555
1548
  const emailVerif = new EmailVerificationModel(store);
1556
1549
  const phoneVerif = new PhoneVerificationModel(store);
@@ -1739,10 +1732,7 @@ function userController(store, bus) {
1739
1732
  {
1740
1733
  status: 200,
1741
1734
  headers: {
1742
- "Set-Cookie": [
1743
- "access_token=; HttpOnly; SameSite=Strict; Path=/; Max-Age=0",
1744
- "refresh_token=; HttpOnly; SameSite=Strict; Path=/auth/refresh; Max-Age=0"
1745
- ].join(", ")
1735
+ "Set-Cookie": clearedTokenCookies(config)
1746
1736
  }
1747
1737
  }
1748
1738
  );
@@ -1846,10 +1836,7 @@ function oauthController(store, config) {
1846
1836
  {
1847
1837
  status: 200,
1848
1838
  headers: {
1849
- "Set-Cookie": [
1850
- `access_token=${accessToken}; HttpOnly; SameSite=Strict; Path=/`,
1851
- `refresh_token=${refreshToken}; HttpOnly; SameSite=Strict; Path=/auth/refresh`
1852
- ].join(", ")
1839
+ "Set-Cookie": tokenPairCookies(accessToken, refreshToken, config)
1853
1840
  }
1854
1841
  }
1855
1842
  );
@@ -1859,7 +1846,7 @@ function oauthController(store, config) {
1859
1846
 
1860
1847
  // src/routes.ts
1861
1848
  function buildAuthRoutes(store, config, bus) {
1862
- const user = userController(store, bus);
1849
+ const user = userController(store, config, bus);
1863
1850
  const auth = authController(store, config, bus);
1864
1851
  const oauth = oauthController(store, config);
1865
1852
  const mfa = mfaController(store, config, config.appName ?? "Fonderie", bus);