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.cjs CHANGED
@@ -82,6 +82,7 @@ var import_bcryptjs = __toESM(require("bcryptjs"), 1);
82
82
  var import_crypto = require("crypto");
83
83
  var import_express = __toESM(require("express"), 1);
84
84
  var import_jsonwebtoken4 = __toESM(require("jsonwebtoken"), 1);
85
+ var import_mongoose5 = __toESM(require("mongoose"), 1);
85
86
 
86
87
  // src/config/loadConfig.ts
87
88
  function loadConfig() {
@@ -308,7 +309,7 @@ var MetadataSchema = new import_mongoose2.default.Schema(
308
309
  );
309
310
  var OrgUserSchema = new import_mongoose2.default.Schema(
310
311
  {
311
- id: { type: String, default: (0, import_uuid.v4)(), index: true, unique: true },
312
+ id: { type: String, default: import_uuid.v4, index: true, unique: true },
312
313
  email: { type: String, required: true, unique: true },
313
314
  firstName: { type: String, required: true },
314
315
  lastName: { type: String, required: true },
@@ -323,6 +324,11 @@ var OrgUserSchema = new import_mongoose2.default.Schema(
323
324
  },
324
325
  { timestamps: true, collection: "users" }
325
326
  );
327
+ OrgUserSchema.index({ projectId: 1, createdAt: -1 });
328
+ OrgUserSchema.index({ projectId: 1, updatedAt: -1 });
329
+ OrgUserSchema.index({ projectId: 1, email: 1 });
330
+ OrgUserSchema.index({ projectId: 1, emailVerified: 1, createdAt: -1 });
331
+ OrgUserSchema.index({ projectId: 1, firstName: 1, lastName: 1 });
326
332
  var OrgUser = import_mongoose2.default.model("OrgUser", OrgUserSchema);
327
333
 
328
334
  // src/utils/extract.ts
@@ -1278,7 +1284,7 @@ function createAuthRouter(options = {}) {
1278
1284
  value
1279
1285
  }));
1280
1286
  await user.save();
1281
- res.json({ ok: true, metadata: user.metadata });
1287
+ res.json({ ok: true, user });
1282
1288
  });
1283
1289
  r.get("/verify-email", async (req, res) => {
1284
1290
  const token = String(req.query.token || "");
@@ -1746,6 +1752,212 @@ function createAuthRouter(options = {}) {
1746
1752
  const user = await OrgUser.find({ projectId: req.query.projectId }).lean();
1747
1753
  res.json(user || null);
1748
1754
  });
1755
+ r.get("/get-users-by-pagination", async (req, res) => {
1756
+ try {
1757
+ const projectId = String(req.query.projectId || "");
1758
+ if (!projectId) {
1759
+ return res.status(400).json({
1760
+ ok: false,
1761
+ error: "projectId is required"
1762
+ });
1763
+ }
1764
+ const page = Math.max(
1765
+ 1,
1766
+ parseInt(String(req.query.page || "1"), 10) || 1
1767
+ );
1768
+ const limit = Math.min(
1769
+ 200,
1770
+ Math.max(1, parseInt(String(req.query.limit || "20"), 10) || 20)
1771
+ );
1772
+ const skip = (page - 1) * limit;
1773
+ const allowedSortFields = [
1774
+ "createdAt",
1775
+ "updatedAt",
1776
+ "email",
1777
+ "firstName",
1778
+ "lastName"
1779
+ ];
1780
+ const sortBy = allowedSortFields.includes(String(req.query.sortBy || "")) ? String(req.query.sortBy) : "createdAt";
1781
+ const sortOrder = String(req.query.sortOrder || "desc").toLowerCase() === "asc" ? 1 : -1;
1782
+ const sortObj = { [sortBy]: sortOrder };
1783
+ const allowedDateFields = ["createdAt", "updatedAt"];
1784
+ const dateField = allowedDateFields.includes(
1785
+ String(req.query.dateField || "")
1786
+ ) ? String(req.query.dateField) : "createdAt";
1787
+ const baseQuery = { projectId };
1788
+ if (req.query.emailVerified !== void 0) {
1789
+ const emailVerified = String(req.query.emailVerified).toLowerCase();
1790
+ if (emailVerified === "true" || emailVerified === "false") {
1791
+ baseQuery.emailVerified = emailVerified === "true";
1792
+ }
1793
+ }
1794
+ if (req.query.from || req.query.to) {
1795
+ const dateFilter = {};
1796
+ if (req.query.from) {
1797
+ const fromDate = new Date(String(req.query.from));
1798
+ if (!isNaN(fromDate.getTime())) {
1799
+ dateFilter.$gte = fromDate;
1800
+ }
1801
+ }
1802
+ if (req.query.to) {
1803
+ const toDate = new Date(String(req.query.to));
1804
+ if (!isNaN(toDate.getTime())) {
1805
+ dateFilter.$lte = toDate;
1806
+ }
1807
+ }
1808
+ if (Object.keys(dateFilter).length > 0) {
1809
+ baseQuery[dateField] = dateFilter;
1810
+ }
1811
+ }
1812
+ if (req.query.email) {
1813
+ const emailSearch = escapeRegex(String(req.query.email));
1814
+ baseQuery.email = { $regex: emailSearch, $options: "i" };
1815
+ }
1816
+ const projection = {
1817
+ id: 1,
1818
+ email: 1,
1819
+ firstName: 1,
1820
+ lastName: 1,
1821
+ orgId: 1,
1822
+ projectId: 1,
1823
+ roles: 1,
1824
+ emailVerified: 1,
1825
+ lastEmailSent: 1,
1826
+ lastPasswordReset: 1,
1827
+ metadata: 1,
1828
+ createdAt: 1,
1829
+ updatedAt: 1
1830
+ };
1831
+ if (req.query.name) {
1832
+ const nameSearch = escapeRegex(String(req.query.name));
1833
+ const nameRegex = { $regex: nameSearch, $options: "i" };
1834
+ const pipeline = [
1835
+ { $match: baseQuery },
1836
+ {
1837
+ $addFields: {
1838
+ fullName: {
1839
+ $concat: [
1840
+ { $ifNull: ["$firstName", ""] },
1841
+ " ",
1842
+ { $ifNull: ["$lastName", ""] }
1843
+ ]
1844
+ }
1845
+ }
1846
+ },
1847
+ {
1848
+ $match: {
1849
+ $or: [
1850
+ { firstName: nameRegex },
1851
+ { lastName: nameRegex },
1852
+ { fullName: nameRegex }
1853
+ ]
1854
+ }
1855
+ },
1856
+ {
1857
+ $facet: {
1858
+ data: [
1859
+ { $sort: sortObj },
1860
+ { $skip: skip },
1861
+ { $limit: limit },
1862
+ { $project: projection }
1863
+ ],
1864
+ total: [{ $count: "count" }]
1865
+ }
1866
+ }
1867
+ ];
1868
+ const result = await OrgUser.aggregate(pipeline);
1869
+ const data2 = result[0]?.data || [];
1870
+ const total2 = result[0]?.total[0]?.count || 0;
1871
+ const totalPages2 = Math.ceil(total2 / limit);
1872
+ return res.json({
1873
+ ok: true,
1874
+ page,
1875
+ limit,
1876
+ total: total2,
1877
+ totalPages: totalPages2,
1878
+ data: data2
1879
+ });
1880
+ }
1881
+ const [data, total] = await Promise.all([
1882
+ OrgUser.find(baseQuery).select(projection).sort(sortObj).skip(skip).limit(limit).lean(),
1883
+ OrgUser.countDocuments(baseQuery)
1884
+ ]);
1885
+ const totalPages = Math.ceil(total / limit);
1886
+ return res.json({
1887
+ ok: true,
1888
+ page,
1889
+ limit,
1890
+ total,
1891
+ totalPages,
1892
+ data
1893
+ });
1894
+ } catch (err) {
1895
+ console.error("Get users by pagination error:", err);
1896
+ return res.status(500).json({
1897
+ ok: false,
1898
+ error: "Internal server error"
1899
+ });
1900
+ }
1901
+ });
1902
+ r.get("/users/:userId", async (req, res) => {
1903
+ try {
1904
+ const userId = String(req.params.userId || "").trim();
1905
+ if (!userId) {
1906
+ return res.status(400).json({
1907
+ ok: false,
1908
+ error: "userId is required"
1909
+ });
1910
+ }
1911
+ const projectId = String(req.query.projectId || "").trim() || process.env.PROJECTID || "";
1912
+ if (!projectId) {
1913
+ return res.status(400).json({
1914
+ ok: false,
1915
+ error: "projectId is required"
1916
+ });
1917
+ }
1918
+ const projection = {
1919
+ id: 1,
1920
+ email: 1,
1921
+ firstName: 1,
1922
+ lastName: 1,
1923
+ orgId: 1,
1924
+ projectId: 1,
1925
+ roles: 1,
1926
+ emailVerified: 1,
1927
+ lastEmailSent: 1,
1928
+ lastPasswordReset: 1,
1929
+ metadata: 1,
1930
+ createdAt: 1,
1931
+ updatedAt: 1
1932
+ };
1933
+ let query = { projectId };
1934
+ if (import_mongoose5.default.Types.ObjectId.isValid(userId)) {
1935
+ query = {
1936
+ projectId,
1937
+ $or: [{ _id: new import_mongoose5.default.Types.ObjectId(userId) }, { id: userId }]
1938
+ };
1939
+ } else {
1940
+ query = { projectId, id: userId };
1941
+ }
1942
+ const user = await OrgUser.findOne(query).select(projection).lean();
1943
+ if (!user) {
1944
+ return res.status(404).json({
1945
+ ok: false,
1946
+ error: "User not found"
1947
+ });
1948
+ }
1949
+ return res.json({
1950
+ ok: true,
1951
+ data: user
1952
+ });
1953
+ } catch (err) {
1954
+ console.error("Get user by id error:", err);
1955
+ return res.status(500).json({
1956
+ ok: false,
1957
+ error: "Internal server error"
1958
+ });
1959
+ }
1960
+ });
1749
1961
  return r;
1750
1962
  }
1751
1963
  function setAuthCookies(res, tokens, cookie) {
@@ -1805,6 +2017,9 @@ async function sendRateLimitedEmail({
1805
2017
  await user.save();
1806
2018
  return { rateLimited: false };
1807
2019
  }
2020
+ function escapeRegex(str) {
2021
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2022
+ }
1808
2023
  function generateTokens(user) {
1809
2024
  const accessPayload = {
1810
2025
  sub: user.id.toString(),
@@ -1922,12 +2137,12 @@ var import_express4 = require("express");
1922
2137
  var import_crypto2 = require("crypto");
1923
2138
 
1924
2139
  // src/models/moduleConnection.model.ts
1925
- var import_mongoose5 = __toESM(require("mongoose"), 1);
1926
- var ModuleItemSchema = new import_mongoose5.default.Schema(
2140
+ var import_mongoose6 = __toESM(require("mongoose"), 1);
2141
+ var ModuleItemSchema = new import_mongoose6.default.Schema(
1927
2142
  { id: { type: String, required: true } },
1928
2143
  { _id: false }
1929
2144
  );
1930
- var ModuleConnectionSchema = new import_mongoose5.default.Schema(
2145
+ var ModuleConnectionSchema = new import_mongoose6.default.Schema(
1931
2146
  {
1932
2147
  projectId: { type: String, required: true, index: true },
1933
2148
  modules: {
@@ -1938,14 +2153,14 @@ var ModuleConnectionSchema = new import_mongoose5.default.Schema(
1938
2153
  },
1939
2154
  { timestamps: true, collection: "module_connection" }
1940
2155
  );
1941
- var ModuleConnection = import_mongoose5.default.model(
2156
+ var ModuleConnection = import_mongoose6.default.model(
1942
2157
  "ModuleConnection",
1943
2158
  ModuleConnectionSchema
1944
2159
  );
1945
2160
 
1946
2161
  // src/models/project.model.ts
1947
- var import_mongoose6 = __toESM(require("mongoose"), 1);
1948
- var ProjectSchema = new import_mongoose6.default.Schema(
2162
+ var import_mongoose7 = __toESM(require("mongoose"), 1);
2163
+ var ProjectSchema = new import_mongoose7.default.Schema(
1949
2164
  {
1950
2165
  _id: { type: String, required: true },
1951
2166
  org_id: { type: String, required: true, index: true },
@@ -1955,7 +2170,7 @@ var ProjectSchema = new import_mongoose6.default.Schema(
1955
2170
  },
1956
2171
  { timestamps: true, collection: "projects" }
1957
2172
  );
1958
- var Project = import_mongoose6.default.model("Project", ProjectSchema);
2173
+ var Project = import_mongoose7.default.model("Project", ProjectSchema);
1959
2174
 
1960
2175
  // src/services/projects.service.ts
1961
2176
  var ProjectsService = class {
@@ -2042,8 +2257,8 @@ function requireRole(...roles) {
2042
2257
  }
2043
2258
 
2044
2259
  // src/models/permissions.model.ts
2045
- var import_mongoose7 = __toESM(require("mongoose"), 1);
2046
- var PermissionsSchema = new import_mongoose7.Schema(
2260
+ var import_mongoose8 = __toESM(require("mongoose"), 1);
2261
+ var PermissionsSchema = new import_mongoose8.Schema(
2047
2262
  {
2048
2263
  id: { type: String, required: true, index: true },
2049
2264
  orgId: { type: String, default: null, index: true },
@@ -2058,7 +2273,7 @@ var PermissionsSchema = new import_mongoose7.Schema(
2058
2273
  }
2059
2274
  );
2060
2275
  PermissionsSchema.index({ orgId: 1, key: 1 }, { unique: true });
2061
- var PermissionsModel = import_mongoose7.default.model(
2276
+ var PermissionsModel = import_mongoose8.default.model(
2062
2277
  "Permissions",
2063
2278
  PermissionsSchema,
2064
2279
  "permissions"