aaspai-authx 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,3 +1,3 @@
1
- MIT License
2
-
1
+ MIT License
2
+
3
3
  (See standard MIT text)
@@ -43,6 +43,7 @@ var import_bcryptjs = __toESM(require("bcryptjs"), 1);
43
43
  var import_crypto = require("crypto");
44
44
  var import_express = __toESM(require("express"), 1);
45
45
  var import_jsonwebtoken4 = __toESM(require("jsonwebtoken"), 1);
46
+ var import_mongoose5 = __toESM(require("mongoose"), 1);
46
47
 
47
48
  // src/config/loadConfig.ts
48
49
  function loadConfig() {
@@ -243,7 +244,7 @@ var MetadataSchema = new import_mongoose2.default.Schema(
243
244
  );
244
245
  var OrgUserSchema = new import_mongoose2.default.Schema(
245
246
  {
246
- id: { type: String, default: (0, import_uuid.v4)(), index: true, unique: true },
247
+ id: { type: String, default: import_uuid.v4, index: true, unique: true },
247
248
  email: { type: String, required: true, unique: true },
248
249
  firstName: { type: String, required: true },
249
250
  lastName: { type: String, required: true },
@@ -251,6 +252,12 @@ var OrgUserSchema = new import_mongoose2.default.Schema(
251
252
  projectId: { type: String, required: true },
252
253
  roles: { type: [String], default: [] },
253
254
  emailVerified: { type: Boolean, default: false },
255
+ status: {
256
+ type: String,
257
+ enum: ["active", "inactive", "suspended", "banned", "pending"],
258
+ default: "active",
259
+ index: true
260
+ },
254
261
  lastEmailSent: { type: [Date], default: [] },
255
262
  lastPasswordReset: { type: Date },
256
263
  metadata: { type: [MetadataSchema], default: [] },
@@ -258,6 +265,12 @@ var OrgUserSchema = new import_mongoose2.default.Schema(
258
265
  },
259
266
  { timestamps: true, collection: "users" }
260
267
  );
268
+ OrgUserSchema.index({ projectId: 1, createdAt: -1 });
269
+ OrgUserSchema.index({ projectId: 1, updatedAt: -1 });
270
+ OrgUserSchema.index({ projectId: 1, email: 1 });
271
+ OrgUserSchema.index({ projectId: 1, emailVerified: 1, createdAt: -1 });
272
+ OrgUserSchema.index({ projectId: 1, status: 1, createdAt: -1 });
273
+ OrgUserSchema.index({ projectId: 1, firstName: 1, lastName: 1 });
261
274
  var OrgUser = import_mongoose2.default.model("OrgUser", OrgUserSchema);
262
275
 
263
276
  // src/utils/extract.ts
@@ -1666,6 +1679,238 @@ function createAuthRouter(options = {}) {
1666
1679
  const user = await OrgUser.find({ projectId: req.query.projectId }).lean();
1667
1680
  res.json(user || null);
1668
1681
  });
1682
+ r.get("/get-users-by-pagination", async (req, res) => {
1683
+ try {
1684
+ const projectId = String(req.query.projectId || "");
1685
+ if (!projectId) {
1686
+ return res.status(400).json({
1687
+ ok: false,
1688
+ error: "projectId is required"
1689
+ });
1690
+ }
1691
+ const page = Math.max(
1692
+ 1,
1693
+ parseInt(String(req.query.page || "1"), 10) || 1
1694
+ );
1695
+ const limit = Math.min(
1696
+ 200,
1697
+ Math.max(1, parseInt(String(req.query.limit || "20"), 10) || 20)
1698
+ );
1699
+ const skip = (page - 1) * limit;
1700
+ const allowedSortFields = [
1701
+ "createdAt",
1702
+ "updatedAt",
1703
+ "email",
1704
+ "firstName",
1705
+ "lastName",
1706
+ "status"
1707
+ ];
1708
+ const sortBy = allowedSortFields.includes(String(req.query.sortBy || "")) ? String(req.query.sortBy) : "createdAt";
1709
+ const sortOrder = String(req.query.sortOrder || "desc").toLowerCase() === "asc" ? 1 : -1;
1710
+ const sortObj = { [sortBy]: sortOrder };
1711
+ const allowedDateFields = ["createdAt", "updatedAt"];
1712
+ const dateField = allowedDateFields.includes(
1713
+ String(req.query.dateField || "")
1714
+ ) ? String(req.query.dateField) : "createdAt";
1715
+ const baseQuery = { projectId };
1716
+ if (req.query.emailVerified !== void 0) {
1717
+ const emailVerified = String(req.query.emailVerified).toLowerCase();
1718
+ if (emailVerified === "true" || emailVerified === "false") {
1719
+ baseQuery.emailVerified = emailVerified === "true";
1720
+ }
1721
+ }
1722
+ if (req.query.from || req.query.to) {
1723
+ const dateFilter = {};
1724
+ if (req.query.from) {
1725
+ const fromDate = new Date(String(req.query.from));
1726
+ if (!isNaN(fromDate.getTime())) {
1727
+ dateFilter.$gte = fromDate;
1728
+ }
1729
+ }
1730
+ if (req.query.to) {
1731
+ const toDate = new Date(String(req.query.to));
1732
+ if (!isNaN(toDate.getTime())) {
1733
+ dateFilter.$lte = toDate;
1734
+ }
1735
+ }
1736
+ if (Object.keys(dateFilter).length > 0) {
1737
+ baseQuery[dateField] = dateFilter;
1738
+ }
1739
+ }
1740
+ if (req.query.email) {
1741
+ const emailSearch = escapeRegex(String(req.query.email));
1742
+ baseQuery.email = { $regex: emailSearch, $options: "i" };
1743
+ }
1744
+ if (req.query.userId) {
1745
+ const userId = String(req.query.userId).trim();
1746
+ if (userId) {
1747
+ baseQuery.id = userId;
1748
+ }
1749
+ }
1750
+ if (req.query.status !== void 0) {
1751
+ const statusValue = String(req.query.status).toLowerCase().trim();
1752
+ if (statusValue === "true" || statusValue === "pending") {
1753
+ baseQuery.metadata = {
1754
+ $elemMatch: {
1755
+ key: "inWaitlist",
1756
+ value: true
1757
+ }
1758
+ };
1759
+ } else if (statusValue === "false" || statusValue === "active") {
1760
+ baseQuery.metadata = {
1761
+ $elemMatch: {
1762
+ key: "inWaitlist",
1763
+ value: false
1764
+ }
1765
+ };
1766
+ }
1767
+ }
1768
+ const projection = {
1769
+ id: 1,
1770
+ email: 1,
1771
+ firstName: 1,
1772
+ lastName: 1,
1773
+ orgId: 1,
1774
+ projectId: 1,
1775
+ roles: 1,
1776
+ emailVerified: 1,
1777
+ status: 1,
1778
+ lastEmailSent: 1,
1779
+ lastPasswordReset: 1,
1780
+ metadata: 1,
1781
+ createdAt: 1,
1782
+ updatedAt: 1
1783
+ };
1784
+ if (req.query.name) {
1785
+ const nameSearch = escapeRegex(String(req.query.name));
1786
+ const nameRegex = { $regex: nameSearch, $options: "i" };
1787
+ const pipeline = [
1788
+ { $match: baseQuery },
1789
+ {
1790
+ $addFields: {
1791
+ fullName: {
1792
+ $concat: [
1793
+ { $ifNull: ["$firstName", ""] },
1794
+ " ",
1795
+ { $ifNull: ["$lastName", ""] }
1796
+ ]
1797
+ }
1798
+ }
1799
+ },
1800
+ {
1801
+ $match: {
1802
+ $or: [
1803
+ { firstName: nameRegex },
1804
+ { lastName: nameRegex },
1805
+ { fullName: nameRegex }
1806
+ ]
1807
+ }
1808
+ },
1809
+ {
1810
+ $facet: {
1811
+ data: [
1812
+ { $sort: sortObj },
1813
+ { $skip: skip },
1814
+ { $limit: limit },
1815
+ { $project: projection }
1816
+ ],
1817
+ total: [{ $count: "count" }]
1818
+ }
1819
+ }
1820
+ ];
1821
+ const result = await OrgUser.aggregate(pipeline);
1822
+ const data2 = result[0]?.data || [];
1823
+ const total2 = result[0]?.total[0]?.count || 0;
1824
+ const totalPages2 = Math.ceil(total2 / limit);
1825
+ return res.json({
1826
+ ok: true,
1827
+ page,
1828
+ limit,
1829
+ total: total2,
1830
+ totalPages: totalPages2,
1831
+ data: data2
1832
+ });
1833
+ }
1834
+ const [data, total] = await Promise.all([
1835
+ OrgUser.find(baseQuery).select(projection).sort(sortObj).skip(skip).limit(limit).lean(),
1836
+ OrgUser.countDocuments(baseQuery)
1837
+ ]);
1838
+ const totalPages = Math.ceil(total / limit);
1839
+ return res.json({
1840
+ ok: true,
1841
+ page,
1842
+ limit,
1843
+ total,
1844
+ totalPages,
1845
+ data
1846
+ });
1847
+ } catch (err) {
1848
+ console.error("Get users by pagination error:", err);
1849
+ return res.status(500).json({
1850
+ ok: false,
1851
+ error: "Internal server error"
1852
+ });
1853
+ }
1854
+ });
1855
+ r.get("/users/:userId", async (req, res) => {
1856
+ try {
1857
+ const userId = String(req.params.userId || "").trim();
1858
+ if (!userId) {
1859
+ return res.status(400).json({
1860
+ ok: false,
1861
+ error: "userId is required"
1862
+ });
1863
+ }
1864
+ const projectId = String(req.query.projectId || "").trim() || process.env.PROJECTID || "";
1865
+ if (!projectId) {
1866
+ return res.status(400).json({
1867
+ ok: false,
1868
+ error: "projectId is required"
1869
+ });
1870
+ }
1871
+ const projection = {
1872
+ id: 1,
1873
+ email: 1,
1874
+ firstName: 1,
1875
+ lastName: 1,
1876
+ orgId: 1,
1877
+ projectId: 1,
1878
+ roles: 1,
1879
+ emailVerified: 1,
1880
+ lastEmailSent: 1,
1881
+ lastPasswordReset: 1,
1882
+ metadata: 1,
1883
+ createdAt: 1,
1884
+ updatedAt: 1
1885
+ };
1886
+ let query = { projectId };
1887
+ if (import_mongoose5.default.Types.ObjectId.isValid(userId)) {
1888
+ query = {
1889
+ projectId,
1890
+ $or: [{ _id: new import_mongoose5.default.Types.ObjectId(userId) }, { id: userId }]
1891
+ };
1892
+ } else {
1893
+ query = { projectId, id: userId };
1894
+ }
1895
+ const user = await OrgUser.findOne(query).select(projection).lean();
1896
+ if (!user) {
1897
+ return res.status(404).json({
1898
+ ok: false,
1899
+ error: "User not found"
1900
+ });
1901
+ }
1902
+ return res.json({
1903
+ ok: true,
1904
+ data: user
1905
+ });
1906
+ } catch (err) {
1907
+ console.error("Get user by id error:", err);
1908
+ return res.status(500).json({
1909
+ ok: false,
1910
+ error: "Internal server error"
1911
+ });
1912
+ }
1913
+ });
1669
1914
  return r;
1670
1915
  }
1671
1916
  function setAuthCookies(res, tokens, cookie) {
@@ -1725,6 +1970,9 @@ async function sendRateLimitedEmail({
1725
1970
  await user.save();
1726
1971
  return { rateLimited: false };
1727
1972
  }
1973
+ function escapeRegex(str) {
1974
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1975
+ }
1728
1976
  function generateTokens(user) {
1729
1977
  const accessPayload = {
1730
1978
  sub: user.id.toString(),
@@ -1744,7 +1992,7 @@ function generateTokens(user) {
1744
1992
  expiresIn: "1d"
1745
1993
  });
1746
1994
  const refreshToken = import_jsonwebtoken4.default.sign(
1747
- { sub: user._id.toString() },
1995
+ { sub: user.id.toString() },
1748
1996
  process.env.JWT_SECRET,
1749
1997
  { expiresIn: "30d" }
1750
1998
  );
@@ -1842,12 +2090,12 @@ var import_express4 = require("express");
1842
2090
  var import_crypto2 = require("crypto");
1843
2091
 
1844
2092
  // src/models/moduleConnection.model.ts
1845
- var import_mongoose5 = __toESM(require("mongoose"), 1);
1846
- var ModuleItemSchema = new import_mongoose5.default.Schema(
2093
+ var import_mongoose6 = __toESM(require("mongoose"), 1);
2094
+ var ModuleItemSchema = new import_mongoose6.default.Schema(
1847
2095
  { id: { type: String, required: true } },
1848
2096
  { _id: false }
1849
2097
  );
1850
- var ModuleConnectionSchema = new import_mongoose5.default.Schema(
2098
+ var ModuleConnectionSchema = new import_mongoose6.default.Schema(
1851
2099
  {
1852
2100
  projectId: { type: String, required: true, index: true },
1853
2101
  modules: {
@@ -1858,14 +2106,14 @@ var ModuleConnectionSchema = new import_mongoose5.default.Schema(
1858
2106
  },
1859
2107
  { timestamps: true, collection: "module_connection" }
1860
2108
  );
1861
- var ModuleConnection = import_mongoose5.default.model(
2109
+ var ModuleConnection = import_mongoose6.default.model(
1862
2110
  "ModuleConnection",
1863
2111
  ModuleConnectionSchema
1864
2112
  );
1865
2113
 
1866
2114
  // src/models/project.model.ts
1867
- var import_mongoose6 = __toESM(require("mongoose"), 1);
1868
- var ProjectSchema = new import_mongoose6.default.Schema(
2115
+ var import_mongoose7 = __toESM(require("mongoose"), 1);
2116
+ var ProjectSchema = new import_mongoose7.default.Schema(
1869
2117
  {
1870
2118
  _id: { type: String, required: true },
1871
2119
  org_id: { type: String, required: true, index: true },
@@ -1875,7 +2123,7 @@ var ProjectSchema = new import_mongoose6.default.Schema(
1875
2123
  },
1876
2124
  { timestamps: true, collection: "projects" }
1877
2125
  );
1878
- var Project = import_mongoose6.default.model("Project", ProjectSchema);
2126
+ var Project = import_mongoose7.default.model("Project", ProjectSchema);
1879
2127
 
1880
2128
  // src/services/projects.service.ts
1881
2129
  var ProjectsService = class {
@@ -1962,8 +2210,8 @@ function requireRole(...roles) {
1962
2210
  }
1963
2211
 
1964
2212
  // src/models/permissions.model.ts
1965
- var import_mongoose7 = __toESM(require("mongoose"), 1);
1966
- var PermissionsSchema = new import_mongoose7.Schema(
2213
+ var import_mongoose8 = __toESM(require("mongoose"), 1);
2214
+ var PermissionsSchema = new import_mongoose8.Schema(
1967
2215
  {
1968
2216
  id: { type: String, required: true, index: true },
1969
2217
  orgId: { type: String, default: null, index: true },
@@ -1978,7 +2226,7 @@ var PermissionsSchema = new import_mongoose7.Schema(
1978
2226
  }
1979
2227
  );
1980
2228
  PermissionsSchema.index({ orgId: 1, key: 1 }, { unique: true });
1981
- var PermissionsModel = import_mongoose7.default.model(
2229
+ var PermissionsModel = import_mongoose8.default.model(
1982
2230
  "Permissions",
1983
2231
  PermissionsSchema,
1984
2232
  "permissions"