securepool 1.0.0

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 (126) hide show
  1. package/.dockerignore +7 -0
  2. package/.env.example +20 -0
  3. package/ARCHITECTURE.md +279 -0
  4. package/DEPLOYMENT.md +441 -0
  5. package/README.md +283 -0
  6. package/SETUP.md +388 -0
  7. package/apps/demo-backend/Dockerfile +33 -0
  8. package/apps/demo-backend/package.json +19 -0
  9. package/apps/demo-backend/src/index.ts +71 -0
  10. package/apps/demo-backend/tsconfig.json +8 -0
  11. package/apps/demo-frontend/.env.example +2 -0
  12. package/apps/demo-frontend/README.md +73 -0
  13. package/apps/demo-frontend/eslint.config.js +23 -0
  14. package/apps/demo-frontend/index.html +13 -0
  15. package/apps/demo-frontend/package.json +24 -0
  16. package/apps/demo-frontend/public/favicon.svg +1 -0
  17. package/apps/demo-frontend/public/icons.svg +24 -0
  18. package/apps/demo-frontend/src/App.tsx +33 -0
  19. package/apps/demo-frontend/src/assets/hero.png +0 -0
  20. package/apps/demo-frontend/src/assets/vite.svg +1 -0
  21. package/apps/demo-frontend/src/components/AccountSwitcher.tsx +373 -0
  22. package/apps/demo-frontend/src/components/ChangePasswordModal.tsx +128 -0
  23. package/apps/demo-frontend/src/index.css +272 -0
  24. package/apps/demo-frontend/src/main.tsx +10 -0
  25. package/apps/demo-frontend/src/pages/DashboardPage.tsx +141 -0
  26. package/apps/demo-frontend/src/pages/ForgotPasswordPage.tsx +183 -0
  27. package/apps/demo-frontend/src/pages/LoginPage.tsx +158 -0
  28. package/apps/demo-frontend/src/pages/OtpLoginPage.tsx +114 -0
  29. package/apps/demo-frontend/src/pages/SignupPage.tsx +95 -0
  30. package/apps/demo-frontend/src/pages/VerifyEmailPage.tsx +84 -0
  31. package/apps/demo-frontend/tsconfig.app.json +28 -0
  32. package/apps/demo-frontend/tsconfig.json +7 -0
  33. package/apps/demo-frontend/tsconfig.node.json +26 -0
  34. package/apps/demo-frontend/vite.config.ts +15 -0
  35. package/docs/DATABASE_MONGODB.md +280 -0
  36. package/docs/DATABASE_SQL.md +472 -0
  37. package/package.json +21 -0
  38. package/packages/api/package.json +30 -0
  39. package/packages/api/src/createSecurePool.ts +113 -0
  40. package/packages/api/src/index.ts +8 -0
  41. package/packages/api/src/middleware/authMiddleware.ts +26 -0
  42. package/packages/api/src/middleware/authorize.ts +24 -0
  43. package/packages/api/src/middleware/rateLimiter.ts +25 -0
  44. package/packages/api/src/middleware/tenantMiddleware.ts +12 -0
  45. package/packages/api/src/routes/authRoutes.ts +229 -0
  46. package/packages/api/src/routes/sessionRoutes.ts +30 -0
  47. package/packages/api/src/swagger.ts +529 -0
  48. package/packages/api/tsconfig.json +8 -0
  49. package/packages/application/package.json +16 -0
  50. package/packages/application/src/index.ts +17 -0
  51. package/packages/application/src/interfaces/IAuditLogRepository.ts +6 -0
  52. package/packages/application/src/interfaces/IAuthPlugin.ts +4 -0
  53. package/packages/application/src/interfaces/IEmailService.ts +3 -0
  54. package/packages/application/src/interfaces/IGoogleAuthService.ts +3 -0
  55. package/packages/application/src/interfaces/IOtpRepository.ts +8 -0
  56. package/packages/application/src/interfaces/IOtpService.ts +4 -0
  57. package/packages/application/src/interfaces/IPasswordHasher.ts +4 -0
  58. package/packages/application/src/interfaces/IRoleRepository.ts +8 -0
  59. package/packages/application/src/interfaces/ISessionRepository.ts +8 -0
  60. package/packages/application/src/interfaces/ITokenRepository.ts +9 -0
  61. package/packages/application/src/interfaces/ITokenService.ts +5 -0
  62. package/packages/application/src/interfaces/IUserRepository.ts +8 -0
  63. package/packages/application/src/services/AuthService.ts +323 -0
  64. package/packages/application/src/services/RefreshTokenService.ts +53 -0
  65. package/packages/application/tsconfig.json +8 -0
  66. package/packages/core/package.json +13 -0
  67. package/packages/core/src/entities/AuditLog.ts +11 -0
  68. package/packages/core/src/entities/OtpCode.ts +10 -0
  69. package/packages/core/src/entities/RefreshToken.ts +9 -0
  70. package/packages/core/src/entities/Role.ts +6 -0
  71. package/packages/core/src/entities/Session.ts +10 -0
  72. package/packages/core/src/entities/Tenant.ts +7 -0
  73. package/packages/core/src/entities/User.ts +10 -0
  74. package/packages/core/src/entities/UserRole.ts +6 -0
  75. package/packages/core/src/enums/index.ts +22 -0
  76. package/packages/core/src/index.ts +10 -0
  77. package/packages/core/tsconfig.json +8 -0
  78. package/packages/infrastructure/package.json +24 -0
  79. package/packages/infrastructure/src/email/NodemailerEmailService.ts +55 -0
  80. package/packages/infrastructure/src/google/GoogleAuthServiceImpl.ts +28 -0
  81. package/packages/infrastructure/src/hashing/BcryptHasher.ts +18 -0
  82. package/packages/infrastructure/src/index.ts +6 -0
  83. package/packages/infrastructure/src/jwt/JwtTokenService.ts +32 -0
  84. package/packages/infrastructure/src/otp/OtpServiceImpl.ts +50 -0
  85. package/packages/infrastructure/tsconfig.json +8 -0
  86. package/packages/persistence/package.json +22 -0
  87. package/packages/persistence/prisma/schema.prisma +88 -0
  88. package/packages/persistence/src/factory.ts +48 -0
  89. package/packages/persistence/src/index.ts +30 -0
  90. package/packages/persistence/src/mongo/connection.ts +9 -0
  91. package/packages/persistence/src/mongo/models/AuditLogModel.ts +21 -0
  92. package/packages/persistence/src/mongo/models/OtpModel.ts +19 -0
  93. package/packages/persistence/src/mongo/models/RefreshTokenModel.ts +17 -0
  94. package/packages/persistence/src/mongo/models/RoleModel.ts +11 -0
  95. package/packages/persistence/src/mongo/models/SessionModel.ts +19 -0
  96. package/packages/persistence/src/mongo/models/UserModel.ts +21 -0
  97. package/packages/persistence/src/mongo/models/UserRoleModel.ts +15 -0
  98. package/packages/persistence/src/mongo/repositories/MongoAuditLogRepository.ts +29 -0
  99. package/packages/persistence/src/mongo/repositories/MongoOtpRepository.ts +34 -0
  100. package/packages/persistence/src/mongo/repositories/MongoRoleRepository.ts +32 -0
  101. package/packages/persistence/src/mongo/repositories/MongoSessionRepository.ts +29 -0
  102. package/packages/persistence/src/mongo/repositories/MongoTokenRepository.ts +34 -0
  103. package/packages/persistence/src/mongo/repositories/MongoUserRepository.ts +37 -0
  104. package/packages/persistence/src/prisma/repositories/PrismaAuditLogRepository.ts +37 -0
  105. package/packages/persistence/src/prisma/repositories/PrismaOtpRepository.ts +43 -0
  106. package/packages/persistence/src/prisma/repositories/PrismaRoleRepository.ts +36 -0
  107. package/packages/persistence/src/prisma/repositories/PrismaSessionRepository.ts +39 -0
  108. package/packages/persistence/src/prisma/repositories/PrismaTokenRepository.ts +50 -0
  109. package/packages/persistence/src/prisma/repositories/PrismaUserRepository.ts +45 -0
  110. package/packages/persistence/tsconfig.json +8 -0
  111. package/packages/react-sdk/package.json +23 -0
  112. package/packages/react-sdk/src/components/GoogleLoginButton.tsx +54 -0
  113. package/packages/react-sdk/src/components/LoginForm.tsx +67 -0
  114. package/packages/react-sdk/src/components/OTPVerification.tsx +104 -0
  115. package/packages/react-sdk/src/components/SessionList.tsx +64 -0
  116. package/packages/react-sdk/src/components/SignupForm.tsx +95 -0
  117. package/packages/react-sdk/src/context/AuthContext.ts +4 -0
  118. package/packages/react-sdk/src/context/SecurePoolProvider.tsx +492 -0
  119. package/packages/react-sdk/src/hooks/useAuth.ts +11 -0
  120. package/packages/react-sdk/src/index.ts +22 -0
  121. package/packages/react-sdk/src/types.ts +53 -0
  122. package/packages/react-sdk/tsconfig.json +12 -0
  123. package/scripts/setup.js +285 -0
  124. package/scripts/setup.sh +309 -0
  125. package/tsconfig.base.json +16 -0
  126. package/turbo.json +16 -0
@@ -0,0 +1,280 @@
1
+ # SecurePool — MongoDB Setup Guide
2
+
3
+ This guide covers using SecurePool with **MongoDB** (the default database).
4
+
5
+ ---
6
+
7
+ ## How It Works
8
+
9
+ SecurePool uses **Mongoose** as the ODM for MongoDB. When you set `type: "mongo"`, the persistence layer auto-creates all collections and indexes.
10
+
11
+ ```ts
12
+ createSecurePool({
13
+ database: {
14
+ type: "mongo",
15
+ url: "mongodb://localhost:27017/securepool"
16
+ },
17
+ ...
18
+ })
19
+ ```
20
+
21
+ No migration step needed — Mongoose creates collections on first use.
22
+
23
+ ---
24
+
25
+ ## Option 1: Local MongoDB (Development)
26
+
27
+ ### Install MongoDB
28
+
29
+ ```bash
30
+ # macOS
31
+ brew tap mongodb/brew
32
+ brew install mongodb-community
33
+ ```
34
+
35
+ ### Start MongoDB
36
+
37
+ **Without authentication (simplest):**
38
+
39
+ ```bash
40
+ brew services start mongodb-community
41
+ ```
42
+
43
+ **With authentication (production-like):**
44
+
45
+ ```bash
46
+ # Create data directory
47
+ mkdir -p ~/mongodb-data
48
+
49
+ # Start with auth enabled
50
+ mongod --dbpath ~/mongodb-data --auth
51
+ ```
52
+
53
+ ### Create Database User (if using auth)
54
+
55
+ Open a new terminal:
56
+
57
+ ```bash
58
+ mongosh
59
+ ```
60
+
61
+ ```js
62
+ // Create the database and user
63
+ use securepool
64
+
65
+ db.createUser({
66
+ user: "securepool-user",
67
+ pwd: "SecurePool@123",
68
+ roles: [{ role: "readWrite", db: "securepool" }]
69
+ })
70
+
71
+ exit
72
+ ```
73
+
74
+ Verify:
75
+
76
+ ```bash
77
+ mongosh "mongodb://securepool-user:SecurePool%40123@localhost:27017/securepool?authSource=securepool"
78
+ ```
79
+
80
+ ### .env Configuration
81
+
82
+ **Without auth:**
83
+
84
+ ```env
85
+ DB_TYPE=mongo
86
+ DB_URL=mongodb://localhost:27017/securepool
87
+ ```
88
+
89
+ **With auth:**
90
+
91
+ ```env
92
+ DB_TYPE=mongo
93
+ DB_URL=mongodb://securepool-user:SecurePool%40123@localhost:27017/securepool?authSource=securepool
94
+ ```
95
+
96
+ > Note: `@` in passwords must be encoded as `%40`.
97
+
98
+ ---
99
+
100
+ ## Option 2: MongoDB Atlas (Production / Cloud)
101
+
102
+ ### Step 1: Create Free Cluster
103
+
104
+ 1. Go to https://cloud.mongodb.com
105
+ 2. Sign up / Sign in
106
+ 3. Click **"Build a Database"**
107
+ 4. Select **M0 Free Tier** (512MB, free forever)
108
+ 5. Choose your region (closest to your backend server)
109
+ 6. Click **Create**
110
+
111
+ ### Step 2: Create Database User
112
+
113
+ 1. Go to **Database Access** → **Add New Database User**
114
+ 2. Set:
115
+ - Username: `securepool-user`
116
+ - Password: `SecurePool@123` (or generate a strong one)
117
+ - Role: **Read and write to any database**
118
+ 3. Click **Add User**
119
+
120
+ ### Step 3: Allow Network Access
121
+
122
+ 1. Go to **Network Access** → **Add IP Address**
123
+ 2. For development: Click **"Allow Access from Anywhere"** (`0.0.0.0/0`)
124
+ 3. For production: Add your backend server's IP only
125
+ 4. Click **Confirm**
126
+
127
+ ### Step 4: Get Connection String
128
+
129
+ 1. Go to **Database** → Click **"Connect"**
130
+ 2. Choose **"Drivers"** → Node.js
131
+ 3. Copy the connection string:
132
+
133
+ ```
134
+ mongodb+srv://securepool-user:<password>@cluster0.xxxxx.mongodb.net/securepool?retryWrites=true&w=majority
135
+ ```
136
+
137
+ 4. Replace `<password>` with your URL-encoded password
138
+
139
+ ### .env Configuration
140
+
141
+ ```env
142
+ DB_TYPE=mongo
143
+ DB_URL=mongodb+srv://securepool-user:SecurePool%40123@cluster0.xxxxx.mongodb.net/securepool?retryWrites=true&w=majority
144
+ ```
145
+
146
+ ### Verify Connection
147
+
148
+ ```bash
149
+ mongosh "mongodb+srv://securepool-user:SecurePool%40123@cluster0.xxxxx.mongodb.net/securepool"
150
+ ```
151
+
152
+ ---
153
+
154
+ ## Collections Created Automatically
155
+
156
+ When SecurePool starts with MongoDB, these collections are auto-created:
157
+
158
+ | Collection | Description |
159
+ |---|---|
160
+ | `users` | User accounts (email, password hash, tenant, verified status) |
161
+ | `roles` | Role definitions (admin, user, etc.) |
162
+ | `userroles` | User-role assignments (many-to-many) |
163
+ | `refreshtokens` | JWT refresh tokens (for token rotation) |
164
+ | `otpcodes` | OTP codes (for email verification, login, password reset) |
165
+ | `sessions` | Active sessions (device, IP, timestamp) |
166
+ | `auditlogs` | Audit trail (login, register, password change events) |
167
+
168
+ ### Indexes
169
+
170
+ The following indexes are auto-created for performance:
171
+
172
+ | Collection | Index | Purpose |
173
+ |---|---|---|
174
+ | `users` | `{ email: 1, tenantId: 1 }` unique | Prevent duplicate emails per tenant |
175
+ | `users` | `{ tenantId: 1 }` | Fast tenant-scoped queries |
176
+ | `refreshtokens` | `{ tokenHash: 1 }` | Fast token lookup on refresh |
177
+ | `refreshtokens` | `{ userId: 1 }` | Find all tokens for a user |
178
+ | `sessions` | `{ userId: 1 }` | List sessions for a user |
179
+ | `otpcodes` | `{ userId: 1 }` | Find OTP for a user |
180
+ | `auditlogs` | `{ userId: 1 }` | Audit trail per user |
181
+ | `auditlogs` | `{ tenantId: 1 }` | Audit trail per tenant |
182
+
183
+ ---
184
+
185
+ ## MongoDB URL Format Reference
186
+
187
+ | Scenario | URL Format |
188
+ |---|---|
189
+ | Local, no auth | `mongodb://localhost:27017/securepool` |
190
+ | Local, with auth | `mongodb://user:pass%40123@localhost:27017/securepool?authSource=securepool` |
191
+ | Atlas (cloud) | `mongodb+srv://user:pass@cluster0.xxx.mongodb.net/securepool` |
192
+ | Custom port | `mongodb://localhost:27018/securepool` |
193
+ | Replica set | `mongodb://host1:27017,host2:27017/securepool?replicaSet=rs0` |
194
+
195
+ ---
196
+
197
+ ## Backup & Restore
198
+
199
+ ### Backup
200
+
201
+ ```bash
202
+ mongodump --uri="mongodb://localhost:27017/securepool" --out=./backup
203
+ ```
204
+
205
+ ### Restore
206
+
207
+ ```bash
208
+ mongorestore --uri="mongodb://localhost:27017/securepool" ./backup/securepool
209
+ ```
210
+
211
+ ### Atlas Backup
212
+
213
+ Atlas M0 free tier doesn't include automated backups. For M2+ tiers, backups are automatic. For M0, use `mongodump` manually.
214
+
215
+ ---
216
+
217
+ ## Monitoring
218
+
219
+ ### Check collections via mongosh
220
+
221
+ ```bash
222
+ mongosh "mongodb://localhost:27017/securepool"
223
+ ```
224
+
225
+ ```js
226
+ // List all collections
227
+ show collections
228
+
229
+ // Count users
230
+ db.users.countDocuments()
231
+
232
+ // Find a user
233
+ db.users.findOne({ email: "test@example.com" })
234
+
235
+ // List active sessions
236
+ db.sessions.find({ isActive: true })
237
+
238
+ // View recent audit logs
239
+ db.auditlogs.find().sort({ timestamp: -1 }).limit(10)
240
+ ```
241
+
242
+ ### Via MongoDB Compass (GUI)
243
+
244
+ 1. Download from https://www.mongodb.com/products/compass
245
+ 2. Connect with your URL
246
+ 3. Browse collections, run queries, view indexes
247
+
248
+ ---
249
+
250
+ ## Troubleshooting
251
+
252
+ ### "Authentication failed"
253
+
254
+ - Check username/password are correct
255
+ - Ensure `authSource` matches the database where the user was created
256
+ - URL-encode special characters: `@` → `%40`, `#` → `%23`
257
+
258
+ ### "Connection refused"
259
+
260
+ ```bash
261
+ # Check if MongoDB is running
262
+ brew services list | grep mongodb
263
+ # or
264
+ lsof -i:27017
265
+ ```
266
+
267
+ ### "Duplicate key error"
268
+
269
+ This means a user with the same email + tenantId already exists. SecurePool normalizes emails (trim + lowercase) to prevent duplicates.
270
+
271
+ ### Slow queries
272
+
273
+ Check if indexes exist:
274
+
275
+ ```js
276
+ db.users.getIndexes()
277
+ db.sessions.getIndexes()
278
+ ```
279
+
280
+ If missing, restart SecurePool — Mongoose auto-creates them on startup.