arkos 1.0.19-beta → 1.0.20-beta

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.
Files changed (108) hide show
  1. package/dist/cjs/app.js +1 -1
  2. package/dist/cjs/app.js.map +1 -1
  3. package/dist/cjs/exports/middlewares/index.js +7 -0
  4. package/dist/cjs/exports/middlewares/index.js.map +1 -0
  5. package/dist/cjs/modules/auth/auth.controller.js +31 -26
  6. package/dist/cjs/modules/auth/auth.controller.js.map +1 -1
  7. package/dist/cjs/modules/auth/auth.router.js +4 -5
  8. package/dist/cjs/modules/auth/auth.router.js.map +1 -1
  9. package/dist/cjs/modules/auth/auth.service.js +27 -24
  10. package/dist/cjs/modules/auth/auth.service.js.map +1 -1
  11. package/dist/cjs/modules/auth/utils/helpers/auth.controller.helpers.js +4 -4
  12. package/dist/cjs/modules/auth/utils/helpers/auth.controller.helpers.js.map +1 -1
  13. package/dist/cjs/modules/base/base.controller.js +2 -29
  14. package/dist/cjs/modules/base/base.controller.js.map +1 -1
  15. package/dist/cjs/modules/base/base.middlewares.js +37 -1
  16. package/dist/cjs/modules/base/base.middlewares.js.map +1 -1
  17. package/dist/cjs/modules/base/base.router.js +2 -2
  18. package/dist/cjs/modules/base/base.router.js.map +1 -1
  19. package/dist/cjs/modules/base/utils/helpers/base.router.helpers.js +2 -3
  20. package/dist/cjs/modules/base/utils/helpers/base.router.helpers.js.map +1 -1
  21. package/dist/cjs/modules/base/utils/helpers/base.service.helpers.js +9 -11
  22. package/dist/cjs/modules/base/utils/helpers/base.service.helpers.js.map +1 -1
  23. package/dist/cjs/modules/error-handler/error-handler.controller.js +2 -0
  24. package/dist/cjs/modules/error-handler/error-handler.controller.js.map +1 -1
  25. package/dist/cjs/modules/error-handler/utils/catch-async.js +1 -1
  26. package/dist/cjs/modules/error-handler/utils/catch-async.js.map +1 -1
  27. package/dist/cjs/modules/file-uploader/file-uploader.service.js +4 -4
  28. package/dist/cjs/modules/file-uploader/file-uploader.service.js.map +1 -1
  29. package/dist/cjs/modules/file-uploader/utils/helpers/file-uploader.helpers.js +7 -5
  30. package/dist/cjs/modules/file-uploader/utils/helpers/file-uploader.helpers.js.map +1 -1
  31. package/dist/cjs/server.js +1 -1
  32. package/dist/cjs/server.js.map +1 -1
  33. package/dist/cjs/types/auth.js.map +1 -1
  34. package/dist/cjs/types/index.js.map +1 -1
  35. package/dist/cjs/utils/features/api.features.js +3 -2
  36. package/dist/cjs/utils/features/api.features.js.map +1 -1
  37. package/dist/cjs/utils/helpers/api.features.helpers.js +4 -4
  38. package/dist/cjs/utils/helpers/api.features.helpers.js.map +1 -1
  39. package/dist/cjs/utils/helpers/base.controller.helpers.js +77 -35
  40. package/dist/cjs/utils/helpers/base.controller.helpers.js.map +1 -1
  41. package/dist/cjs/utils/helpers/change-case.helpers.js +3 -3
  42. package/dist/cjs/utils/helpers/change-case.helpers.js.map +1 -1
  43. package/dist/cjs/utils/helpers/fs.helpers.js.map +1 -1
  44. package/dist/cjs/utils/helpers/models.helpers.js +35 -30
  45. package/dist/cjs/utils/helpers/models.helpers.js.map +1 -1
  46. package/dist/cjs/utils/scripts/generate-insomnia-collection.js +228 -0
  47. package/dist/cjs/utils/scripts/generate-insomnia-collection.js.map +1 -0
  48. package/dist/cjs/utils/scripts/index.js +12 -0
  49. package/dist/cjs/utils/scripts/index.js.map +1 -0
  50. package/dist/es2020/app.js +1 -1
  51. package/dist/es2020/app.js.map +1 -1
  52. package/dist/es2020/exports/middlewares/index.js +2 -0
  53. package/dist/es2020/exports/middlewares/index.js.map +1 -0
  54. package/dist/es2020/modules/auth/auth.controller.js +32 -27
  55. package/dist/es2020/modules/auth/auth.controller.js.map +1 -1
  56. package/dist/es2020/modules/auth/auth.router.js +1 -2
  57. package/dist/es2020/modules/auth/auth.router.js.map +1 -1
  58. package/dist/es2020/modules/auth/auth.service.js +27 -24
  59. package/dist/es2020/modules/auth/auth.service.js.map +1 -1
  60. package/dist/es2020/modules/auth/utils/helpers/auth.controller.helpers.js +4 -4
  61. package/dist/es2020/modules/auth/utils/helpers/auth.controller.helpers.js.map +1 -1
  62. package/dist/es2020/modules/base/base.controller.js +2 -29
  63. package/dist/es2020/modules/base/base.controller.js.map +1 -1
  64. package/dist/es2020/modules/base/base.middlewares.js +33 -1
  65. package/dist/es2020/modules/base/base.middlewares.js.map +1 -1
  66. package/dist/es2020/modules/base/base.router.js +2 -2
  67. package/dist/es2020/modules/base/base.router.js.map +1 -1
  68. package/dist/es2020/modules/base/utils/helpers/base.router.helpers.js +1 -2
  69. package/dist/es2020/modules/base/utils/helpers/base.router.helpers.js.map +1 -1
  70. package/dist/es2020/modules/base/utils/helpers/base.service.helpers.js +9 -11
  71. package/dist/es2020/modules/base/utils/helpers/base.service.helpers.js.map +1 -1
  72. package/dist/es2020/modules/error-handler/error-handler.controller.js +2 -0
  73. package/dist/es2020/modules/error-handler/error-handler.controller.js.map +1 -1
  74. package/dist/es2020/modules/error-handler/utils/catch-async.js +1 -1
  75. package/dist/es2020/modules/error-handler/utils/catch-async.js.map +1 -1
  76. package/dist/es2020/modules/file-uploader/file-uploader.service.js +4 -4
  77. package/dist/es2020/modules/file-uploader/file-uploader.service.js.map +1 -1
  78. package/dist/es2020/modules/file-uploader/utils/helpers/file-uploader.helpers.js +7 -5
  79. package/dist/es2020/modules/file-uploader/utils/helpers/file-uploader.helpers.js.map +1 -1
  80. package/dist/es2020/server.js +1 -1
  81. package/dist/es2020/server.js.map +1 -1
  82. package/dist/es2020/types/auth.js.map +1 -1
  83. package/dist/es2020/types/index.js.map +1 -1
  84. package/dist/es2020/utils/features/api.features.js +3 -2
  85. package/dist/es2020/utils/features/api.features.js.map +1 -1
  86. package/dist/es2020/utils/helpers/api.features.helpers.js +4 -4
  87. package/dist/es2020/utils/helpers/api.features.helpers.js.map +1 -1
  88. package/dist/es2020/utils/helpers/base.controller.helpers.js +75 -31
  89. package/dist/es2020/utils/helpers/base.controller.helpers.js.map +1 -1
  90. package/dist/es2020/utils/helpers/change-case.helpers.js +3 -3
  91. package/dist/es2020/utils/helpers/change-case.helpers.js.map +1 -1
  92. package/dist/es2020/utils/helpers/fs.helpers.js.map +1 -1
  93. package/dist/es2020/utils/helpers/models.helpers.js +35 -30
  94. package/dist/es2020/utils/helpers/models.helpers.js.map +1 -1
  95. package/dist/es2020/utils/scripts/generate-insomnia-collection.js +189 -0
  96. package/dist/es2020/utils/scripts/generate-insomnia-collection.js.map +1 -0
  97. package/dist/es2020/utils/scripts/index.js +10 -0
  98. package/dist/es2020/utils/scripts/index.js.map +1 -0
  99. package/dist/types/exports/middlewares/index.d.ts +1 -0
  100. package/dist/types/modules/auth/auth.service.d.ts +3 -3
  101. package/dist/types/modules/base/base.middlewares.d.ts +6 -1
  102. package/dist/types/modules/base/utils/helpers/base.service.helpers.d.ts +1 -1
  103. package/dist/types/types/auth.d.ts +9 -8
  104. package/dist/types/types/index.d.ts +2 -1
  105. package/dist/types/utils/helpers/base.controller.helpers.d.ts +4 -6
  106. package/dist/types/utils/scripts/generate-insomnia-collection.d.ts +1 -0
  107. package/dist/types/utils/scripts/index.d.ts +2 -0
  108. package/package.json +10 -1
@@ -12,7 +12,7 @@ import AppError from "../error-handler/utils/app-error";
12
12
  import authService from "./auth.service";
13
13
  import { getBaseServices } from "../base/base.service";
14
14
  import { getPrismaInstance } from "../../utils/helpers/prisma.helpers";
15
- import { importPrismaModelModules } from "../../utils/helpers/models.helpers";
15
+ import { importPrismaModelModules, } from "../../utils/helpers/models.helpers";
16
16
  import deepmerge from "../../utils/helpers/deepmerge.helper";
17
17
  import arkosEnv from "../../utils/arkos-env";
18
18
  import { getArkosConfig } from "../../server";
@@ -31,8 +31,8 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
31
31
  getMe: catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
32
32
  const user = yield baseServices["user"].findOne({ id: req.user.id }, stringifiedQueryOptions);
33
33
  Object.keys(defaultExcludedUserFields).forEach((key) => {
34
- if (req.user)
35
- delete req.user[key];
34
+ if (user)
35
+ delete user[key];
36
36
  });
37
37
  if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.afterGetMe) {
38
38
  req.responseData = { data: user };
@@ -46,15 +46,15 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
46
46
  throw new AppError("In order to update password use the update-password endpoint.", 400);
47
47
  const user = yield baseServices["user"].updateOne({ id: req.user.id }, req.body, stringifiedQueryOptions);
48
48
  Object.keys(defaultExcludedUserFields).forEach((key) => {
49
- if (req.user)
50
- delete req.user[key];
49
+ if (user)
50
+ delete user[key];
51
51
  });
52
52
  if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.afterGetMe) {
53
53
  req.responseData = user;
54
54
  req.responseStatus = 200;
55
55
  return next();
56
56
  }
57
- res.status(200).json({ data: req.user });
57
+ res.status(200).json({ data: user });
58
58
  })),
59
59
  logout: catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
60
60
  res.cookie("arkos_access_token", "no-token", {
@@ -69,7 +69,7 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
69
69
  res.status(204).json();
70
70
  })),
71
71
  login: catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
72
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
72
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
73
73
  const authConfigs = (_a = getArkosConfig()) === null || _a === void 0 ? void 0 : _a.authentication;
74
74
  const usernameField = determineUsernameField(req);
75
75
  const lastField = usernameField.split(".")[usernameField.split(".").length - 1];
@@ -79,7 +79,7 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
79
79
  return next(new AppError(`Please provide both ${lastField} and password`, 400));
80
80
  const prisma = getPrismaInstance();
81
81
  let whereClause;
82
- if (usernameField === null || usernameField === void 0 ? void 0 : usernameField.includes(".")) {
82
+ if ((_b = usernameField === null || usernameField === void 0 ? void 0 : usernameField.includes) === null || _b === void 0 ? void 0 : _b.call(usernameField, ".")) {
83
83
  const valueToFind = getNestedValue(req.body, usernameField);
84
84
  if (valueToFind === undefined) {
85
85
  return next(new AppError(`Invalid ${usernameField} provided`, 400));
@@ -99,38 +99,41 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
99
99
  const token = authService.signJwtToken(user.id);
100
100
  const cookieOptions = {
101
101
  expires: new Date(Date.now() +
102
- Number(toMs(((_b = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _b === void 0 ? void 0 : _b.expiresIn) ||
102
+ Number(toMs(((_c = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _c === void 0 ? void 0 : _c.expiresIn) ||
103
103
  process.env.JWT_EXPIRES_IN ||
104
104
  arkosEnv.JWT_EXPIRES_IN))),
105
- httpOnly: ((_d = (_c = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _c === void 0 ? void 0 : _c.cookie) === null || _d === void 0 ? void 0 : _d.httpOnly) ||
105
+ httpOnly: ((_e = (_d = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _d === void 0 ? void 0 : _d.cookie) === null || _e === void 0 ? void 0 : _e.httpOnly) ||
106
106
  process.env.JWT_COOKIE_HTTP_ONLY === "true" ||
107
107
  true,
108
- secure: ((_f = (_e = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _e === void 0 ? void 0 : _e.cookie) === null || _f === void 0 ? void 0 : _f.secure) ||
108
+ secure: ((_g = (_f = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _f === void 0 ? void 0 : _f.cookie) === null || _g === void 0 ? void 0 : _g.secure) ||
109
109
  process.env.JWT_COOKIE_SECURE === "true" ||
110
110
  req.secure ||
111
111
  req.headers["x-forwarded-proto"] === "https",
112
- sameSite: ((_h = (_g = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _g === void 0 ? void 0 : _g.cookie) === null || _h === void 0 ? void 0 : _h.sameSite) ||
112
+ sameSite: ((_j = (_h = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.jwt) === null || _h === void 0 ? void 0 : _h.cookie) === null || _j === void 0 ? void 0 : _j.sameSite) ||
113
113
  process.env.JWT_COOKIE_SAME_SITE ||
114
114
  process.env.NODE_ENV === "production"
115
115
  ? "none"
116
116
  : "lax",
117
117
  };
118
- if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.afterLogin) {
118
+ if (((_k = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _k === void 0 ? void 0 : _k.sendAccessTokenThrough) === "response-only" ||
119
+ ((_l = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _l === void 0 ? void 0 : _l.sendAccessTokenThrough) === "both") {
119
120
  req.responseData = { accessToken: token };
121
+ }
122
+ else if (((_m = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _m === void 0 ? void 0 : _m.sendAccessTokenThrough) === "cookie-only" ||
123
+ ((_o = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _o === void 0 ? void 0 : _o.sendAccessTokenThrough) === "both")
124
+ res.cookie("arkos_access_token", token, cookieOptions);
125
+ if (middlewares === null || middlewares === void 0 ? void 0 : middlewares.afterLogin) {
126
+ req.additionalData = { user };
120
127
  req.responseStatus = 200;
121
128
  return next();
122
129
  }
123
- if (((_j = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _j === void 0 ? void 0 : _j.sendAccessTokenThrough) === "response-only") {
124
- res.status(200).json({ accessToken: token });
130
+ if (((_p = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _p === void 0 ? void 0 : _p.sendAccessTokenThrough) === "response-only" ||
131
+ ((_q = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _q === void 0 ? void 0 : _q.sendAccessTokenThrough) === "both") {
132
+ res.status(200).json(req.responseData);
125
133
  }
126
- else if (((_k = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _k === void 0 ? void 0 : _k.sendAccessTokenThrough) === "cookie-only") {
127
- res.cookie("arkos_access_token", token, cookieOptions);
134
+ else if (((_r = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _r === void 0 ? void 0 : _r.sendAccessTokenThrough) === "cookie-only" ||
135
+ ((_s = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _s === void 0 ? void 0 : _s.sendAccessTokenThrough) === "both")
128
136
  res.status(200).send();
129
- }
130
- else {
131
- res.cookie("arkos_access_token", token, cookieOptions);
132
- res.status(200).json({ accessToken: token });
133
- }
134
137
  })),
135
138
  signup: catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
136
139
  const userService = baseServices["user"];
@@ -146,7 +149,7 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
146
149
  res.status(201).json({ data: user });
147
150
  })),
148
151
  updatePassword: catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
149
- var _a, _b;
152
+ var _a;
150
153
  const { currentPassword, newPassword } = req.body;
151
154
  if (!currentPassword || !newPassword)
152
155
  return next(new AppError("currentPassword and newPassword are required", 400));
@@ -154,12 +157,14 @@ export const authControllerFactory = (...args_1) => __awaiter(void 0, [...args_1
154
157
  if (!user || (user === null || user === void 0 ? void 0 : user.isActive) === false || (user === null || user === void 0 ? void 0 : user.deletedSelfAccountAt))
155
158
  return next(new AppError("User not found!", 404));
156
159
  const isPasswordCorrect = yield authService.isCorrectPassword(String(currentPassword), String(user.password));
160
+ const configs = getArkosConfig();
161
+ const initAuthConfigs = configs === null || configs === void 0 ? void 0 : configs.authentication;
157
162
  if (!isPasswordCorrect)
158
163
  return next(new AppError("Current password is incorrect.", 400));
159
- if (!authService.isPasswordStrong(String(newPassword))) {
160
- const initAuthConfigs = (_a = getArkosConfig()) === null || _a === void 0 ? void 0 : _a.authentication;
161
- return next(new AppError(((_b = initAuthConfigs === null || initAuthConfigs === void 0 ? void 0 : initAuthConfigs.passwordValidation) === null || _b === void 0 ? void 0 : _b.message) ||
162
- "Password must contain at least one uppercase letter, one lowercase letter, and one number", 400));
164
+ if (!authService.isPasswordStrong(String(newPassword)) &&
165
+ !(configs === null || configs === void 0 ? void 0 : configs.validation)) {
166
+ return next(new AppError(((_a = initAuthConfigs === null || initAuthConfigs === void 0 ? void 0 : initAuthConfigs.passwordValidation) === null || _a === void 0 ? void 0 : _a.message) ||
167
+ "The new password must contain at least one uppercase letter, one lowercase letter, and one number", 400));
163
168
  }
164
169
  const prisma = getPrismaInstance();
165
170
  yield prisma.user.update({
@@ -1 +1 @@
1
- {"version":3,"file":"auth.controller.js","sourceRoot":"","sources":["../../../../src/modules/auth/auth.controller.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,UAAU,MAAM,oCAAoC,CAAC;AAC5D,OAAO,QAAQ,MAAM,kCAAkC,CAAC;AAGxD,OAAO,WAAW,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAC9E,OAAO,SAAS,MAAM,sCAAsC,CAAC;AAC7D,OAAO,QAAQ,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,cAAc,EAEd,IAAI,GACL,MAAM,yCAAyC,CAAC;AAKjD,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,QAAQ,EAAE,KAAK;CAChB,CAAC;AAQF,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAA8B,EAAE,mDAAzB,cAAmB,EAAE;IAC/D,MAAM,YAAY,GAAG,eAAe,EAAE,CAAC;IACvC,IAA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C;YAChB,CAAC;YAED,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACnB,MAAM,EAAE,SAAS;gBACjB,OAAO,EAAE,gCAAgC;aAC1C,CAAC,CAAC;QACL,CAAC,CAAA,CACF;KACF,CAAC;AACJ,CAAC,CAAA,CAAC","sourcesContent":["import catchAsync from \"../error-handler/utils/catch-async\";\nimport AppError from \"../error-handler/utils/app-error\";\nimport { CookieOptions } from \"express\";\nimport { ArkosRequest, ArkosResponse, ArkosNextFunction } from \"../../types\";\nimport authService from \"./auth.service\";\nimport { getBaseServices } from \"../base/base.service\";\nimport { User } from \"../../types\";\nimport { getPrismaInstance } from \"../../utils/helpers/prisma.helpers\";\nimport { importPrismaModelModules } from \"../../utils/helpers/models.helpers\";\nimport deepmerge from \"../../utils/helpers/deepmerge.helper\";\nimport arkosEnv from \"../../utils/arkos-env\";\nimport { getArkosConfig } from \"../../server\";\nimport {\n createPrismaWhereClause,\n determineUsernameField,\n getNestedValue,\n MsDuration,\n toMs,\n} from \"./utils/helpers/auth.controller.helpers\";\n\n/**\n * Default fields to exclude from user object when returning to client\n */\nexport const defaultExcludedUserFields = {\n password: false,\n};\n\n/**\n * Factory function to create authentication controller with configurable middlewares\n *\n * @param middlewares - Optional middleware functions to execute after controller actions\n * @returns An object containing all authentication controller methods\n */\nexport const authControllerFactory = async (middlewares: any = {}) => {\n const baseServices = getBaseServices();\n let prismaQueryOptions: Record<string, any> = {};\n\n const userModules = await importPrismaModelModules(\"user\");\n if (userModules) prismaQueryOptions = userModules?.prismaQueryOptions || {};\n\n const stringifiedQueryOptions = JSON.stringify(\n deepmerge(\n prismaQueryOptions?.queryOptions || {},\n prismaQueryOptions?.findOne || {}\n ) || {}\n );\n\n return {\n /**\n * Retrieves the current authenticated user's information\n */\n getMe: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const user = await baseServices[\"user\"].findOne(\n { id: req.user!.id },\n stringifiedQueryOptions\n );\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n if (req.user) delete req.user[key as keyof User];\n });\n\n if (middlewares?.afterGetMe) {\n req.responseData = { data: user };\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({ data: user });\n }\n ),\n\n /**\n * Updates the current authenticated user's information\n */\n updateMe: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n if (\"password\" in req.body)\n throw new AppError(\n \"In order to update password use the update-password endpoint.\",\n 400\n );\n\n const user = await baseServices[\"user\"].updateOne(\n { id: req.user!.id },\n req.body,\n stringifiedQueryOptions\n );\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n if (req.user) delete req.user[key as keyof User];\n });\n\n if (middlewares?.afterGetMe) {\n req.responseData = user;\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({ data: req.user });\n }\n ),\n\n /**\n * Logs out the current user by invalidating their access token cookie\n */\n logout: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n res.cookie(\"arkos_access_token\", \"no-token\", {\n expires: new Date(Date.now() + 10 * 1000),\n httpOnly: true,\n });\n\n if (middlewares?.afterLogout) {\n req.responseData = null;\n req.responseStatus = 204;\n return next();\n }\n\n res.status(204).json();\n }\n ),\n\n /**\n * Authenticates a user using configurable username field and password\n * Username field can be specified in query parameter or config\n * Supports nested fields and array queries (e.g., \"profile.nickname\", \"phones.some.number\")\n */\n login: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const authConfigs = getArkosConfig()?.authentication;\n\n const usernameField = determineUsernameField(req);\n\n // For the error message, we only care about the top-level field name\n const lastField =\n usernameField.split(\".\")[usernameField.split(\".\").length - 1];\n\n const usernameValue = req.body[lastField];\n\n const { password } = req.body;\n\n if (!usernameValue || !password)\n return next(\n new AppError(`Please provide both ${lastField} and password`, 400)\n );\n\n const prisma = getPrismaInstance();\n\n // Create appropriate where clause for the query\n let whereClause: Record<string, any>;\n\n if (usernameField?.includes(\".\")) {\n // For nested paths, we need to extract the actual value to search for\n const valueToFind = getNestedValue(req.body, usernameField);\n if (valueToFind === undefined) {\n return next(new AppError(`Invalid ${usernameField} provided`, 400));\n }\n whereClause = createPrismaWhereClause(usernameField, valueToFind);\n } else {\n // Simple field case\n whereClause = { [usernameField]: usernameValue };\n }\n\n // Use findFirst instead of findUnique for complex queries\n const user = await (prisma as any).user.findFirst({\n where: whereClause,\n });\n\n if (\n !user ||\n !(await authService.isCorrectPassword(password, user.password))\n ) {\n return next(new AppError(`Incorrect ${lastField} or password`, 401));\n }\n\n const token = authService.signJwtToken(user.id!);\n\n const cookieOptions: CookieOptions = {\n expires: new Date(\n Date.now() +\n Number(\n toMs(\n authConfigs?.jwt?.expiresIn ||\n (process.env.JWT_EXPIRES_IN as MsDuration) ||\n (arkosEnv.JWT_EXPIRES_IN as MsDuration)\n )\n )\n ),\n httpOnly:\n authConfigs?.jwt?.cookie?.httpOnly ||\n process.env.JWT_COOKIE_HTTP_ONLY === \"true\" ||\n true,\n secure:\n authConfigs?.jwt?.cookie?.secure ||\n process.env.JWT_COOKIE_SECURE === \"true\" ||\n req.secure ||\n req.headers[\"x-forwarded-proto\"] === \"https\",\n sameSite:\n authConfigs?.jwt?.cookie?.sameSite ||\n process.env.JWT_COOKIE_SAME_SITE ||\n process.env.NODE_ENV === \"production\"\n ? \"none\"\n : \"lax\",\n };\n\n if (middlewares?.afterLogin) {\n req.responseData = { accessToken: token };\n req.responseStatus = 200;\n return next();\n }\n\n if (authConfigs?.login?.sendAccessTokenThrough === \"response-only\") {\n res.status(200).json({ accessToken: token });\n } else if (\n authConfigs?.login?.sendAccessTokenThrough === \"cookie-only\"\n ) {\n res.cookie(\"arkos_access_token\", token, cookieOptions);\n res.status(200).send();\n } else {\n res.cookie(\"arkos_access_token\", token, cookieOptions);\n res.status(200).json({ accessToken: token });\n }\n }\n ),\n\n /**\n * Creates a new user account\n */\n signup: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const userService = baseServices[\"user\"];\n\n const user = await userService.createOne(\n req.body,\n stringifiedQueryOptions\n );\n\n if (middlewares?.afterSignup) {\n req.responseData = { data: user };\n req.responseStatus = 201;\n return next();\n }\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n delete user[key as keyof User];\n });\n\n res.status(201).json({ data: user });\n }\n ),\n\n /**\n * Updates the password of the authenticated user\n */\n updatePassword: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const { currentPassword, newPassword } = req.body;\n\n if (!currentPassword || !newPassword)\n return next(\n new AppError(\"currentPassword and newPassword are required\", 400)\n );\n\n const user = req.user;\n\n if (!user || user?.isActive === false || user?.deletedSelfAccountAt)\n return next(new AppError(\"User not found!\", 404));\n\n // Check if the current password is correct\n const isPasswordCorrect = await authService.isCorrectPassword(\n String(currentPassword),\n String(user.password)\n );\n\n if (!isPasswordCorrect)\n return next(new AppError(\"Current password is incorrect.\", 400));\n\n // Check password strength (optional but recommended)\n if (!authService.isPasswordStrong(String(newPassword))) {\n const initAuthConfigs = getArkosConfig()?.authentication;\n\n return next(\n new AppError(\n initAuthConfigs?.passwordValidation?.message ||\n \"Password must contain at least one uppercase letter, one lowercase letter, and one number\",\n 400\n )\n );\n }\n\n const prisma = getPrismaInstance();\n\n // Update the password\n await (prisma as any).user.update({\n where: { id: user.id },\n data: {\n password: await authService.hashPassword(newPassword),\n passwordChangedAt: new Date(),\n },\n });\n\n if (middlewares?.afterUpdatePassword) {\n (req as any).additionalData = {\n user,\n };\n req.responseData = {\n status: \"success\",\n message: \"Password updated successfully!\",\n };\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({\n status: \"success\",\n message: \"Password updated successfully!\",\n });\n }\n ),\n };\n};\n"]}
1
+ {"version":3,"file":"auth.controller.js","sourceRoot":"","sources":["../../../../src/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,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACnB,MAAM,EAAE,SAAS;gBACjB,OAAO,EAAE,gCAAgC;aAC1C,CAAC,CAAC;QACL,CAAC,CAAA,CACF;KACF,CAAC;AACJ,CAAC,CAAA,CAAC","sourcesContent":["import catchAsync from \"../error-handler/utils/catch-async\";\nimport AppError from \"../error-handler/utils/app-error\";\nimport { CookieOptions } from \"express\";\nimport { ArkosRequest, ArkosResponse, ArkosNextFunction } from \"../../types\";\nimport authService from \"./auth.service\";\nimport { getBaseServices } from \"../base/base.service\";\nimport { User } from \"../../types\";\nimport { getPrismaInstance } from \"../../utils/helpers/prisma.helpers\";\nimport {\n getModelModules,\n importPrismaModelModules,\n} from \"../../utils/helpers/models.helpers\";\nimport deepmerge from \"../../utils/helpers/deepmerge.helper\";\nimport arkosEnv from \"../../utils/arkos-env\";\nimport { getArkosConfig } from \"../../server\";\nimport {\n createPrismaWhereClause,\n determineUsernameField,\n getNestedValue,\n MsDuration,\n toMs,\n} from \"./utils/helpers/auth.controller.helpers\";\n\n/**\n * Default fields to exclude from user object when returning to client\n */\nexport const defaultExcludedUserFields = {\n password: false,\n};\n\n/**\n * Factory function to create authentication controller with configurable middlewares\n *\n * @param middlewares - Optional middleware functions to execute after controller actions\n * @returns An object containing all authentication controller methods\n */\nexport const authControllerFactory = async (middlewares: any = {}) => {\n const baseServices = getBaseServices();\n let prismaQueryOptions: Record<string, any> = {};\n\n const userModules = await importPrismaModelModules(\"user\");\n if (userModules) prismaQueryOptions = userModules?.prismaQueryOptions || {};\n\n const stringifiedQueryOptions = JSON.stringify(\n deepmerge(\n prismaQueryOptions?.queryOptions || {},\n prismaQueryOptions?.findOne || {}\n ) || {}\n );\n\n return {\n /**\n * Retrieves the current authenticated user's information\n */\n getMe: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const user = await baseServices[\"user\"].findOne(\n { id: req.user!.id },\n stringifiedQueryOptions\n );\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n if (user) delete user[key as keyof User];\n });\n\n if (middlewares?.afterGetMe) {\n req.responseData = { data: user };\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({ data: user });\n }\n ),\n\n /**\n * Updates the current authenticated user's information\n */\n updateMe: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n if (\"password\" in req.body)\n throw new AppError(\n \"In order to update password use the update-password endpoint.\",\n 400\n );\n\n const user = await baseServices[\"user\"].updateOne(\n { id: req.user!.id },\n req.body,\n stringifiedQueryOptions\n );\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n if (user) delete user[key as keyof User];\n });\n\n if (middlewares?.afterGetMe) {\n req.responseData = user;\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({ data: user });\n }\n ),\n\n /**\n * Logs out the current user by invalidating their access token cookie\n */\n logout: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n res.cookie(\"arkos_access_token\", \"no-token\", {\n expires: new Date(Date.now() + 10 * 1000),\n httpOnly: true,\n });\n\n if (middlewares?.afterLogout) {\n req.responseData = null;\n req.responseStatus = 204;\n return next();\n }\n\n res.status(204).json();\n }\n ),\n\n /**\n * Authenticates a user using configurable username field and password\n * Username field can be specified in query parameter or config\n * Supports nested fields and array queries (e.g., \"profile.nickname\", \"phones.some.number\")\n */\n login: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const authConfigs = getArkosConfig()?.authentication;\n\n const usernameField = determineUsernameField(req);\n\n // For the error message, we only care about the top-level field name\n const lastField =\n usernameField.split(\".\")[usernameField.split(\".\").length - 1];\n\n const usernameValue = req.body[lastField];\n\n const { password } = req.body;\n\n if (!usernameValue || !password)\n return next(\n new AppError(`Please provide both ${lastField} and password`, 400)\n );\n\n const prisma = getPrismaInstance();\n\n // Create appropriate where clause for the query\n let whereClause: Record<string, any>;\n\n if (usernameField?.includes?.(\".\")) {\n // For nested paths, we need to extract the actual value to search for\n const valueToFind = getNestedValue(req.body, usernameField);\n if (valueToFind === undefined) {\n return next(new AppError(`Invalid ${usernameField} provided`, 400));\n }\n whereClause = createPrismaWhereClause(usernameField, valueToFind);\n } else {\n // Simple field case\n whereClause = { [usernameField]: usernameValue };\n }\n\n // Use findFirst instead of findUnique for complex queries\n const user = await (prisma as any).user.findFirst({\n where: whereClause,\n // select: {\n // id: true,\n // password: true,\n // },\n });\n\n if (\n !user ||\n !(await authService.isCorrectPassword(password, user.password))\n ) {\n return next(new AppError(`Incorrect ${lastField} or password`, 401));\n }\n\n const token = authService.signJwtToken(user.id!);\n\n const cookieOptions: CookieOptions = {\n expires: new Date(\n Date.now() +\n Number(\n toMs(\n authConfigs?.jwt?.expiresIn ||\n (process.env.JWT_EXPIRES_IN as MsDuration) ||\n (arkosEnv.JWT_EXPIRES_IN as MsDuration)\n )\n )\n ),\n httpOnly:\n authConfigs?.jwt?.cookie?.httpOnly ||\n process.env.JWT_COOKIE_HTTP_ONLY === \"true\" ||\n true,\n secure:\n authConfigs?.jwt?.cookie?.secure ||\n process.env.JWT_COOKIE_SECURE === \"true\" ||\n req.secure ||\n req.headers[\"x-forwarded-proto\"] === \"https\",\n sameSite:\n authConfigs?.jwt?.cookie?.sameSite ||\n process.env.JWT_COOKIE_SAME_SITE ||\n process.env.NODE_ENV === \"production\"\n ? \"none\"\n : \"lax\",\n };\n\n if (\n authConfigs?.login?.sendAccessTokenThrough === \"response-only\" ||\n authConfigs?.login?.sendAccessTokenThrough === \"both\"\n ) {\n req.responseData = { accessToken: token };\n } else if (\n authConfigs?.login?.sendAccessTokenThrough === \"cookie-only\" ||\n authConfigs?.login?.sendAccessTokenThrough === \"both\"\n )\n res.cookie(\"arkos_access_token\", token, cookieOptions);\n\n if (middlewares?.afterLogin) {\n req.additionalData = { user };\n req.responseStatus = 200;\n return next();\n }\n\n if (\n authConfigs?.login?.sendAccessTokenThrough === \"response-only\" ||\n authConfigs?.login?.sendAccessTokenThrough === \"both\"\n ) {\n res.status(200).json(req.responseData);\n } else if (\n authConfigs?.login?.sendAccessTokenThrough === \"cookie-only\" ||\n authConfigs?.login?.sendAccessTokenThrough === \"both\"\n )\n res.status(200).send();\n }\n ),\n\n /**\n * Creates a new user account\n */\n signup: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const userService = baseServices[\"user\"];\n\n const user = await userService.createOne(\n req.body,\n stringifiedQueryOptions\n );\n\n if (middlewares?.afterSignup) {\n req.responseData = { data: user };\n req.responseStatus = 201;\n return next();\n }\n\n Object.keys(defaultExcludedUserFields).forEach((key) => {\n delete user[key as keyof User];\n });\n\n res.status(201).json({ data: user });\n }\n ),\n\n /**\n * Updates the password of the authenticated user\n */\n updatePassword: catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n const { currentPassword, newPassword } = req.body;\n\n if (!currentPassword || !newPassword)\n return next(\n new AppError(\"currentPassword and newPassword are required\", 400)\n );\n\n const user = req.user;\n\n if (!user || user?.isActive === false || user?.deletedSelfAccountAt)\n return next(new AppError(\"User not found!\", 404));\n\n // Check if the current password is correct\n const isPasswordCorrect = await authService.isCorrectPassword(\n String(currentPassword),\n String(user.password)\n );\n\n const configs = getArkosConfig();\n const initAuthConfigs = configs?.authentication;\n // const modules = getModelModules(\"auth\");\n\n if (!isPasswordCorrect)\n return next(new AppError(\"Current password is incorrect.\", 400));\n\n // Check password strength (optional but recommended)\n if (\n !authService.isPasswordStrong(String(newPassword)) &&\n !configs?.validation\n ) {\n return next(\n new AppError(\n initAuthConfigs?.passwordValidation?.message ||\n \"The new password must contain at least one uppercase letter, one lowercase letter, and one number\",\n 400\n )\n );\n }\n\n const prisma = getPrismaInstance();\n\n // Update the password\n await (prisma as any).user.update({\n where: { id: user.id },\n data: {\n password: await authService.hashPassword(newPassword),\n passwordChangedAt: new Date(),\n },\n });\n\n if (middlewares?.afterUpdatePassword) {\n req.additionalData = {\n user,\n };\n req.responseData = {\n status: \"success\",\n message: \"Password updated successfully!\",\n };\n req.responseStatus = 200;\n return next();\n }\n\n res.status(200).json({\n status: \"success\",\n message: \"Password updated successfully!\",\n });\n }\n ),\n };\n};\n"]}
@@ -12,9 +12,8 @@ import { authControllerFactory } from "./auth.controller";
12
12
  import authService from "./auth.service";
13
13
  import rateLimit from "express-rate-limit";
14
14
  import { importPrismaModelModules } from "../../utils/helpers/models.helpers";
15
- import { sendResponse } from "../base/base.middlewares";
15
+ import { handleRequestBodyValidationAndTransformation, sendResponse, } from "../base/base.middlewares";
16
16
  import deepmerge from "../../utils/helpers/deepmerge.helper";
17
- import { handleRequestBodyValidationAndTransformation } from "../../utils/helpers/base.controller.helpers";
18
17
  const router = Router();
19
18
  export function getAuthRouter(arkosConfigs) {
20
19
  return __awaiter(this, void 0, void 0, function* () {
@@ -1 +1 @@
1
- {"version":3,"file":"auth.router.js","sourceRoot":"","sources":["../../../../src/modules/auth/auth.router.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,WAAW,MAAM,gBAAgB,CAAC;AACzC,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAC9E,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,SAAS,MAAM,sCAAsC,CAAC;AAC7D,OAAO,EAAE,4CAA4C,EAAE,MAAM,6CAA6C,CAAC;AAE3G,MAAM,MAAM,GAAW,MAAM,EAAE,CAAC;AAEhC,MAAM,UAAgB,aAAa,CAAC,YAAyB;;;QAC3D,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;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AX,WAAW,uBAAX,WAAW,CAAE,oBAAoB,mCAAI,cAAc,CAAC,cAAc,EAClE,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,oBAAoB;YAC/B,CAAC,CAAC,cAAc,CAAC,cAAc;YAC/B,CAAC,CAAC,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,mBAAmB,mCAAI,YAAY,EACpD,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,oBAAoB,MAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,mBAAmB,CAAA;YACnE,CAAC,CAAC,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,mBAAmB;YAClC,CAAC,CAAC,YAAY,EAChB,YAAY,CACb,CAAC;QAEF,OAAO,MAAM,CAAC;IAChB,CAAC;CAAA","sourcesContent":["import { Router } from \"express\";\nimport { authControllerFactory } from \"./auth.controller\";\nimport authService from \"./auth.service\";\nimport rateLimit from \"express-rate-limit\";\nimport { importPrismaModelModules } from \"../../utils/helpers/models.helpers\";\nimport { sendResponse } from \"../base/base.middlewares\";\nimport { ArkosConfig } from \"../../types/arkos-config\";\nimport deepmerge from \"../../utils/helpers/deepmerge.helper\";\nimport { handleRequestBodyValidationAndTransformation } from \"../../utils/helpers/base.controller.helpers\";\n\nconst router: Router = Router();\n\nexport async function getAuthRouter(arkosConfigs: ArkosConfig) {\n const { middlewares } = await importPrismaModelModules(\"auth\");\n const authController = await authControllerFactory(middlewares);\n\n router\n .get(\n \"/users/me\",\n authService.authenticate,\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n )\n .patch(\n \"/users/me\",\n authService.authenticate,\n handleRequestBodyValidationAndTransformation(\"auth\", \"updateMe\"),\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n )\n .delete(\n \"/users/me\",\n authService.authenticate,\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n );\n\n router.use(\n rateLimit(\n deepmerge(\n {\n windowMs: 5000,\n limit: 10,\n standardHeaders: \"draft-7\",\n legacyHeaders: false,\n },\n arkosConfigs?.authentication?.requestRateLimitOptions || {}\n )\n )\n );\n\n router.post(\n \"/auth/login\",\n handleRequestBodyValidationAndTransformation(\"auth\", \"login\"),\n middlewares?.beforeLogin ?? authController.login,\n middlewares?.beforeLogin\n ? authController.login\n : middlewares?.afterLogin ?? sendResponse,\n middlewares?.beforeLogin && middlewares?.afterLogin\n ? middlewares?.afterLogin\n : sendResponse,\n sendResponse\n );\n\n router.delete(\n \"/auth/logout\",\n authService.authenticate,\n middlewares?.beforeLogout ?? authController.logout,\n middlewares?.beforeLogout\n ? authController.logout\n : middlewares?.afterLogout ?? sendResponse,\n middlewares?.beforeLogout && middlewares?.afterLogout\n ? middlewares?.afterLogout\n : sendResponse,\n sendResponse\n );\n\n router.post(\n \"/auth/signup\",\n handleRequestBodyValidationAndTransformation(\"auth\", \"signup\"),\n middlewares?.beforeSignup ?? authController.signup,\n middlewares?.beforeSignup\n ? authController.signup\n : middlewares?.afterSignup ?? sendResponse,\n middlewares?.beforeSignup && middlewares?.afterSignup\n ? middlewares?.afterSignup\n : sendResponse,\n sendResponse\n );\n\n router.post(\n \"/auth/update-password\",\n authService.authenticate,\n handleRequestBodyValidationAndTransformation(\"auth\", \"updatePassword\"),\n middlewares?.beforeUpdatePassword ?? authController.updatePassword,\n middlewares?.beforeUpdatePassword\n ? authController.updatePassword\n : middlewares?.afterUpdatePassword ?? sendResponse,\n middlewares?.beforeUpdatePassword && middlewares?.afterUpdatePassword\n ? middlewares?.afterUpdatePassword\n : sendResponse,\n sendResponse\n );\n\n return router;\n}\n"]}
1
+ {"version":3,"file":"auth.router.js","sourceRoot":"","sources":["../../../../src/modules/auth/auth.router.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,WAAW,MAAM,gBAAgB,CAAC;AACzC,OAAO,SAAS,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAC9E,OAAO,EACL,4CAA4C,EAC5C,YAAY,GACb,MAAM,0BAA0B,CAAC;AAElC,OAAO,SAAS,MAAM,sCAAsC,CAAC;AAE7D,MAAM,MAAM,GAAW,MAAM,EAAE,CAAC;AAEhC,MAAM,UAAgB,aAAa,CAAC,YAAyB;;;QAC3D,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,wBAAwB,CAAC,MAAM,CAAC,CAAC;QAC/D,MAAM,cAAc,GAAG,MAAM,qBAAqB,CAAC,WAAW,CAAC,CAAC;QAEhE,MAAM;aACH,GAAG,CACF,WAAW,EACX,WAAW,CAAC,YAAY,EACxB,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,WAAW,mCAAI,cAAc,CAAC,KAAK,EAChD,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,WAAW;YACtB,CAAC,CAAC,cAAc,CAAC,KAAK;YACtB,CAAC,CAAC,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,UAAU,mCAAI,YAAY,EAC3C,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,WAAW,MAAI,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,UAAU,CAAA;YACjD,CAAC,CAAC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;CAAA","sourcesContent":["import { Router } from \"express\";\nimport { authControllerFactory } from \"./auth.controller\";\nimport authService from \"./auth.service\";\nimport rateLimit from \"express-rate-limit\";\nimport { importPrismaModelModules } from \"../../utils/helpers/models.helpers\";\nimport {\n handleRequestBodyValidationAndTransformation,\n sendResponse,\n} from \"../base/base.middlewares\";\nimport { ArkosConfig } from \"../../types/arkos-config\";\nimport deepmerge from \"../../utils/helpers/deepmerge.helper\";\n\nconst router: Router = Router();\n\nexport async function getAuthRouter(arkosConfigs: ArkosConfig) {\n const { middlewares } = await importPrismaModelModules(\"auth\");\n const authController = await authControllerFactory(middlewares);\n\n router\n .get(\n \"/users/me\",\n authService.authenticate,\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n )\n .patch(\n \"/users/me\",\n authService.authenticate,\n handleRequestBodyValidationAndTransformation(\"auth\", \"updateMe\"),\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n )\n .delete(\n \"/users/me\",\n authService.authenticate,\n middlewares?.beforeGetMe ?? authController.getMe,\n middlewares?.beforeGetMe\n ? authController.getMe\n : middlewares?.afterGetMe ?? sendResponse,\n middlewares?.beforeGetMe && middlewares?.afterGetMe\n ? middlewares?.afterGetMe\n : sendResponse,\n sendResponse\n );\n\n router.use(\n rateLimit(\n deepmerge(\n {\n windowMs: 5000,\n limit: 10,\n standardHeaders: \"draft-7\",\n legacyHeaders: false,\n },\n arkosConfigs?.authentication?.requestRateLimitOptions || {}\n )\n )\n );\n\n router.post(\n \"/auth/login\",\n handleRequestBodyValidationAndTransformation(\"auth\", \"login\"),\n middlewares?.beforeLogin ?? authController.login,\n middlewares?.beforeLogin\n ? authController.login\n : middlewares?.afterLogin ?? sendResponse,\n middlewares?.beforeLogin && middlewares?.afterLogin\n ? middlewares?.afterLogin\n : sendResponse,\n sendResponse\n );\n\n router.delete(\n \"/auth/logout\",\n authService.authenticate,\n middlewares?.beforeLogout ?? authController.logout,\n middlewares?.beforeLogout\n ? authController.logout\n : middlewares?.afterLogout ?? sendResponse,\n middlewares?.beforeLogout && middlewares?.afterLogout\n ? middlewares?.afterLogout\n : sendResponse,\n sendResponse\n );\n\n router.post(\n \"/auth/signup\",\n handleRequestBodyValidationAndTransformation(\"auth\", \"signup\"),\n middlewares?.beforeSignup ?? authController.signup,\n middlewares?.beforeSignup\n ? authController.signup\n : middlewares?.afterSignup ?? sendResponse,\n middlewares?.beforeSignup && middlewares?.afterSignup\n ? middlewares?.afterSignup\n : sendResponse,\n sendResponse\n );\n\n router.post(\n \"/auth/update-password\",\n authService.authenticate,\n handleRequestBodyValidationAndTransformation(\"auth\", \"updatePassword\"),\n middlewares?.beforeUpdatePassword ?? authController.updatePassword,\n middlewares?.beforeUpdatePassword\n ? authController.updatePassword\n : middlewares?.afterUpdatePassword ?? sendResponse,\n middlewares?.beforeUpdatePassword && middlewares?.afterUpdatePassword\n ? middlewares?.afterUpdatePassword\n : sendResponse,\n sendResponse\n );\n\n return router;\n}\n"]}
@@ -33,8 +33,8 @@ class AuthService {
33
33
  var _a, _b, _c;
34
34
  const { authentication: configs } = getArkosConfig();
35
35
  if (process.env.NODE_ENV === "production" &&
36
- !process.env.JWT_SECRET &&
37
- !((_a = configs === null || configs === void 0 ? void 0 : configs.jwt) === null || _a === void 0 ? void 0 : _a.secret))
36
+ (!process.env.JWT_SECRET ||
37
+ !((_a = configs === null || configs === void 0 ? void 0 : configs.jwt) === null || _a === void 0 ? void 0 : _a.secret)))
38
38
  throw new AppError("Missing JWT secret on production!", 500);
39
39
  secret =
40
40
  secret ||
@@ -96,9 +96,9 @@ class AuthService {
96
96
  });
97
97
  });
98
98
  }
99
- handleActionAccessControl(authConfigs, action, modelName) {
99
+ handleActionAccessControl(authConfigs, action, resourceName) {
100
100
  return catchAsync((req, res, next) => __awaiter(this, void 0, void 0, function* () {
101
- var _a, _b, _c, _d;
101
+ var _a, _b, _c, _d, _e;
102
102
  if (req.user) {
103
103
  const user = req.user;
104
104
  const prisma = getPrismaInstance();
@@ -108,14 +108,21 @@ class AuthService {
108
108
  return;
109
109
  }
110
110
  if (((_a = configs === null || configs === void 0 ? void 0 : configs.authentication) === null || _a === void 0 ? void 0 : _a.mode) === "dynamic") {
111
- const permissions = yield prisma.authPermission.count({
111
+ const matchingRole = yield prisma.userRole.findFirst({
112
112
  where: {
113
- resource: kebabCase(singular(modelName)),
114
- action,
115
- roleId: { in: user.roles.map((role) => role.roleId) },
113
+ userId: req.user.id,
114
+ role: {
115
+ permissions: {
116
+ some: {
117
+ resource: kebabCase(singular(resourceName)),
118
+ action: action,
119
+ },
120
+ },
121
+ },
116
122
  },
123
+ select: { id: true },
117
124
  });
118
- if (!permissions)
125
+ if (!matchingRole)
119
126
  return next(new AppError("You do not have permission to perfom this action", 403));
120
127
  }
121
128
  else if (((_b = configs === null || configs === void 0 ? void 0 : configs.authentication) === null || _b === void 0 ? void 0 : _b.mode) === "static") {
@@ -126,9 +133,9 @@ class AuthService {
126
133
  authorizedRoles = accessControl;
127
134
  else if (accessControl[action])
128
135
  authorizedRoles = accessControl[action];
129
- if (!(authorizedRoles === null || authorizedRoles === void 0 ? void 0 : authorizedRoles.includes((_c = req.user) === null || _c === void 0 ? void 0 : _c.role)) ||
130
- (((_d = req.user) === null || _d === void 0 ? void 0 : _d.roles).length > 0 &&
131
- !(authorizedRoles === null || authorizedRoles === void 0 ? void 0 : authorizedRoles.some((role) => { var _a, _b; return (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.roles) === null || _b === void 0 ? void 0 : _b.includes(role); }))))
136
+ if (!((_c = authorizedRoles === null || authorizedRoles === void 0 ? void 0 : authorizedRoles.includes) === null || _c === void 0 ? void 0 : _c.call(authorizedRoles, (_d = req.user) === null || _d === void 0 ? void 0 : _d.role)) ||
137
+ (((_e = req.user) === null || _e === void 0 ? void 0 : _e.roles).length > 0 &&
138
+ !(authorizedRoles === null || authorizedRoles === void 0 ? void 0 : authorizedRoles.some((role) => { var _a, _b, _c; return (_c = (_b = (_a = req.user) === null || _a === void 0 ? void 0 : _a.roles) === null || _b === void 0 ? void 0 : _b.includes) === null || _c === void 0 ? void 0 : _c.call(_b, role); }))))
132
139
  return next(new AppError("You do not have permission to perfom this action", 403));
133
140
  }
134
141
  }
@@ -138,7 +145,7 @@ class AuthService {
138
145
  }
139
146
  getAuthenticatedUser(req) {
140
147
  return __awaiter(this, void 0, void 0, function* () {
141
- var _a, _b, _c, _d, _e, _f;
148
+ var _a, _b, _c, _d, _e, _f, _g;
142
149
  const arkosConfig = getArkosConfig();
143
150
  if (!(arkosConfig === null || arkosConfig === void 0 ? void 0 : arkosConfig.authentication))
144
151
  return null;
@@ -164,22 +171,18 @@ class AuthService {
164
171
  throw new AppError("Your auth token is invalid, please login again.", 401);
165
172
  const user = yield prisma.user.findUnique({
166
173
  where: { id: String(decoded.id) },
167
- include: {
168
- roles: {
169
- include: {
170
- role: {
171
- include: {
172
- permissions: true,
173
- },
174
- },
175
- },
176
- },
174
+ select: {
175
+ id: true,
176
+ passwordChangedAt: true,
177
+ isActive: true,
178
+ deletedSelfAccountAt: true,
179
+ isSuperUser: true,
177
180
  },
178
181
  });
179
182
  if (!user)
180
183
  throw new AppError("The user belonging to this token does no longer exists", 401);
181
184
  if (this.userChangedPasswordAfter(user, decoded.iat) &&
182
- !((_f = req.path) === null || _f === void 0 ? void 0 : _f.includes("logout")))
185
+ !((_g = (_f = req.path) === null || _f === void 0 ? void 0 : _f.includes) === null || _g === void 0 ? void 0 : _g.call(_f, "logout")))
183
186
  throw new AppError("User recently changed password! Please log in again.", 401);
184
187
  return user;
185
188
  });
@@ -1 +1 @@
1
- {"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IAAI,CAAC;QACd,CAAC;KAAA;IA8BD,2BAA2B,CACzB,WAAoC,EACpC,MAAyB;QAEzB,MAAM,qBAAqB,GAAG,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,qBAAqB,CAAC;QAEjE,IAAI,qBAAqB,IAAI,OAAO,qBAAqB,KAAK,QAAQ,EAAE,CAAC;YACvE,IAAI,qBAAqB,CAAC,MAAM,CAAC,KAAK,KAAK;gBAAE,OAAO,QAAQ,CAAC;iBACxD,IAAI,qBAAqB,CAAC,MAAM,CAAC,KAAK,IAAI;gBAAE,OAAO,IAAI,CAAC,YAAY,CAAC;QAC5E,CAAC;;YAAM,OAAO,IAAI,CAAC,YAAY,CAAC;QAEhC,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF;AAKD,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC,eAAe,WAAW,CAAC","sourcesContent":["import jwt, { SignOptions } from \"jsonwebtoken\";\nimport bcrypt from \"bcryptjs\";\nimport { User, UserRole } from \"../../types\";\nimport catchAsync from \"../error-handler/utils/catch-async\";\nimport AppError from \"../error-handler/utils/app-error\";\nimport { callNext } from \"../base/base.middlewares\";\nimport { getArkosConfig } from \"../../server\";\nimport arkosEnv from \"../../utils/arkos-env\";\nimport { getPrismaInstance } from \"../../utils/helpers/prisma.helpers\";\nimport {\n ArkosRequest,\n ArkosResponse,\n ArkosNextFunction,\n ArkosRequestHandler,\n} from \"../../types\";\nimport {\n AuthConfigs,\n AuthJwtPayload,\n ControllerActions,\n} from \"../../types/auth\";\nimport { kebabCase } from \"../../utils/helpers/change-case.helpers\";\nimport { singular } from \"pluralize\";\nimport { MsDuration } from \"./utils/helpers/auth.controller.helpers\";\n\n/**\n * Handles various authentication-related tasks such as JWT signing, password hashing, and verifying user credentials.\n */\nclass AuthService {\n /**\n * Signs a JWT token for the user.\n *\n * @param {number | string} id - The unique identifier of the user to generate the token for.\n * @param {string | number} [expiresIn] - The expiration time for the token. Defaults to environment variable `JWT_EXPIRES_IN`.\n * @param {string} [secret] - The secret key used to sign the token. Defaults to environment variable `JWT_SECRET`.\n * @returns {string} The signed JWT token.\n */\n signJwtToken(\n id: number | string,\n expiresIn?: MsDuration | number,\n secret?: string\n ): string {\n const { authentication: configs } = getArkosConfig();\n\n if (\n process.env.NODE_ENV === \"production\" &&\n !process.env.JWT_SECRET &&\n !configs?.jwt?.secret\n )\n throw new AppError(\"Missing JWT secret on production!\", 500);\n\n secret =\n secret ||\n configs?.jwt?.secret ||\n process.env.JWT_SECRET ||\n arkosEnv.JWT_SECRET;\n\n expiresIn = (expiresIn ||\n configs?.jwt?.expiresIn ||\n process.env.JWT_EXPIRES_IN ||\n arkosEnv.JWT_EXPIRES_IN) as keyof SignOptions[\"expiresIn\"];\n\n return jwt.sign({ id }, secret, {\n expiresIn: expiresIn as any,\n });\n }\n\n /**\n * Compares a candidate password with the stored user password to check if they match.\n *\n * @param {string} candidatePassword - The password provided by the user during login.\n * @param {string} userPassword - The password stored in the database.\n * @returns {Promise<boolean>} Returns true if the passwords match, otherwise false.\n */\n async isCorrectPassword(\n candidatePassword: string,\n userPassword: string\n ): Promise<boolean> {\n return await bcrypt.compare(candidatePassword, userPassword);\n }\n\n /**\n * Hashes a plain text password using bcrypt.\n *\n * @param {string} password - The password to be hashed.\n * @returns {Promise<string>} Returns the hashed password.\n */\n async hashPassword(password: string): Promise<string> {\n return await bcrypt.hash(password, 12);\n }\n\n /**\n * Checks if a password is strong, requiring uppercase, lowercase, and numeric characters as the default.\n *\n * **NB**: You must pay attention when using custom validation with zod or class-validator, try to use the same regex always.\n *\n * **Note**: You can define it when calling arkos.init()\n * ```ts\n * arkos.init({\n * authentication: {\n * passwordValidation:{ regex: /your-desired-regex/, message: 'password must contain...'}\n * }\n * })\n * ```\n *\n * @param {string} password - The password to check.\n * @returns {boolean} Returns true if the password meets the strength criteria, otherwise false.\n */\n public isPasswordStrong(password: string): boolean {\n const initAuthConfigs = getArkosConfig()?.authentication;\n\n const strongPasswordRegex =\n initAuthConfigs?.passwordValidation?.regex ||\n /^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).+$/;\n return strongPasswordRegex.test(password);\n }\n\n /**\n * Checks if a user has changed their password after the JWT was issued.\n *\n * @param {User} user - The user object containing the passwordChangedAt field.\n * @param {number} JWTTimestamp - The timestamp when the JWT was issued.\n * @returns {boolean} Returns true if the user changed their password after the JWT was issued, otherwise false.\n */\n userChangedPasswordAfter(user: User, JWTTimestamp: number): boolean {\n if (user.passwordChangedAt) {\n const convertedTimestamp = parseInt(\n String(user.passwordChangedAt.getTime() / 1000),\n 10\n );\n\n return JWTTimestamp < convertedTimestamp;\n }\n return false;\n }\n\n /**\n * Verifies the authenticity of a JWT token.\n *\n * @param {string} token - The JWT token to verify.\n * @param {string} [secret] - The secret key used to verify the token. Defaults to environment variable `JWT_SECRET`.\n * @returns {Promise<AuthJwtPayload>} Returns the decoded JWT payload if the token is valid.\n * @throws {Error} Throws an error if the token is invalid or expired.\n */\n async verifyJwtToken(\n token: string,\n secret?: string\n ): Promise<AuthJwtPayload> {\n const { authentication: configs } = getArkosConfig();\n\n if (\n process.env.NODE_ENV === \"production\" &&\n !process.env.JWT_SECRET &&\n !configs?.jwt?.secret\n )\n throw new AppError(\"Missing JWT secret!\", 500);\n\n secret =\n secret ||\n configs?.jwt?.secret ||\n process.env.JWT_SECRET ||\n arkosEnv.JWT_SECRET;\n\n return new Promise((resolve, reject) => {\n jwt.verify(token, secret, (err, decoded) => {\n if (err) reject(err);\n else resolve(decoded as AuthJwtPayload);\n });\n });\n }\n\n /**\n * Middleware function to handle access control based on user roles and permissions.\n *\n * @param {AuthConfigs} authConfigs - The configuration object for authentication and access control.\n * @param {ControllerActions} action - The action being performed (e.g., create, update, delete, view).\n * @param {string} modelName - The model name that the action is being performed on (e.g., \"User\", \"Post\").\n * @returns {ArkosRequestHandler} The middleware function that checks if the user has permission to perform the action.\n */\n handleActionAccessControl(\n authConfigs: AuthConfigs,\n action: ControllerActions,\n modelName: string\n ): ArkosRequestHandler {\n return catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n if (req.user) {\n const user = req.user as any;\n const prisma = getPrismaInstance();\n const configs = getArkosConfig();\n\n if (user.isSuperUser) {\n next();\n return;\n }\n\n if (configs?.authentication?.mode === \"dynamic\") {\n const permissions = await (prisma as any).authPermission.count({\n where: {\n resource: kebabCase(singular(modelName)),\n action,\n roleId: { in: user.roles.map((role: UserRole) => role.roleId) },\n },\n });\n\n if (!permissions)\n return next(\n new AppError(\n \"You do not have permission to perfom this action\",\n 403\n )\n );\n } else if (configs?.authentication?.mode === \"static\") {\n const accessControl = authConfigs?.accessControl;\n let authorizedRoles: any[] = [];\n\n if (accessControl) {\n if (Array.isArray(accessControl)) authorizedRoles = accessControl;\n else if (accessControl[action])\n authorizedRoles = accessControl[action];\n\n // Checks for both cases if using single role or multiple roles\n if (\n !authorizedRoles?.includes(req.user?.role) ||\n ((req.user?.roles as any[]).length > 0 &&\n !authorizedRoles?.some((role) =>\n (req.user?.roles as any[])?.includes(role)\n ))\n )\n return next(\n new AppError(\n \"You do not have permission to perfom this action\",\n 403\n )\n );\n }\n }\n }\n\n next();\n }\n );\n }\n\n /**\n * Processes the cookies or authoriation token and returns the user.\n * @param req\n * @returns {Promise<User | null>} - if authentication is turned off in arkosConfig it returns null\n * @throws {AppError} Throws an error if the token is invalid or the user is not logged in.\n */\n async getAuthenticatedUser(req: ArkosRequest): Promise<User | null> {\n const arkosConfig = getArkosConfig();\n if (!arkosConfig?.authentication) return null;\n\n const prisma = getPrismaInstance();\n\n let token: string | undefined;\n\n if (\n req?.headers?.authorization &&\n req?.headers?.authorization.startsWith(\"Bearer\")\n ) {\n token = req?.headers?.authorization.split(\" \")[1];\n } else if (req?.cookies?.arkos_access_token !== \"no-token\" && req.cookies) {\n token = req?.cookies?.arkos_access_token;\n }\n\n if (!token)\n throw new AppError(\n \"You are not logged in! please log in to get access\",\n 401\n );\n\n let decoded: AuthJwtPayload | undefined;\n try {\n decoded = await this.verifyJwtToken(token);\n } catch (err) {\n throw new AppError(\n \"Your auth token is invalid, please login again.\",\n 401\n );\n }\n\n if (!decoded?.id)\n throw new AppError(\n \"Your auth token is invalid, please login again.\",\n 401\n );\n\n const user: any | null = await (prisma as any).user.findUnique({\n where: { id: String(decoded.id) },\n include: {\n roles: {\n include: {\n role: {\n include: {\n permissions: true,\n },\n },\n },\n },\n },\n });\n\n if (!user)\n throw new AppError(\n \"The user belonging to this token does no longer exists\",\n 401\n );\n\n if (\n this.userChangedPasswordAfter(user, decoded.iat!) &&\n !req.path?.includes(\"logout\")\n )\n throw new AppError(\n \"User recently changed password! Please log in again.\",\n 401\n );\n\n return user;\n }\n\n /**\n * Middleware function to authenticate the user based on the JWT token.\n *\n * @param {ArkosRequest} req - The request object.\n * @param {ArkosResponse} res - The response object.\n * @param {ArkosNextFunction} next - The next middleware function to be called.\n * @returns {void}\n */\n authenticate = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n const arkosConfig = getArkosConfig();\n if (!arkosConfig?.authentication) {\n next();\n return;\n }\n\n req.user = (await this.getAuthenticatedUser(req)) as User;\n next();\n }\n );\n\n /**\n * Handles authentication control by checking the `authenticationControl` configuration in the `authConfigs`.\n *\n * @param {AuthConfigs | undefined} authConfigs - The authentication configuration object.\n * @param {ControllerActions} action - The action being performed (e.g., create, update, delete, view).\n * @returns {ArkosRequestHandler} The middleware function that checks if authentication is required.\n */\n handleAuthenticationControl(\n authConfigs: AuthConfigs | undefined,\n action: ControllerActions\n ): ArkosRequestHandler {\n const authenticationControl = authConfigs?.authenticationControl;\n\n if (authenticationControl && typeof authenticationControl === \"object\") {\n if (authenticationControl[action] === false) return callNext;\n else if (authenticationControl[action] === true) return this.authenticate;\n } else return this.authenticate;\n\n return this.authenticate;\n }\n}\n\n/**\n * Handles various authentication-related tasks such as JWT signing, password hashing, and verifying user credentials.\n */\nconst authService = new AuthService();\n\nexport default authService;\n"]}
1
+ {"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../../../../src/modules/auth/auth.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,GAAoB,MAAM,cAAc,CAAC;AAChD,OAAO,MAAM,MAAM,UAAU,CAAC;AAE9B,OAAO,UAAU,MAAM,oCAAoC,CAAC;AAC5D,OAAO,QAAQ,MAAM,kCAAkC,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,QAAQ,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AAYvE,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAMrC,MAAM,WAAW;IAAjB;QAqTE,iBAAY,GAAG,UAAU,CACvB,CAAO,GAAiB,EAAE,GAAkB,EAAE,IAAuB,EAAE,EAAE;YACvE,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;YACrC,IAAI,CAAC,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,cAAc,CAAA,EAAE,CAAC;gBACjC,IAAI,EAAE,CAAC;gBACP,OAAO;YACT,CAAC;YAED,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAS,CAAC;YAC1D,IAAI,EAAE,CAAC;QACT,CAAC,CAAA,CACF,CAAC;IAsBJ,CAAC;IA7UC,YAAY,CACV,EAAmB,EACnB,SAA+B,EAC/B,MAAe;;QAEf,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,GAAG,cAAc,EAAE,CAAC;QAErD,IACE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;YACrC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU;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CAAC,IAAI,0CAAE,QAAQ,mDAAG,QAAQ,CAAC,CAAA;gBAE/B,MAAM,IAAI,QAAQ,CAChB,sDAAsD,EACtD,GAAG,CACJ,CAAC;YAEJ,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IA8BD,2BAA2B,CACzB,WAAoC,EACpC,MAA6B;QAE7B,MAAM,qBAAqB,GAAG,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,qBAAqB,CAAC;QAEjE,IAAI,qBAAqB,IAAI,OAAO,qBAAqB,KAAK,QAAQ,EAAE,CAAC;YACvE,IAAI,qBAAqB,CAAC,MAAM,CAAC,KAAK,KAAK;gBAAE,OAAO,QAAQ,CAAC;iBACxD,IAAI,qBAAqB,CAAC,MAAM,CAAC,KAAK,IAAI;gBAAE,OAAO,IAAI,CAAC,YAAY,CAAC;QAC5E,CAAC;;YAAM,OAAO,IAAI,CAAC,YAAY,CAAC;QAEhC,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;CACF;AAKD,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC,eAAe,WAAW,CAAC","sourcesContent":["import jwt, { SignOptions } from \"jsonwebtoken\";\nimport bcrypt from \"bcryptjs\";\nimport { AuthPermissionAction, User, UserRole } from \"../../types\";\nimport catchAsync from \"../error-handler/utils/catch-async\";\nimport AppError from \"../error-handler/utils/app-error\";\nimport { callNext } from \"../base/base.middlewares\";\nimport { getArkosConfig } from \"../../server\";\nimport arkosEnv from \"../../utils/arkos-env\";\nimport { getPrismaInstance } from \"../../utils/helpers/prisma.helpers\";\nimport {\n ArkosRequest,\n ArkosResponse,\n ArkosNextFunction,\n ArkosRequestHandler,\n} from \"../../types\";\nimport {\n AuthConfigs,\n AuthJwtPayload,\n BaseControllerActions,\n} from \"../../types/auth\";\nimport { kebabCase } from \"../../utils/helpers/change-case.helpers\";\nimport { singular } from \"pluralize\";\nimport { MsDuration } from \"./utils/helpers/auth.controller.helpers\";\n\n/**\n * Handles various authentication-related tasks such as JWT signing, password hashing, and verifying user credentials.\n */\nclass AuthService {\n /**\n * Signs a JWT token for the user.\n *\n * @param {number | string} id - The unique identifier of the user to generate the token for.\n * @param {string | number} [expiresIn] - The expiration time for the token. Defaults to environment variable `JWT_EXPIRES_IN`.\n * @param {string} [secret] - The secret key used to sign the token. Defaults to environment variable `JWT_SECRET`.\n * @returns {string} The signed JWT token.\n */\n signJwtToken(\n id: number | string,\n expiresIn?: MsDuration | number,\n secret?: string\n ): string {\n const { authentication: configs } = getArkosConfig();\n\n if (\n process.env.NODE_ENV === \"production\" &&\n (!process.env.JWT_SECRET ||\n !configs?.jwt?.secret)\n )\n throw new AppError(\"Missing JWT secret on production!\", 500);\n\n secret =\n secret ||\n configs?.jwt?.secret ||\n process.env.JWT_SECRET ||\n arkosEnv.JWT_SECRET;\n\n expiresIn = (expiresIn ||\n configs?.jwt?.expiresIn ||\n process.env.JWT_EXPIRES_IN ||\n arkosEnv.JWT_EXPIRES_IN) as keyof SignOptions[\"expiresIn\"];\n\n return jwt.sign({ id }, secret, {\n expiresIn: expiresIn as MsDuration,\n });\n }\n\n /**\n * Compares a candidate password with the stored user password to check if they match.\n *\n * @param {string} candidatePassword - The password provided by the user during login.\n * @param {string} userPassword - The password stored in the database.\n * @returns {Promise<boolean>} Returns true if the passwords match, otherwise false.\n */\n async isCorrectPassword(\n candidatePassword: string,\n userPassword: string\n ): Promise<boolean> {\n return await bcrypt.compare(candidatePassword, userPassword);\n }\n\n /**\n * Hashes a plain text password using bcrypt.\n *\n * @param {string} password - The password to be hashed.\n * @returns {Promise<string>} Returns the hashed password.\n */\n async hashPassword(password: string): Promise<string> {\n return await bcrypt.hash(password, 12);\n }\n\n /**\n * Checks if a password is strong, requiring uppercase, lowercase, and numeric characters as the default.\n *\n * **NB**: You must pay attention when using custom validation with zod or class-validator, try to use the same regex always.\n *\n * **Note**: You can define it when calling arkos.init()\n * ```ts\n * arkos.init({\n * authentication: {\n * passwordValidation:{ regex: /your-desired-regex/, message: 'password must contain...'}\n * }\n * })\n * ```\n *\n * @param {string} password - The password to check.\n * @returns {boolean} Returns true if the password meets the strength criteria, otherwise false.\n */\n public isPasswordStrong(password: string): boolean {\n const initAuthConfigs = getArkosConfig()?.authentication;\n\n const strongPasswordRegex =\n initAuthConfigs?.passwordValidation?.regex ||\n /^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).+$/;\n return strongPasswordRegex.test(password);\n }\n\n /**\n * Checks if a user has changed their password after the JWT was issued.\n *\n * @param {User} user - The user object containing the passwordChangedAt field.\n * @param {number} JWTTimestamp - The timestamp when the JWT was issued.\n * @returns {boolean} Returns true if the user changed their password after the JWT was issued, otherwise false.\n */\n userChangedPasswordAfter(user: User, JWTTimestamp: number): boolean {\n if (user.passwordChangedAt) {\n const convertedTimestamp = parseInt(\n String(user.passwordChangedAt.getTime() / 1000),\n 10\n );\n\n return JWTTimestamp < convertedTimestamp;\n }\n return false;\n }\n\n /**\n * Verifies the authenticity of a JWT token.\n *\n * @param {string} token - The JWT token to verify.\n * @param {string} [secret] - The secret key used to verify the token. Defaults to environment variable `JWT_SECRET`.\n * @returns {Promise<AuthJwtPayload>} Returns the decoded JWT payload if the token is valid.\n * @throws {Error} Throws an error if the token is invalid or expired.\n */\n async verifyJwtToken(\n token: string,\n secret?: string\n ): Promise<AuthJwtPayload> {\n const { authentication: configs } = getArkosConfig();\n\n if (\n process.env.NODE_ENV === \"production\" &&\n !process.env.JWT_SECRET &&\n !configs?.jwt?.secret\n )\n throw new AppError(\"Missing JWT secret!\", 500);\n\n secret =\n secret ||\n configs?.jwt?.secret ||\n process.env.JWT_SECRET ||\n arkosEnv.JWT_SECRET;\n\n return new Promise((resolve, reject) => {\n jwt.verify(token, secret, (err, decoded) => {\n if (err) reject(err);\n else resolve(decoded as AuthJwtPayload);\n });\n });\n }\n\n /**\n * Middleware function to handle access control based on user roles and permissions.\n *\n * @param {AuthConfigs} authConfigs - The configuration object for authentication and access control.\n * @param {ControllerActions} action - The action being performed (e.g., create, update, delete, view).\n * @param {string} resourceName - The resource name that the action is being performed on (e.g., \"User\", \"Post\").\n * @returns {ArkosRequestHandler} The middleware function that checks if the user has permission to perform the action.\n */\n handleActionAccessControl(\n authConfigs: AuthConfigs,\n action: BaseControllerActions,\n resourceName: string\n ): ArkosRequestHandler {\n return catchAsync(\n async (\n req: ArkosRequest,\n res: ArkosResponse,\n next: ArkosNextFunction\n ) => {\n if (req.user) {\n const user = req.user as any;\n const prisma = getPrismaInstance();\n const configs = getArkosConfig();\n\n if (user.isSuperUser) {\n next();\n return;\n }\n\n if (configs?.authentication?.mode === \"dynamic\") {\n const matchingRole = await prisma.userRole.findFirst({\n where: {\n userId: req.user.id,\n role: {\n permissions: {\n some: {\n resource: kebabCase(singular(resourceName)),\n action: action,\n },\n },\n },\n },\n select: { id: true },\n });\n\n if (!matchingRole)\n return next(\n new AppError(\n \"You do not have permission to perfom this action\",\n 403\n )\n );\n } else if (configs?.authentication?.mode === \"static\") {\n const accessControl = authConfigs?.accessControl;\n let authorizedRoles: any[] = [];\n\n if (accessControl) {\n if (Array.isArray(accessControl)) authorizedRoles = accessControl;\n else if (accessControl[action])\n authorizedRoles = accessControl[action];\n\n // Checks for both cases if using single role or multiple roles\n if (\n !authorizedRoles?.includes?.(req.user?.role) ||\n ((req.user?.roles as any[]).length > 0 &&\n !authorizedRoles?.some((role) =>\n (req.user?.roles as any[])?.includes?.(role)\n ))\n )\n return next(\n new AppError(\n \"You do not have permission to perfom this action\",\n 403\n )\n );\n }\n }\n }\n\n next();\n }\n );\n }\n\n /**\n * Processes the cookies or authoriation token and returns the user.\n * @param req\n * @returns {Promise<User | null>} - if authentication is turned off in arkosConfig it returns null\n * @throws {AppError} Throws an error if the token is invalid or the user is not logged in.\n */\n async getAuthenticatedUser(req: ArkosRequest): Promise<User | null> {\n const arkosConfig = getArkosConfig();\n if (!arkosConfig?.authentication) return null;\n\n const prisma = getPrismaInstance();\n\n let token: string | undefined;\n\n if (\n req?.headers?.authorization &&\n req?.headers?.authorization.startsWith(\"Bearer\")\n ) {\n token = req?.headers?.authorization.split(\" \")[1];\n } else if (req?.cookies?.arkos_access_token !== \"no-token\" && req.cookies) {\n token = req?.cookies?.arkos_access_token;\n }\n\n if (!token)\n throw new AppError(\n \"You are not logged in! please log in to get access\",\n 401\n );\n\n let decoded: AuthJwtPayload | undefined;\n try {\n decoded = await this.verifyJwtToken(token);\n } catch (err) {\n throw new AppError(\n \"Your auth token is invalid, please login again.\",\n 401\n );\n }\n\n if (!decoded?.id)\n throw new AppError(\n \"Your auth token is invalid, please login again.\",\n 401\n );\n\n const user: any | null = await (prisma as any).user.findUnique({\n where: { id: String(decoded.id) },\n select: {\n id: true,\n passwordChangedAt: true,\n isActive: true,\n deletedSelfAccountAt: true,\n isSuperUser: true,\n },\n });\n\n if (!user)\n throw new AppError(\n \"The user belonging to this token does no longer exists\",\n 401\n );\n\n if (\n this.userChangedPasswordAfter(user, decoded.iat!) &&\n !req.path?.includes?.(\"logout\")\n )\n throw new AppError(\n \"User recently changed password! Please log in again.\",\n 401\n );\n\n return user;\n }\n\n /**\n * Middleware function to authenticate the user based on the JWT token.\n *\n * @param {ArkosRequest} req - The request object.\n * @param {ArkosResponse} res - The response object.\n * @param {ArkosNextFunction} next - The next middleware function to be called.\n * @returns {void}\n */\n authenticate = catchAsync(\n async (req: ArkosRequest, res: ArkosResponse, next: ArkosNextFunction) => {\n const arkosConfig = getArkosConfig();\n if (!arkosConfig?.authentication) {\n next();\n return;\n }\n\n req.user = (await this.getAuthenticatedUser(req)) as User;\n next();\n }\n );\n\n /**\n * Handles authentication control by checking the `authenticationControl` configuration in the `authConfigs`.\n *\n * @param {AuthConfigs | undefined} authConfigs - The authentication configuration object.\n * @param {ControllerActions} action - The action being performed (e.g., create, update, delete, view).\n * @returns {ArkosRequestHandler} The middleware function that checks if authentication is required.\n */\n handleAuthenticationControl(\n authConfigs: AuthConfigs | undefined,\n action: BaseControllerActions\n ): ArkosRequestHandler {\n const authenticationControl = authConfigs?.authenticationControl;\n\n if (authenticationControl && typeof authenticationControl === \"object\") {\n if (authenticationControl[action] === false) return callNext;\n else if (authenticationControl[action] === true) return this.authenticate;\n } else return this.authenticate;\n\n return this.authenticate;\n }\n}\n\n/**\n * Handles various authentication-related tasks such as JWT signing, password hashing, and verifying user credentials.\n */\nconst authService = new AuthService();\n\nexport default authService;\n"]}
@@ -1,15 +1,15 @@
1
1
  import { getArkosConfig } from "../../../../server";
2
2
  import AppError from "../../../error-handler/utils/app-error";
3
3
  export const determineUsernameField = (req) => {
4
- var _a, _b, _c, _d, _e, _f, _g, _h;
4
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
5
5
  const authConfigs = (_a = getArkosConfig()) === null || _a === void 0 ? void 0 : _a.authentication;
6
6
  if (((_b = req.query) === null || _b === void 0 ? void 0 : _b.usernameField) &&
7
7
  typeof ((_c = req.query) === null || _c === void 0 ? void 0 : _c.usernameField) === "string" &&
8
- ((_e = (_d = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _d === void 0 ? void 0 : _d.allowedUsernames) === null || _e === void 0 ? void 0 : _e.includes(req.query.usernameField)))
8
+ ((_f = (_e = (_d = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _d === void 0 ? void 0 : _d.allowedUsernames) === null || _e === void 0 ? void 0 : _e.includes) === null || _f === void 0 ? void 0 : _f.call(_e, req.query.usernameField)))
9
9
  return req.query.usernameField;
10
- else if ((_f = req.query) === null || _f === void 0 ? void 0 : _f.usernameField)
10
+ else if ((_g = req.query) === null || _g === void 0 ? void 0 : _g.usernameField)
11
11
  throw new AppError("Invalid usernameField parameter, it is not allowed!", 400);
12
- return ((_h = (_g = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _g === void 0 ? void 0 : _g.allowedUsernames) === null || _h === void 0 ? void 0 : _h[0]) || "username";
12
+ return ((_j = (_h = authConfigs === null || authConfigs === void 0 ? void 0 : authConfigs.login) === null || _h === void 0 ? void 0 : _h.allowedUsernames) === null || _j === void 0 ? void 0 : _j[0]) || "username";
13
13
  };
14
14
  export const createPrismaWhereClause = (path, value) => {
15
15
  if (!path)
@@ -1 +1 @@
1
- {"version":3,"file":"auth.controller.helpers.js","sourceRoot":"","sources":["../../../../../../src/modules/auth/utils/helpers/auth.controller.helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,QAAQ,MAAM,wCAAwC,CAAC;AAc9D,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAiB,EAAU,EAAE;;IAClE,MAAM,WAAW,GAAG,MAAA,cAAc,EAAE,0CAAE,cAAc,CAAC;IAErD,IACE,CAAA,MAAA,GAAG,CAAC,KAAK,0CAAE,aAAa;QACxB,OAAO,CAAA,MAAA,GAAG,CAAC,KAAK,0CAAE,aAAa,CAAA,KAAK,QAAQ;SAC5C,MAAA,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,KAAK,0CAAE,gBAAgB,0CAAE,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;QAEvE,OAAO,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC;SAC5B,IAAI,MAAA,GAAG,CAAC,KAAK,0CAAE,aAAa;QAC/B,MAAM,IAAI,QAAQ,CAChB,qDAAqD,EACrD,GAAG,CACJ,CAAC;IAEJ,OAAO,CAAA,MAAA,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,KAAK,0CAAE,gBAAgB,0CAAG,CAAC,CAAC,KAAI,UAAU,CAAC;AACjE,CAAC,CAAC;AAWF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,IAAY,EACZ,KAAU,EACW,EAAE;IACvB,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IAErB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,WAAW,G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,EAAE,GAAG,CAAC;SAClC,OAAO,CAAC,YAAY,EAAE,GAAG,CAAC;SAC1B,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;SACtB,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;SACvB,OAAO,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,KAA0B;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,GAAG,IAAI,CAAC;IAGnD,MAAM,eAAe,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAGxD,MAAM,KAAK,GAAG,oCAAoC,CAAC;IACnD,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAE3C,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC;IAE7D,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAO1B,CAAC;IAER,MAAM,WAAW,GACf;QACE,EAAE,EAAE,CAAC;QACL,CAAC,EAAE,IAAI;QACP,CAAC,EAAE,KAAK;QACR,CAAC,EAAE,OAAO;QACV,CAAC,EAAE,QAAQ;QACX,CAAC,EAAE,SAAS;QACZ,CAAC,EAAE,WAAW;KACf,CAAC;IAEJ,OAAO,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import { getArkosConfig } from \"../../../../server\";\nimport { ArkosRequest } from \"../../../../types\";\nimport AppError from \"../../../error-handler/utils/app-error\";\n\n/**\n * Determines the username field to use for authentication and supports nested paths\n * Priority:\n * 1. req.query.usernameField\n * 2. config setting\n * 3. default \"username\"\n *\n * Supports dot notation for nested fields and array queries (e.g., \"profile.nickname\", \"phones.some.number\")\n *\n * @param req - The request object\n * @returns The field name to use for username identification\n */\nexport const determineUsernameField = (req: ArkosRequest): string => {\n const authConfigs = getArkosConfig()?.authentication;\n\n if (\n req.query?.usernameField &&\n typeof req.query?.usernameField === \"string\" &&\n authConfigs?.login?.allowedUsernames?.includes(req.query.usernameField)\n )\n return req.query.usernameField;\n else if (req.query?.usernameField)\n throw new AppError(\n \"Invalid usernameField parameter, it is not allowed!\",\n 400\n );\n\n return authConfigs?.login?.allowedUsernames?.[0] || \"username\";\n};\n\n/**\n * Creates a Prisma-compatible where clause from a path using dot notation\n * Handles nested objects and array queries with \"some\" operator\n * Example: createPrismaWhereClause(\"profile.nickname\", \"john\") or createPrismaWhereClause(\"phones.some.number\", \"1234567890\")\n *\n * @param path - The dot notation path (e.g., \"profile.nickname\" or \"phones.some.number\")\n * @param value - The value to search for\n * @returns A nested object suitable for Prisma's where clause\n */\nexport const createPrismaWhereClause = (\n path: string,\n value: any\n): Record<string, any> => {\n if (!path) return {};\n\n const parts = path.split(\".\");\n const whereClause: Record<string, any> = {};\n\n // Handle simple field case\n if (parts.length === 1) {\n whereClause[parts[0]] = value;\n return whereClause;\n }\n\n // Handle nested fields\n let current = whereClause;\n for (let i = 0; i < parts.length - 1; i++) {\n const part = parts[i];\n current[part] = {};\n current = current[part];\n }\n\n current[parts[parts.length - 1]] = value;\n\n return whereClause;\n};\n\n/**\n * Access a value from a simple object based on a dot notation path\n * The object will only contain a single property that matches the last part of the path\n * Example: getNestedValue({nickname: \"john\"}, \"profile.nickname\") => \"john\"\n *\n * @param obj - The object containing the value (simple key-value pair)\n * @param path - The dot notation path (only the last part is used to access the object)\n * @returns The value from the object if the key matches the last part of the path, or undefined\n */\nexport const getNestedValue = (obj: any, path: string): any => {\n if (!obj || !path) return undefined;\n\n const properties = path.split(\".\");\n const lastProperty = properties[properties.length - 1];\n\n // If the last property exists in the object, return its value\n if (lastProperty in obj) {\n return obj[lastProperty];\n }\n\n return undefined;\n};\n\n/**\n * MsDuration type allows specific units for durations\n *\n * **For example**: 90d, 10ms, 50s.\n *\n * **Available metrics**: ms, s, m, h, d, w, y.\n * */\nexport type MsDuration =\n | number\n | `${number}`\n | `${number}${\"ms\" | \"s\" | \"m\" | \"h\" | \"d\" | \"w\" | \"y\"}`; // Short format (e.g., \"1y\")\n\n// Normalize function to convert long units to short ones\nfunction normalizeDuration(input: string): string {\n return input\n .toLowerCase()\n .replace(/years?|yr|year/g, \"y\")\n .replace(/minutes?|min/g, \"m\")\n .replace(/seconds?|sec|secs/g, \"s\")\n .replace(/hours?|hr/g, \"h\")\n .replace(/days?/g, \"d\")\n .replace(/weeks?/g, \"w\")\n .replace(/milliseconds?/g, \"ms\");\n}\n\nexport function toMs(input: number | MsDuration): number {\n if (typeof input === \"number\") return input * 1000; // If it's a number, assume it's in seconds\n\n // Normalize the string input\n const normalizedInput = normalizeDuration(input.trim());\n\n // Type assertion: we assert that normalizedInput will now match the MsDuration format\n const regex = /^(\\d+(?:\\.\\d+)?)(ms|s|m|h|d|w|y)$/i;\n const match = normalizedInput.match(regex);\n\n if (!match) throw new Error(`Invalid time format: ${input}`);\n\n const value = parseFloat(match[1]);\n const unit = match[2].toLowerCase() as\n | \"ms\"\n | \"s\"\n | \"m\"\n | \"h\"\n | \"d\"\n | \"w\"\n | \"y\";\n\n const multipliers: Record<\"ms\" | \"s\" | \"m\" | \"h\" | \"d\" | \"w\" | \"y\", number> =\n {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n y: 31557600000, // 365.25 days in ms\n };\n\n return value * multipliers[unit];\n}\n"]}
1
+ {"version":3,"file":"auth.controller.helpers.js","sourceRoot":"","sources":["../../../../../../src/modules/auth/utils/helpers/auth.controller.helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,QAAQ,MAAM,wCAAwC,CAAC;AAc9D,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC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,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAGxD,MAAM,KAAK,GAAG,oCAAoC,CAAC;IACnD,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAE3C,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,KAAK,EAAE,CAAC,CAAC;IAE7D,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAO1B,CAAC;IAER,MAAM,WAAW,GACf;QACE,EAAE,EAAE,CAAC;QACL,CAAC,EAAE,IAAI;QACP,CAAC,EAAE,KAAK;QACR,CAAC,EAAE,OAAO;QACV,CAAC,EAAE,QAAQ;QACX,CAAC,EAAE,SAAS;QACZ,CAAC,EAAE,WAAW;KACf,CAAC;IAEJ,OAAO,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import { getArkosConfig } from \"../../../../server\";\nimport { ArkosRequest } from \"../../../../types\";\nimport AppError from \"../../../error-handler/utils/app-error\";\n\n/**\n * Determines the username field to use for authentication and supports nested paths\n * Priority:\n * 1. req.query.usernameField\n * 2. config setting\n * 3. default \"username\"\n *\n * Supports dot notation for nested fields and array queries (e.g., \"profile.nickname\", \"phones.some.number\")\n *\n * @param req - The request object\n * @returns The field name to use for username identification\n */\nexport const determineUsernameField = (req: ArkosRequest): string => {\n const authConfigs = getArkosConfig()?.authentication;\n\n if (\n req.query?.usernameField &&\n typeof req.query?.usernameField === \"string\" &&\n authConfigs?.login?.allowedUsernames?.includes?.(req.query.usernameField)\n )\n return req.query.usernameField;\n else if (req.query?.usernameField)\n throw new AppError(\n \"Invalid usernameField parameter, it is not allowed!\",\n 400\n );\n\n return authConfigs?.login?.allowedUsernames?.[0] || \"username\";\n};\n\n/**\n * Creates a Prisma-compatible where clause from a path using dot notation\n * Handles nested objects and array queries with \"some\" operator\n * Example: createPrismaWhereClause(\"profile.nickname\", \"john\") or createPrismaWhereClause(\"phones.some.number\", \"1234567890\")\n *\n * @param path - The dot notation path (e.g., \"profile.nickname\" or \"phones.some.number\")\n * @param value - The value to search for\n * @returns A nested object suitable for Prisma's where clause\n */\nexport const createPrismaWhereClause = (\n path: string,\n value: any\n): Record<string, any> => {\n if (!path) return {};\n\n const parts = path.split(\".\");\n const whereClause: Record<string, any> = {};\n\n // Handle simple field case\n if (parts.length === 1) {\n whereClause[parts[0]] = value;\n return whereClause;\n }\n\n // Handle nested fields\n let current = whereClause;\n for (let i = 0; i < parts.length - 1; i++) {\n const part = parts[i];\n current[part] = {};\n current = current[part];\n }\n\n current[parts[parts.length - 1]] = value;\n\n return whereClause;\n};\n\n/**\n * Access a value from a simple object based on a dot notation path\n * The object will only contain a single property that matches the last part of the path\n * Example: getNestedValue({nickname: \"john\"}, \"profile.nickname\") => \"john\"\n *\n * @param obj - The object containing the value (simple key-value pair)\n * @param path - The dot notation path (only the last part is used to access the object)\n * @returns The value from the object if the key matches the last part of the path, or undefined\n */\nexport const getNestedValue = (obj: any, path: string): any => {\n if (!obj || !path) return undefined;\n\n const properties = path.split(\".\");\n const lastProperty = properties[properties.length - 1];\n\n // If the last property exists in the object, return its value\n if (lastProperty in obj) {\n return obj[lastProperty];\n }\n\n return undefined;\n};\n\n/**\n * MsDuration type allows specific units for durations\n *\n * **For example**: 90d, 10ms, 50s.\n *\n * **Available metrics**: ms, s, m, h, d, w, y.\n * */\nexport type MsDuration =\n | number\n | `${number}`\n | `${number}${\"ms\" | \"s\" | \"m\" | \"h\" | \"d\" | \"w\" | \"y\"}`; // Short format (e.g., \"1y\")\n\n// Normalize function to convert long units to short ones\nfunction normalizeDuration(input: string): string {\n return input\n .toLowerCase()\n .replace(/years?|yr|year/g, \"y\")\n .replace(/minutes?|min/g, \"m\")\n .replace(/seconds?|sec|secs/g, \"s\")\n .replace(/hours?|hr/g, \"h\")\n .replace(/days?/g, \"d\")\n .replace(/weeks?/g, \"w\")\n .replace(/milliseconds?/g, \"ms\");\n}\n\nexport function toMs(input: number | MsDuration): number {\n if (typeof input === \"number\") return input * 1000; // If it's a number, assume it's in seconds\n\n // Normalize the string input\n const normalizedInput = normalizeDuration(input.trim());\n\n // Type assertion: we assert that normalizedInput will now match the MsDuration format\n const regex = /^(\\d+(?:\\.\\d+)?)(ms|s|m|h|d|w|y)$/i;\n const match = normalizedInput.match(regex);\n\n if (!match) throw new Error(`Invalid time format: ${input}`);\n\n const value = parseFloat(match[1]);\n const unit = match[2].toLowerCase() as\n | \"ms\"\n | \"s\"\n | \"m\"\n | \"h\"\n | \"d\"\n | \"w\"\n | \"y\";\n\n const multipliers: Record<\"ms\" | \"s\" | \"m\" | \"h\" | \"d\" | \"w\" | \"y\", number> =\n {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n y: 31557600000, // 365.25 days in ms\n };\n\n return value * multipliers[unit];\n}\n"]}
@@ -12,8 +12,8 @@ import APIFeatures from "../../utils/features/api.features";
12
12
  import { BaseService } from "./base.service";
13
13
  import AppError from "../error-handler/utils/app-error";
14
14
  import { kebabCase } from "../../utils/helpers/change-case.helpers";
15
- import { getExpressApp } from "../../server";
16
15
  import { getModelModules, getModels } from "../../utils/helpers/models.helpers";
16
+ import { getAppRoutes } from "../../utils/helpers/base.controller.helpers";
17
17
  export class BaseController {
18
18
  constructor(modelName) {
19
19
  var _a;
@@ -121,34 +121,7 @@ export class BaseController {
121
121
  }
122
122
  }
123
123
  export function getAvalibleRoutes(req, res, next) {
124
- const routes = [];
125
- req.params;
126
- const app = getExpressApp();
127
- app._router.stack.forEach((middleware) => {
128
- if (middleware.route) {
129
- Object.keys(middleware.route.methods).forEach((method) => {
130
- routes.push({
131
- method: method.toUpperCase(),
132
- path: middleware.route.path,
133
- });
134
- });
135
- }
136
- else if (middleware.handle && middleware.handle.stack) {
137
- middleware.handle.stack.forEach((routerMiddleware) => {
138
- if (routerMiddleware.route) {
139
- Object.keys(routerMiddleware.route.methods).forEach((method) => {
140
- const fullPath = (middleware.regexp
141
- ? middleware.regexp.toString().replace("/", "")
142
- : "") + routerMiddleware.route.path;
143
- routes.push({
144
- method: method.toUpperCase(),
145
- path: routerMiddleware.route.path,
146
- });
147
- });
148
- }
149
- });
150
- }
151
- });
124
+ const routes = getAppRoutes();
152
125
  res.json(routes);
153
126
  }
154
127
  export const getAvailableResources = catchAsync((req, res, next) => __awaiter(void 0, void 0, void 0, function* () {