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
package/.dockerignore ADDED
@@ -0,0 +1,7 @@
1
+ node_modules
2
+ dist
3
+ .turbo
4
+ *.pem
5
+ .env
6
+ .env.local
7
+ apps/demo-frontend
package/.env.example ADDED
@@ -0,0 +1,20 @@
1
+ # ===== DATABASE =====
2
+ DB_TYPE=mongo
3
+ DB_URL=mongodb+srv://user:pass@cluster.mongodb.net/securepool
4
+
5
+ # ===== JWT (paste the full key content, use \n for newlines) =====
6
+ JWT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nMIIE...\n-----END RSA PRIVATE KEY-----"
7
+ JWT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nMIIB...\n-----END PUBLIC KEY-----"
8
+
9
+ # ===== EMAIL (Gmail SMTP) =====
10
+ EMAIL_HOST=smtp.gmail.com
11
+ EMAIL_PORT=587
12
+ EMAIL_SECURE=false
13
+ EMAIL_USER=your-email@gmail.com
14
+ EMAIL_PASS=your-app-password
15
+ EMAIL_FROM=your-email@gmail.com
16
+
17
+ # ===== SERVER =====
18
+ PORT=5001
19
+ CORS_ORIGINS=https://your-frontend.vercel.app
20
+ RATE_LIMIT_ENABLED=true
@@ -0,0 +1,279 @@
1
+ # SecurePool - Architecture Guide
2
+
3
+ ## What is SecurePool?
4
+
5
+ SecurePool is a **plug-and-play authentication framework** distributed as NPM packages. Other developers install it and get a full auth system (JWT, OTP, sessions, multi-tenancy) without building one from scratch.
6
+
7
+ Think of it like **Auth0 or Firebase Auth** — but self-hosted and open-source.
8
+
9
+ ---
10
+
11
+ ## Monorepo Structure
12
+
13
+ ```
14
+ securepool/
15
+
16
+ ├── packages/ ← THE PRODUCT (published to npm)
17
+ │ ├── core/ @securepool/core
18
+ │ ├── application/ @securepool/application
19
+ │ ├── infrastructure/ @securepool/infrastructure
20
+ │ ├── persistence/ @securepool/persistence
21
+ │ ├── api/ @securepool/api
22
+ │ └── react-sdk/ @securepool/react-sdk
23
+
24
+ ├── apps/ ← DEMO APPS (for testing, not published)
25
+ │ ├── demo-backend/ Example server using @securepool/api
26
+ │ └── demo-frontend/ Example React app using @securepool/react-sdk
27
+
28
+ ├── scripts/ ← Automation
29
+ │ └── setup.sh One-command setup script
30
+
31
+ ├── .env ← Secrets (never in git)
32
+ ├── private.pem ← JWT private key (never in git)
33
+ ├── public.pem ← JWT public key (never in git)
34
+ ├── package.json ← Monorepo root (npm workspaces)
35
+ ├── tsconfig.base.json ← Shared TypeScript config
36
+ ├── turbo.json ← Turborepo build pipeline
37
+ ├── SETUP.md ← How to run locally
38
+ ├── ARCHITECTURE.md ← This file
39
+ └── .env.example ← Template for .env
40
+ ```
41
+
42
+ ---
43
+
44
+ ## packages/ vs apps/ — The Key Distinction
45
+
46
+ ### `packages/*` = The Library (YOUR PRODUCT)
47
+
48
+ This is the code that gets **published to npm**. Other developers install it:
49
+
50
+ ```bash
51
+ npm install @securepool/api # For their backend
52
+ npm install @securepool/react-sdk # For their frontend
53
+ ```
54
+
55
+ **You do NOT deploy packages.** They live on the npm registry. They are libraries, not running servers.
56
+
57
+ ### `apps/*` = Demo Applications (FOR TESTING)
58
+
59
+ These are small apps that **use** your packages. They exist so you can:
60
+
61
+ - Test your packages during development
62
+ - Show other developers how to use your library
63
+ - Deploy a working demo
64
+
65
+ **These are what you deploy** to Railway/Vercel.
66
+
67
+ ### Side-by-Side Comparison
68
+
69
+ | | `packages/*` | `apps/*` |
70
+ |---|---|---|
71
+ | Purpose | The library (product) | Example usage / testing |
72
+ | Published to npm? | **Yes** | No |
73
+ | Deployed as server? | No | **Yes** |
74
+ | Who uses it? | Other developers | Only you |
75
+ | Size | Thousands of lines | ~20 lines each |
76
+ | Analogy | The `express` npm package | Express's example app |
77
+
78
+ ---
79
+
80
+ ## Package Dependency Flow
81
+
82
+ ```
83
+ @securepool/core ← Zero dependencies. Pure entities & enums.
84
+
85
+ @securepool/application ← Depends on core. Interfaces & business logic.
86
+
87
+ @securepool/infrastructure ← Depends on core + application.
88
+ ↓ JWT, bcrypt, OTP, email implementations.
89
+ @securepool/persistence ← Depends on core + application.
90
+ ↓ MongoDB & PostgreSQL repositories.
91
+ @securepool/api ← Depends on ALL above.
92
+ Express routes, middleware, createSecurePool().
93
+
94
+ @securepool/react-sdk ← Independent. React provider, hooks, components.
95
+ Only talks to the API via HTTP.
96
+ ```
97
+
98
+ ### Why separate packages?
99
+
100
+ A developer building a backend-only system installs:
101
+ ```bash
102
+ npm install @securepool/api
103
+ ```
104
+
105
+ A developer building a React frontend installs:
106
+ ```bash
107
+ npm install @securepool/react-sdk
108
+ ```
109
+
110
+ They don't need each other's code. This is how enterprise libraries work.
111
+
112
+ ---
113
+
114
+ ## Clean Architecture Layers
115
+
116
+ ```
117
+ ┌─────────────────────────────────────────────┐
118
+ │ API Layer (@securepool/api) │ Express routes, middleware
119
+ │ ─ depends on everything below ─ │ Entry point: createSecurePool()
120
+ ├─────────────────────────────────────────────┤
121
+ │ Infrastructure (@securepool/infrastructure)│ JWT (jsonwebtoken), bcrypt,
122
+ │ ─ implements application interfaces ─ │ OTP, Google SSO, Nodemailer
123
+ ├─────────────────────────────────────────────┤
124
+ │ Persistence (@securepool/persistence) │ MongoDB (mongoose) repos
125
+ │ ─ implements application interfaces ─ │ PostgreSQL (prisma) repos
126
+ ├─────────────────────────────────────────────┤
127
+ │ Application (@securepool/application) │ AuthService, RefreshTokenService
128
+ │ ─ depends only on core ─ │ Interfaces (IUserRepository, etc.)
129
+ ├─────────────────────────────────────────────┤
130
+ │ Core (@securepool/core) │ User, Role, Session, OtpCode,
131
+ │ ─ zero dependencies ─ │ RefreshToken, AuditLog, enums
132
+ └─────────────────────────────────────────────┘
133
+ ```
134
+
135
+ **Rule:** Each layer can only depend on layers BELOW it. Never above. Never sideways.
136
+
137
+ **Why?** If you swap MongoDB for PostgreSQL, only the persistence layer changes. Business logic (application) stays untouched.
138
+
139
+ ---
140
+
141
+ ## What Gets Deployed Where
142
+
143
+ ```
144
+ ┌──────────────────────────────────────┐
145
+ │ npm registry (npmjs.com) │
146
+ │ │
147
+ │ Publish: │
148
+ │ ├── @securepool/core │
149
+ │ ├── @securepool/application │
150
+ │ ├── @securepool/infrastructure │
151
+ │ ├── @securepool/persistence │
152
+ │ ├── @securepool/api │
153
+ │ └── @securepool/react-sdk │
154
+ │ │
155
+ │ Command: npm publish --access public│
156
+ └──────────────────────────────────────┘
157
+
158
+ ┌──────────────────────────────────────┐
159
+ │ Server (Railway / Render / AWS) │
160
+ │ │
161
+ │ Deploy: apps/demo-backend │
162
+ │ What it does: runs createSecurePool │
163
+ │ and listens on a port │
164
+ │ │
165
+ │ This is a 20-line file that USES │
166
+ │ @securepool/api │
167
+ └──────────────────────────────────────┘
168
+
169
+ ┌──────────────────────────────────────┐
170
+ │ Static Hosting (Vercel / Netlify) │
171
+ │ │
172
+ │ Deploy: apps/demo-frontend │
173
+ │ What it does: serves the React app │
174
+ │ that USES @securepool/react-sdk │
175
+ │ │
176
+ │ Build output: static HTML/JS/CSS │
177
+ └──────────────────────────────────────┘
178
+ ```
179
+
180
+ ---
181
+
182
+ ## Real-World Analogy
183
+
184
+ | Auth0 (the company) | SecurePool (your project) |
185
+ |---|---|
186
+ | `auth0` npm package | `packages/api` — the backend library |
187
+ | `@auth0/auth0-react` npm package | `packages/react-sdk` — the React library |
188
+ | Auth0's dashboard website | `apps/demo-frontend` — your demo UI |
189
+ | Auth0's API servers | `apps/demo-backend` — your demo server |
190
+ | Auth0's internal modules | `packages/core`, `application`, `infrastructure`, `persistence` |
191
+
192
+ ---
193
+
194
+ ## How Another Developer Uses SecurePool
195
+
196
+ ### Their Backend (their own project, not yours)
197
+
198
+ ```bash
199
+ npm install @securepool/api
200
+ ```
201
+
202
+ ```ts
203
+ // their-project/server.ts
204
+ import { createSecurePool } from "@securepool/api";
205
+
206
+ const { app } = await createSecurePool({
207
+ database: { type: "mongo", url: process.env.MONGO_URL },
208
+ jwt: { privateKey: process.env.JWT_KEY, publicKey: process.env.JWT_PUB },
209
+ });
210
+
211
+ app.listen(3000);
212
+ // They now have full auth: /auth/login, /auth/register, /sessions, etc.
213
+ ```
214
+
215
+ ### Their Frontend (their own React app)
216
+
217
+ ```bash
218
+ npm install @securepool/react-sdk
219
+ ```
220
+
221
+ ```tsx
222
+ // their-project/App.tsx
223
+ import { SecurePoolProvider, LoginForm, useAuth } from "@securepool/react-sdk";
224
+
225
+ function App() {
226
+ return (
227
+ <SecurePoolProvider config={{ apiBaseUrl: "https://their-api.com", tenantId: "their-tenant" }}>
228
+ <LoginForm onSuccess={() => console.log("logged in!")} />
229
+ </SecurePoolProvider>
230
+ );
231
+ }
232
+ ```
233
+
234
+ ---
235
+
236
+ ## Features Implemented
237
+
238
+ | Feature | Package | Level |
239
+ |---|---|---|
240
+ | User, Role, Session entities | core | Level 2 |
241
+ | Repository interfaces, AuthService | application | Level 3 |
242
+ | JWT (RS256), bcrypt, OTP, Google SSO | infrastructure | Level 5 |
243
+ | MongoDB + PostgreSQL repositories | persistence | Level 5-6 |
244
+ | Express routes, middleware, rate limiting | api | Level 7 |
245
+ | React provider, hooks, UI components | react-sdk | Level 5 |
246
+ | Multi-tenancy | core + application | Level 4 |
247
+ | Session & device management | api + react-sdk | Level 4 |
248
+ | Audit logging | application + persistence | Level 4 |
249
+ | Refresh token rotation | application | Level 3 |
250
+ | Email OTP via Nodemailer | infrastructure | Level 6 |
251
+ | Forgot/reset/change password | application + api | Level 6 |
252
+ | Multi-account switching | react-sdk | Level 6 |
253
+ | Swagger API documentation | api | Level 7 |
254
+ | Role-based access control (RBAC) | api middleware | Level 3 |
255
+
256
+ ---
257
+
258
+ ## Useful Commands
259
+
260
+ | Command | What |
261
+ |---|---|
262
+ | `npm run setup` | Interactive first-time setup |
263
+ | `npm run build` | Build all packages |
264
+ | `npm run start:backend` | Start API server (port 5001) |
265
+ | `npm run start:frontend` | Start React app (port 5173) |
266
+ | `npm run clean` | Delete all dist folders |
267
+
268
+ ---
269
+
270
+ ## Files You Should NEVER Commit
271
+
272
+ | File | Why |
273
+ |---|---|
274
+ | `.env` | Contains DB credentials, email passwords |
275
+ | `private.pem` | JWT signing key — anyone with this can forge tokens |
276
+ | `public.pem` | JWT verification key |
277
+ | `node_modules/` | Dependencies (reinstall with npm install) |
278
+
279
+ These are in `.gitignore`. In production, set them as environment variables on your hosting platform.