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.
@@ -5,6 +5,7 @@ import express, {
5
5
  Router
6
6
  } from "express";
7
7
  import jwt4 from "jsonwebtoken";
8
+ import mongoose5 from "mongoose";
8
9
 
9
10
  // src/config/loadConfig.ts
10
11
  function loadConfig() {
@@ -205,7 +206,7 @@ var MetadataSchema = new mongoose2.Schema(
205
206
  );
206
207
  var OrgUserSchema = new mongoose2.Schema(
207
208
  {
208
- id: { type: String, default: uuid(), index: true, unique: true },
209
+ id: { type: String, default: uuid, index: true, unique: true },
209
210
  email: { type: String, required: true, unique: true },
210
211
  firstName: { type: String, required: true },
211
212
  lastName: { type: String, required: true },
@@ -220,6 +221,11 @@ var OrgUserSchema = new mongoose2.Schema(
220
221
  },
221
222
  { timestamps: true, collection: "users" }
222
223
  );
224
+ OrgUserSchema.index({ projectId: 1, createdAt: -1 });
225
+ OrgUserSchema.index({ projectId: 1, updatedAt: -1 });
226
+ OrgUserSchema.index({ projectId: 1, email: 1 });
227
+ OrgUserSchema.index({ projectId: 1, emailVerified: 1, createdAt: -1 });
228
+ OrgUserSchema.index({ projectId: 1, firstName: 1, lastName: 1 });
223
229
  var OrgUser = mongoose2.model("OrgUser", OrgUserSchema);
224
230
 
225
231
  // src/utils/extract.ts
@@ -1160,7 +1166,7 @@ function createAuthRouter(options = {}) {
1160
1166
  value
1161
1167
  }));
1162
1168
  await user.save();
1163
- res.json({ ok: true, metadata: user.metadata });
1169
+ res.json({ ok: true, user });
1164
1170
  });
1165
1171
  r.get("/verify-email", async (req, res) => {
1166
1172
  const token = String(req.query.token || "");
@@ -1628,6 +1634,212 @@ function createAuthRouter(options = {}) {
1628
1634
  const user = await OrgUser.find({ projectId: req.query.projectId }).lean();
1629
1635
  res.json(user || null);
1630
1636
  });
1637
+ r.get("/get-users-by-pagination", async (req, res) => {
1638
+ try {
1639
+ const projectId = String(req.query.projectId || "");
1640
+ if (!projectId) {
1641
+ return res.status(400).json({
1642
+ ok: false,
1643
+ error: "projectId is required"
1644
+ });
1645
+ }
1646
+ const page = Math.max(
1647
+ 1,
1648
+ parseInt(String(req.query.page || "1"), 10) || 1
1649
+ );
1650
+ const limit = Math.min(
1651
+ 200,
1652
+ Math.max(1, parseInt(String(req.query.limit || "20"), 10) || 20)
1653
+ );
1654
+ const skip = (page - 1) * limit;
1655
+ const allowedSortFields = [
1656
+ "createdAt",
1657
+ "updatedAt",
1658
+ "email",
1659
+ "firstName",
1660
+ "lastName"
1661
+ ];
1662
+ const sortBy = allowedSortFields.includes(String(req.query.sortBy || "")) ? String(req.query.sortBy) : "createdAt";
1663
+ const sortOrder = String(req.query.sortOrder || "desc").toLowerCase() === "asc" ? 1 : -1;
1664
+ const sortObj = { [sortBy]: sortOrder };
1665
+ const allowedDateFields = ["createdAt", "updatedAt"];
1666
+ const dateField = allowedDateFields.includes(
1667
+ String(req.query.dateField || "")
1668
+ ) ? String(req.query.dateField) : "createdAt";
1669
+ const baseQuery = { projectId };
1670
+ if (req.query.emailVerified !== void 0) {
1671
+ const emailVerified = String(req.query.emailVerified).toLowerCase();
1672
+ if (emailVerified === "true" || emailVerified === "false") {
1673
+ baseQuery.emailVerified = emailVerified === "true";
1674
+ }
1675
+ }
1676
+ if (req.query.from || req.query.to) {
1677
+ const dateFilter = {};
1678
+ if (req.query.from) {
1679
+ const fromDate = new Date(String(req.query.from));
1680
+ if (!isNaN(fromDate.getTime())) {
1681
+ dateFilter.$gte = fromDate;
1682
+ }
1683
+ }
1684
+ if (req.query.to) {
1685
+ const toDate = new Date(String(req.query.to));
1686
+ if (!isNaN(toDate.getTime())) {
1687
+ dateFilter.$lte = toDate;
1688
+ }
1689
+ }
1690
+ if (Object.keys(dateFilter).length > 0) {
1691
+ baseQuery[dateField] = dateFilter;
1692
+ }
1693
+ }
1694
+ if (req.query.email) {
1695
+ const emailSearch = escapeRegex(String(req.query.email));
1696
+ baseQuery.email = { $regex: emailSearch, $options: "i" };
1697
+ }
1698
+ const projection = {
1699
+ id: 1,
1700
+ email: 1,
1701
+ firstName: 1,
1702
+ lastName: 1,
1703
+ orgId: 1,
1704
+ projectId: 1,
1705
+ roles: 1,
1706
+ emailVerified: 1,
1707
+ lastEmailSent: 1,
1708
+ lastPasswordReset: 1,
1709
+ metadata: 1,
1710
+ createdAt: 1,
1711
+ updatedAt: 1
1712
+ };
1713
+ if (req.query.name) {
1714
+ const nameSearch = escapeRegex(String(req.query.name));
1715
+ const nameRegex = { $regex: nameSearch, $options: "i" };
1716
+ const pipeline = [
1717
+ { $match: baseQuery },
1718
+ {
1719
+ $addFields: {
1720
+ fullName: {
1721
+ $concat: [
1722
+ { $ifNull: ["$firstName", ""] },
1723
+ " ",
1724
+ { $ifNull: ["$lastName", ""] }
1725
+ ]
1726
+ }
1727
+ }
1728
+ },
1729
+ {
1730
+ $match: {
1731
+ $or: [
1732
+ { firstName: nameRegex },
1733
+ { lastName: nameRegex },
1734
+ { fullName: nameRegex }
1735
+ ]
1736
+ }
1737
+ },
1738
+ {
1739
+ $facet: {
1740
+ data: [
1741
+ { $sort: sortObj },
1742
+ { $skip: skip },
1743
+ { $limit: limit },
1744
+ { $project: projection }
1745
+ ],
1746
+ total: [{ $count: "count" }]
1747
+ }
1748
+ }
1749
+ ];
1750
+ const result = await OrgUser.aggregate(pipeline);
1751
+ const data2 = result[0]?.data || [];
1752
+ const total2 = result[0]?.total[0]?.count || 0;
1753
+ const totalPages2 = Math.ceil(total2 / limit);
1754
+ return res.json({
1755
+ ok: true,
1756
+ page,
1757
+ limit,
1758
+ total: total2,
1759
+ totalPages: totalPages2,
1760
+ data: data2
1761
+ });
1762
+ }
1763
+ const [data, total] = await Promise.all([
1764
+ OrgUser.find(baseQuery).select(projection).sort(sortObj).skip(skip).limit(limit).lean(),
1765
+ OrgUser.countDocuments(baseQuery)
1766
+ ]);
1767
+ const totalPages = Math.ceil(total / limit);
1768
+ return res.json({
1769
+ ok: true,
1770
+ page,
1771
+ limit,
1772
+ total,
1773
+ totalPages,
1774
+ data
1775
+ });
1776
+ } catch (err) {
1777
+ console.error("Get users by pagination error:", err);
1778
+ return res.status(500).json({
1779
+ ok: false,
1780
+ error: "Internal server error"
1781
+ });
1782
+ }
1783
+ });
1784
+ r.get("/users/:userId", async (req, res) => {
1785
+ try {
1786
+ const userId = String(req.params.userId || "").trim();
1787
+ if (!userId) {
1788
+ return res.status(400).json({
1789
+ ok: false,
1790
+ error: "userId is required"
1791
+ });
1792
+ }
1793
+ const projectId = String(req.query.projectId || "").trim() || process.env.PROJECTID || "";
1794
+ if (!projectId) {
1795
+ return res.status(400).json({
1796
+ ok: false,
1797
+ error: "projectId is required"
1798
+ });
1799
+ }
1800
+ const projection = {
1801
+ id: 1,
1802
+ email: 1,
1803
+ firstName: 1,
1804
+ lastName: 1,
1805
+ orgId: 1,
1806
+ projectId: 1,
1807
+ roles: 1,
1808
+ emailVerified: 1,
1809
+ lastEmailSent: 1,
1810
+ lastPasswordReset: 1,
1811
+ metadata: 1,
1812
+ createdAt: 1,
1813
+ updatedAt: 1
1814
+ };
1815
+ let query = { projectId };
1816
+ if (mongoose5.Types.ObjectId.isValid(userId)) {
1817
+ query = {
1818
+ projectId,
1819
+ $or: [{ _id: new mongoose5.Types.ObjectId(userId) }, { id: userId }]
1820
+ };
1821
+ } else {
1822
+ query = { projectId, id: userId };
1823
+ }
1824
+ const user = await OrgUser.findOne(query).select(projection).lean();
1825
+ if (!user) {
1826
+ return res.status(404).json({
1827
+ ok: false,
1828
+ error: "User not found"
1829
+ });
1830
+ }
1831
+ return res.json({
1832
+ ok: true,
1833
+ data: user
1834
+ });
1835
+ } catch (err) {
1836
+ console.error("Get user by id error:", err);
1837
+ return res.status(500).json({
1838
+ ok: false,
1839
+ error: "Internal server error"
1840
+ });
1841
+ }
1842
+ });
1631
1843
  return r;
1632
1844
  }
1633
1845
  function setAuthCookies(res, tokens, cookie) {
@@ -1687,6 +1899,9 @@ async function sendRateLimitedEmail({
1687
1899
  await user.save();
1688
1900
  return { rateLimited: false };
1689
1901
  }
1902
+ function escapeRegex(str) {
1903
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1904
+ }
1690
1905
  function generateTokens(user) {
1691
1906
  const accessPayload = {
1692
1907
  sub: user.id.toString(),
@@ -1804,12 +2019,12 @@ import { Router as Router4 } from "express";
1804
2019
  import { randomUUID as randomUUID2 } from "crypto";
1805
2020
 
1806
2021
  // src/models/moduleConnection.model.ts
1807
- import mongoose5 from "mongoose";
1808
- var ModuleItemSchema = new mongoose5.Schema(
2022
+ import mongoose6 from "mongoose";
2023
+ var ModuleItemSchema = new mongoose6.Schema(
1809
2024
  { id: { type: String, required: true } },
1810
2025
  { _id: false }
1811
2026
  );
1812
- var ModuleConnectionSchema = new mongoose5.Schema(
2027
+ var ModuleConnectionSchema = new mongoose6.Schema(
1813
2028
  {
1814
2029
  projectId: { type: String, required: true, index: true },
1815
2030
  modules: {
@@ -1820,14 +2035,14 @@ var ModuleConnectionSchema = new mongoose5.Schema(
1820
2035
  },
1821
2036
  { timestamps: true, collection: "module_connection" }
1822
2037
  );
1823
- var ModuleConnection = mongoose5.model(
2038
+ var ModuleConnection = mongoose6.model(
1824
2039
  "ModuleConnection",
1825
2040
  ModuleConnectionSchema
1826
2041
  );
1827
2042
 
1828
2043
  // src/models/project.model.ts
1829
- import mongoose6 from "mongoose";
1830
- var ProjectSchema = new mongoose6.Schema(
2044
+ import mongoose7 from "mongoose";
2045
+ var ProjectSchema = new mongoose7.Schema(
1831
2046
  {
1832
2047
  _id: { type: String, required: true },
1833
2048
  org_id: { type: String, required: true, index: true },
@@ -1837,7 +2052,7 @@ var ProjectSchema = new mongoose6.Schema(
1837
2052
  },
1838
2053
  { timestamps: true, collection: "projects" }
1839
2054
  );
1840
- var Project = mongoose6.model("Project", ProjectSchema);
2055
+ var Project = mongoose7.model("Project", ProjectSchema);
1841
2056
 
1842
2057
  // src/services/projects.service.ts
1843
2058
  var ProjectsService = class {
@@ -1924,7 +2139,7 @@ function requireRole(...roles) {
1924
2139
  }
1925
2140
 
1926
2141
  // src/models/permissions.model.ts
1927
- import mongoose7, { Schema as Schema3 } from "mongoose";
2142
+ import mongoose8, { Schema as Schema3 } from "mongoose";
1928
2143
  var PermissionsSchema = new Schema3(
1929
2144
  {
1930
2145
  id: { type: String, required: true, index: true },
@@ -1940,7 +2155,7 @@ var PermissionsSchema = new Schema3(
1940
2155
  }
1941
2156
  );
1942
2157
  PermissionsSchema.index({ orgId: 1, key: 1 }, { unique: true });
1943
- var PermissionsModel = mongoose7.model(
2158
+ var PermissionsModel = mongoose8.model(
1944
2159
  "Permissions",
1945
2160
  PermissionsSchema,
1946
2161
  "permissions"