aaspai-authx 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ import express, {
30
30
  Router
31
31
  } from "express";
32
32
  import jwt4 from "jsonwebtoken";
33
+ import mongoose5 from "mongoose";
33
34
 
34
35
  // src/config/loadConfig.ts
35
36
  function loadConfig() {
@@ -256,7 +257,7 @@ var MetadataSchema = new mongoose2.Schema(
256
257
  );
257
258
  var OrgUserSchema = new mongoose2.Schema(
258
259
  {
259
- id: { type: String, default: uuid(), index: true, unique: true },
260
+ id: { type: String, default: uuid, index: true, unique: true },
260
261
  email: { type: String, required: true, unique: true },
261
262
  firstName: { type: String, required: true },
262
263
  lastName: { type: String, required: true },
@@ -271,6 +272,11 @@ var OrgUserSchema = new mongoose2.Schema(
271
272
  },
272
273
  { timestamps: true, collection: "users" }
273
274
  );
275
+ OrgUserSchema.index({ projectId: 1, createdAt: -1 });
276
+ OrgUserSchema.index({ projectId: 1, updatedAt: -1 });
277
+ OrgUserSchema.index({ projectId: 1, email: 1 });
278
+ OrgUserSchema.index({ projectId: 1, emailVerified: 1, createdAt: -1 });
279
+ OrgUserSchema.index({ projectId: 1, firstName: 1, lastName: 1 });
274
280
  var OrgUser = mongoose2.model("OrgUser", OrgUserSchema);
275
281
 
276
282
  // src/utils/extract.ts
@@ -1226,7 +1232,7 @@ function createAuthRouter(options = {}) {
1226
1232
  value
1227
1233
  }));
1228
1234
  await user.save();
1229
- res.json({ ok: true, metadata: user.metadata });
1235
+ res.json({ ok: true, user });
1230
1236
  });
1231
1237
  r.get("/verify-email", async (req, res) => {
1232
1238
  const token = String(req.query.token || "");
@@ -1694,6 +1700,212 @@ function createAuthRouter(options = {}) {
1694
1700
  const user = await OrgUser.find({ projectId: req.query.projectId }).lean();
1695
1701
  res.json(user || null);
1696
1702
  });
1703
+ r.get("/get-users-by-pagination", async (req, res) => {
1704
+ try {
1705
+ const projectId = String(req.query.projectId || "");
1706
+ if (!projectId) {
1707
+ return res.status(400).json({
1708
+ ok: false,
1709
+ error: "projectId is required"
1710
+ });
1711
+ }
1712
+ const page = Math.max(
1713
+ 1,
1714
+ parseInt(String(req.query.page || "1"), 10) || 1
1715
+ );
1716
+ const limit = Math.min(
1717
+ 200,
1718
+ Math.max(1, parseInt(String(req.query.limit || "20"), 10) || 20)
1719
+ );
1720
+ const skip = (page - 1) * limit;
1721
+ const allowedSortFields = [
1722
+ "createdAt",
1723
+ "updatedAt",
1724
+ "email",
1725
+ "firstName",
1726
+ "lastName"
1727
+ ];
1728
+ const sortBy = allowedSortFields.includes(String(req.query.sortBy || "")) ? String(req.query.sortBy) : "createdAt";
1729
+ const sortOrder = String(req.query.sortOrder || "desc").toLowerCase() === "asc" ? 1 : -1;
1730
+ const sortObj = { [sortBy]: sortOrder };
1731
+ const allowedDateFields = ["createdAt", "updatedAt"];
1732
+ const dateField = allowedDateFields.includes(
1733
+ String(req.query.dateField || "")
1734
+ ) ? String(req.query.dateField) : "createdAt";
1735
+ const baseQuery = { projectId };
1736
+ if (req.query.emailVerified !== void 0) {
1737
+ const emailVerified = String(req.query.emailVerified).toLowerCase();
1738
+ if (emailVerified === "true" || emailVerified === "false") {
1739
+ baseQuery.emailVerified = emailVerified === "true";
1740
+ }
1741
+ }
1742
+ if (req.query.from || req.query.to) {
1743
+ const dateFilter = {};
1744
+ if (req.query.from) {
1745
+ const fromDate = new Date(String(req.query.from));
1746
+ if (!isNaN(fromDate.getTime())) {
1747
+ dateFilter.$gte = fromDate;
1748
+ }
1749
+ }
1750
+ if (req.query.to) {
1751
+ const toDate = new Date(String(req.query.to));
1752
+ if (!isNaN(toDate.getTime())) {
1753
+ dateFilter.$lte = toDate;
1754
+ }
1755
+ }
1756
+ if (Object.keys(dateFilter).length > 0) {
1757
+ baseQuery[dateField] = dateFilter;
1758
+ }
1759
+ }
1760
+ if (req.query.email) {
1761
+ const emailSearch = escapeRegex(String(req.query.email));
1762
+ baseQuery.email = { $regex: emailSearch, $options: "i" };
1763
+ }
1764
+ const projection = {
1765
+ id: 1,
1766
+ email: 1,
1767
+ firstName: 1,
1768
+ lastName: 1,
1769
+ orgId: 1,
1770
+ projectId: 1,
1771
+ roles: 1,
1772
+ emailVerified: 1,
1773
+ lastEmailSent: 1,
1774
+ lastPasswordReset: 1,
1775
+ metadata: 1,
1776
+ createdAt: 1,
1777
+ updatedAt: 1
1778
+ };
1779
+ if (req.query.name) {
1780
+ const nameSearch = escapeRegex(String(req.query.name));
1781
+ const nameRegex = { $regex: nameSearch, $options: "i" };
1782
+ const pipeline = [
1783
+ { $match: baseQuery },
1784
+ {
1785
+ $addFields: {
1786
+ fullName: {
1787
+ $concat: [
1788
+ { $ifNull: ["$firstName", ""] },
1789
+ " ",
1790
+ { $ifNull: ["$lastName", ""] }
1791
+ ]
1792
+ }
1793
+ }
1794
+ },
1795
+ {
1796
+ $match: {
1797
+ $or: [
1798
+ { firstName: nameRegex },
1799
+ { lastName: nameRegex },
1800
+ { fullName: nameRegex }
1801
+ ]
1802
+ }
1803
+ },
1804
+ {
1805
+ $facet: {
1806
+ data: [
1807
+ { $sort: sortObj },
1808
+ { $skip: skip },
1809
+ { $limit: limit },
1810
+ { $project: projection }
1811
+ ],
1812
+ total: [{ $count: "count" }]
1813
+ }
1814
+ }
1815
+ ];
1816
+ const result = await OrgUser.aggregate(pipeline);
1817
+ const data2 = result[0]?.data || [];
1818
+ const total2 = result[0]?.total[0]?.count || 0;
1819
+ const totalPages2 = Math.ceil(total2 / limit);
1820
+ return res.json({
1821
+ ok: true,
1822
+ page,
1823
+ limit,
1824
+ total: total2,
1825
+ totalPages: totalPages2,
1826
+ data: data2
1827
+ });
1828
+ }
1829
+ const [data, total] = await Promise.all([
1830
+ OrgUser.find(baseQuery).select(projection).sort(sortObj).skip(skip).limit(limit).lean(),
1831
+ OrgUser.countDocuments(baseQuery)
1832
+ ]);
1833
+ const totalPages = Math.ceil(total / limit);
1834
+ return res.json({
1835
+ ok: true,
1836
+ page,
1837
+ limit,
1838
+ total,
1839
+ totalPages,
1840
+ data
1841
+ });
1842
+ } catch (err) {
1843
+ console.error("Get users by pagination error:", err);
1844
+ return res.status(500).json({
1845
+ ok: false,
1846
+ error: "Internal server error"
1847
+ });
1848
+ }
1849
+ });
1850
+ r.get("/users/:userId", async (req, res) => {
1851
+ try {
1852
+ const userId = String(req.params.userId || "").trim();
1853
+ if (!userId) {
1854
+ return res.status(400).json({
1855
+ ok: false,
1856
+ error: "userId is required"
1857
+ });
1858
+ }
1859
+ const projectId = String(req.query.projectId || "").trim() || process.env.PROJECTID || "";
1860
+ if (!projectId) {
1861
+ return res.status(400).json({
1862
+ ok: false,
1863
+ error: "projectId is required"
1864
+ });
1865
+ }
1866
+ const projection = {
1867
+ id: 1,
1868
+ email: 1,
1869
+ firstName: 1,
1870
+ lastName: 1,
1871
+ orgId: 1,
1872
+ projectId: 1,
1873
+ roles: 1,
1874
+ emailVerified: 1,
1875
+ lastEmailSent: 1,
1876
+ lastPasswordReset: 1,
1877
+ metadata: 1,
1878
+ createdAt: 1,
1879
+ updatedAt: 1
1880
+ };
1881
+ let query = { projectId };
1882
+ if (mongoose5.Types.ObjectId.isValid(userId)) {
1883
+ query = {
1884
+ projectId,
1885
+ $or: [{ _id: new mongoose5.Types.ObjectId(userId) }, { id: userId }]
1886
+ };
1887
+ } else {
1888
+ query = { projectId, id: userId };
1889
+ }
1890
+ const user = await OrgUser.findOne(query).select(projection).lean();
1891
+ if (!user) {
1892
+ return res.status(404).json({
1893
+ ok: false,
1894
+ error: "User not found"
1895
+ });
1896
+ }
1897
+ return res.json({
1898
+ ok: true,
1899
+ data: user
1900
+ });
1901
+ } catch (err) {
1902
+ console.error("Get user by id error:", err);
1903
+ return res.status(500).json({
1904
+ ok: false,
1905
+ error: "Internal server error"
1906
+ });
1907
+ }
1908
+ });
1697
1909
  return r;
1698
1910
  }
1699
1911
  function setAuthCookies(res, tokens, cookie) {
@@ -1753,6 +1965,9 @@ async function sendRateLimitedEmail({
1753
1965
  await user.save();
1754
1966
  return { rateLimited: false };
1755
1967
  }
1968
+ function escapeRegex(str) {
1969
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1970
+ }
1756
1971
  function generateTokens(user) {
1757
1972
  const accessPayload = {
1758
1973
  sub: user.id.toString(),
@@ -1870,12 +2085,12 @@ import { Router as Router4 } from "express";
1870
2085
  import { randomUUID as randomUUID2 } from "crypto";
1871
2086
 
1872
2087
  // src/models/moduleConnection.model.ts
1873
- import mongoose5 from "mongoose";
1874
- var ModuleItemSchema = new mongoose5.Schema(
2088
+ import mongoose6 from "mongoose";
2089
+ var ModuleItemSchema = new mongoose6.Schema(
1875
2090
  { id: { type: String, required: true } },
1876
2091
  { _id: false }
1877
2092
  );
1878
- var ModuleConnectionSchema = new mongoose5.Schema(
2093
+ var ModuleConnectionSchema = new mongoose6.Schema(
1879
2094
  {
1880
2095
  projectId: { type: String, required: true, index: true },
1881
2096
  modules: {
@@ -1886,14 +2101,14 @@ var ModuleConnectionSchema = new mongoose5.Schema(
1886
2101
  },
1887
2102
  { timestamps: true, collection: "module_connection" }
1888
2103
  );
1889
- var ModuleConnection = mongoose5.model(
2104
+ var ModuleConnection = mongoose6.model(
1890
2105
  "ModuleConnection",
1891
2106
  ModuleConnectionSchema
1892
2107
  );
1893
2108
 
1894
2109
  // src/models/project.model.ts
1895
- import mongoose6 from "mongoose";
1896
- var ProjectSchema = new mongoose6.Schema(
2110
+ import mongoose7 from "mongoose";
2111
+ var ProjectSchema = new mongoose7.Schema(
1897
2112
  {
1898
2113
  _id: { type: String, required: true },
1899
2114
  org_id: { type: String, required: true, index: true },
@@ -1903,7 +2118,7 @@ var ProjectSchema = new mongoose6.Schema(
1903
2118
  },
1904
2119
  { timestamps: true, collection: "projects" }
1905
2120
  );
1906
- var Project = mongoose6.model("Project", ProjectSchema);
2121
+ var Project = mongoose7.model("Project", ProjectSchema);
1907
2122
 
1908
2123
  // src/services/projects.service.ts
1909
2124
  var ProjectsService = class {
@@ -1990,7 +2205,7 @@ function requireRole(...roles) {
1990
2205
  }
1991
2206
 
1992
2207
  // src/models/permissions.model.ts
1993
- import mongoose7, { Schema as Schema3 } from "mongoose";
2208
+ import mongoose8, { Schema as Schema3 } from "mongoose";
1994
2209
  var PermissionsSchema = new Schema3(
1995
2210
  {
1996
2211
  id: { type: String, required: true, index: true },
@@ -2006,7 +2221,7 @@ var PermissionsSchema = new Schema3(
2006
2221
  }
2007
2222
  );
2008
2223
  PermissionsSchema.index({ orgId: 1, key: 1 }, { unique: true });
2009
- var PermissionsModel = mongoose7.model(
2224
+ var PermissionsModel = mongoose8.model(
2010
2225
  "Permissions",
2011
2226
  PermissionsSchema,
2012
2227
  "permissions"