securenow 7.6.6 → 7.6.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/NPM_README.md +13 -13
- package/README.md +21 -37
- package/app-config.js +5 -3
- package/cli/config.js +4 -3
- package/cli/diagnostics.js +54 -15
- package/cli/run.js +40 -11
- package/firewall-only.js +1 -1
- package/firewall.js +88 -57
- package/mcp/catalog.js +1 -1
- package/nextjs-webpack-config.js +3 -15
- package/nextjs.js +21 -23
- package/nuxt-server-plugin.mjs +20 -10
- package/package.json +33 -34
- package/register.js +1 -1
- package/tracing.js +17 -7
- package/web-vite.mjs +23 -13
- package/CONSUMING-APPS-GUIDE.md +0 -463
- package/docs/ALL-FRAMEWORKS-QUICKSTART.md +0 -1388
- package/docs/API-KEYS-GUIDE.md +0 -278
- package/docs/ARCHITECTURE.md +0 -408
- package/docs/AUTO-BODY-CAPTURE.md +0 -412
- package/docs/AUTO-SETUP-SUMMARY.md +0 -331
- package/docs/AUTO-SETUP.md +0 -419
- package/docs/AUTOMATIC-IP-CAPTURE.md +0 -359
- package/docs/BODY-CAPTURE-FIX.md +0 -261
- package/docs/BODY-CAPTURE-QUICKSTART.md +0 -147
- package/docs/CHANGELOG-NEXTJS.md +0 -235
- package/docs/COMPLETION-REPORT.md +0 -408
- package/docs/CUSTOMER-GUIDE.md +0 -364
- package/docs/EASIEST-SETUP.md +0 -342
- package/docs/ENVIRONMENT-VARIABLES.md +0 -166
- package/docs/ENVIRONMENTS.md +0 -60
- package/docs/EXPRESS-BODY-CAPTURE.md +0 -1028
- package/docs/EXPRESS-SETUP-GUIDE.md +0 -722
- package/docs/FINAL-SOLUTION.md +0 -335
- package/docs/FIREWALL-GUIDE.md +0 -440
- package/docs/IMPLEMENTATION-SUMMARY.md +0 -410
- package/docs/INDEX.md +0 -222
- package/docs/LOGGING-GUIDE.md +0 -704
- package/docs/LOGGING-QUICKSTART.md +0 -221
- package/docs/MCP-GUIDE.md +0 -58
- package/docs/NEXTJS-BODY-CAPTURE-COMPARISON.md +0 -323
- package/docs/NEXTJS-BODY-CAPTURE.md +0 -368
- package/docs/NEXTJS-GUIDE.md +0 -392
- package/docs/NEXTJS-QUICKSTART.md +0 -83
- package/docs/NEXTJS-SETUP-COMPLETE.md +0 -795
- package/docs/NEXTJS-WEBPACK-WARNINGS.md +0 -267
- package/docs/NEXTJS-WRAPPER-APPROACH.md +0 -414
- package/docs/NUXT-GUIDE.md +0 -173
- package/docs/QUICKSTART-BODY-CAPTURE.md +0 -293
- package/docs/REDACTION-EXAMPLES.md +0 -484
- package/docs/REQUEST-BODY-CAPTURE.md +0 -587
- package/docs/SOLUTION-SUMMARY.md +0 -312
- package/docs/VERCEL-OTEL-MIGRATION.md +0 -255
- package/examples/README.md +0 -265
- package/examples/express-with-logging.js +0 -137
- package/examples/instrumentation-with-auto-capture.ts +0 -41
- package/examples/next.config.js +0 -37
- package/examples/nextjs-api-route-with-body-capture.ts +0 -54
- package/examples/nextjs-env-example.txt +0 -32
- package/examples/nextjs-instrumentation.js +0 -36
- package/examples/nextjs-instrumentation.ts +0 -36
- package/examples/nextjs-middleware.js +0 -37
- package/examples/nextjs-middleware.ts +0 -37
- package/examples/nextjs-with-logging-example.md +0 -301
- package/examples/nextjs-with-options.ts +0 -36
- package/examples/test-nextjs-setup.js +0 -70
- package/postinstall.js +0 -296
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
# SecureNow Logging — Quick Start
|
|
2
|
-
|
|
3
|
-
Get logging sent to your SecureNow dashboard in under 2 minutes.
|
|
4
|
-
|
|
5
|
-
**Since v7.0.0:** Logging is **on by default**. `console.log` / `warn` / `error` / `info` / `debug` calls are automatically forwarded as OTLP log records. Disable with `SECURENOW_LOGGING_ENABLED=0` if you don't want it.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## 1. Install + login
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm install securenow
|
|
13
|
-
npx securenow login # pick/create your app in the browser
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
`login` writes `.securenow/credentials.json` locally. No `.env` setup required.
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## 2. Add to Your App
|
|
21
|
-
|
|
22
|
-
**Option A: Automatic Console Logging (Easiest)**
|
|
23
|
-
|
|
24
|
-
Add this line at the top of your main file:
|
|
25
|
-
|
|
26
|
-
```javascript
|
|
27
|
-
// app.js, index.js, server.js, or main.ts
|
|
28
|
-
require('securenow/register');
|
|
29
|
-
|
|
30
|
-
// That's it! Now use console normally
|
|
31
|
-
console.log('App started');
|
|
32
|
-
console.error('An error occurred', { userId: 123 });
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
**Option B: Use NODE_OPTIONS (No code changes)**
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
NODE_OPTIONS="-r securenow/register" node app.js
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
---
|
|
42
|
-
|
|
43
|
-
## 3. Run Your App
|
|
44
|
-
|
|
45
|
-
```bash
|
|
46
|
-
node app.js
|
|
47
|
-
# or
|
|
48
|
-
npm start
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
You should see:
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
[securenow] OTel SDK started → http://your-otlp-backend:4318/v1/traces
|
|
55
|
-
[securenow] 📋 Logging: ENABLED → http://your-otlp-backend:4318/v1/logs
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## 4. View Logs in SecureNow
|
|
61
|
-
|
|
62
|
-
1. Open your SecureNow dashboard
|
|
63
|
-
2. Go to **Logs** section
|
|
64
|
-
3. Your logs appear under the app you picked during `securenow login`
|
|
65
|
-
4. All logs come with automatic trace correlation
|
|
66
|
-
|
|
67
|
-
Or from the CLI:
|
|
68
|
-
|
|
69
|
-
```bash
|
|
70
|
-
npx securenow logs --minutes 5
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
---
|
|
74
|
-
|
|
75
|
-
## Framework-Specific Examples
|
|
76
|
-
|
|
77
|
-
### Express.js
|
|
78
|
-
|
|
79
|
-
```javascript
|
|
80
|
-
// app.js
|
|
81
|
-
require('securenow/register');
|
|
82
|
-
|
|
83
|
-
const express = require('express');
|
|
84
|
-
const app = express();
|
|
85
|
-
|
|
86
|
-
app.get('/', (req, res) => {
|
|
87
|
-
console.log('Request received');
|
|
88
|
-
res.send('Hello World');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
app.listen(3000);
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Next.js
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
// instrumentation.ts (in project root)
|
|
98
|
-
import { registerSecureNow } from 'securenow/nextjs';
|
|
99
|
-
export function register() {
|
|
100
|
-
registerSecureNow();
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
No `.env.local` needed — credentials come from `.securenow/credentials.json` after `npx securenow login`.
|
|
105
|
-
|
|
106
|
-
### Fastify
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
// server.js
|
|
110
|
-
require('securenow/register');
|
|
111
|
-
|
|
112
|
-
const fastify = require('fastify')();
|
|
113
|
-
|
|
114
|
-
fastify.get('/', async () => {
|
|
115
|
-
console.log('Route called');
|
|
116
|
-
return { hello: 'world' };
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
fastify.listen({ port: 3000 });
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### NestJS
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
// main.ts
|
|
126
|
-
require('securenow/register');
|
|
127
|
-
|
|
128
|
-
import { NestFactory } from '@nestjs/core';
|
|
129
|
-
import { AppModule } from './app.module';
|
|
130
|
-
|
|
131
|
-
async function bootstrap() {
|
|
132
|
-
const app = await NestFactory.create(AppModule);
|
|
133
|
-
console.log('App starting');
|
|
134
|
-
await app.listen(3000);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
bootstrap();
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
---
|
|
141
|
-
|
|
142
|
-
## Troubleshooting
|
|
143
|
-
|
|
144
|
-
**Logs not appearing?**
|
|
145
|
-
|
|
146
|
-
1. Check `.securenow/credentials.json` exists (run `npx securenow whoami`).
|
|
147
|
-
2. Confirm `SECURENOW_LOGGING_ENABLED` is not set to `0`.
|
|
148
|
-
3. Run `npx securenow doctor` — it probes the full pipeline and reports the failure mode.
|
|
149
|
-
4. Enable verbose output: `OTEL_LOG_LEVEL=debug`.
|
|
150
|
-
|
|
151
|
-
**Console logs not forwarding?**
|
|
152
|
-
|
|
153
|
-
Load `securenow/register` before other app code so logging hooks run first:
|
|
154
|
-
|
|
155
|
-
```javascript
|
|
156
|
-
// ✅ Correct
|
|
157
|
-
require('securenow/register'); // First
|
|
158
|
-
|
|
159
|
-
// ❌ Wrong
|
|
160
|
-
require('express');
|
|
161
|
-
require('securenow/register'); // Too late!
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
---
|
|
165
|
-
|
|
166
|
-
## What Gets Logged?
|
|
167
|
-
|
|
168
|
-
All standard console methods:
|
|
169
|
-
|
|
170
|
-
- `console.log()` → INFO
|
|
171
|
-
- `console.info()` → INFO
|
|
172
|
-
- `console.warn()` → WARN
|
|
173
|
-
- `console.error()` → ERROR
|
|
174
|
-
- `console.debug()` → DEBUG
|
|
175
|
-
|
|
176
|
-
**Structured logging example:**
|
|
177
|
-
|
|
178
|
-
```javascript
|
|
179
|
-
console.log('User logged in', {
|
|
180
|
-
userId: 123,
|
|
181
|
-
email: 'user@example.com',
|
|
182
|
-
ip: '192.168.1.1'
|
|
183
|
-
});
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
This creates a log with attributes you can filter/search in SecureNow!
|
|
187
|
-
|
|
188
|
-
---
|
|
189
|
-
|
|
190
|
-
## Advanced Usage
|
|
191
|
-
|
|
192
|
-
**Direct Logger API:**
|
|
193
|
-
|
|
194
|
-
```javascript
|
|
195
|
-
const { getLogger } = require('securenow/tracing');
|
|
196
|
-
const logger = getLogger('my-module', '1.0.0');
|
|
197
|
-
|
|
198
|
-
logger.emit({
|
|
199
|
-
severityNumber: 9,
|
|
200
|
-
severityText: 'INFO',
|
|
201
|
-
body: 'Custom log message',
|
|
202
|
-
attributes: {
|
|
203
|
-
customField: 'value',
|
|
204
|
-
},
|
|
205
|
-
});
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
---
|
|
209
|
-
|
|
210
|
-
## Next Steps
|
|
211
|
-
|
|
212
|
-
- [Complete Logging Guide](./LOGGING-GUIDE.md) - All features and options
|
|
213
|
-
- [SecureNow](https://securenow.ai/)
|
|
214
|
-
- [Documentation](./INDEX.md)
|
|
215
|
-
- [Combine with Tracing](../README.md) - Full observability
|
|
216
|
-
|
|
217
|
-
---
|
|
218
|
-
|
|
219
|
-
**That's it!** 🎉 Your app is now sending logs to SecureNow.
|
|
220
|
-
|
|
221
|
-
Need help? Check the [full documentation](./LOGGING-GUIDE.md) or open an issue.
|
package/docs/MCP-GUIDE.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# SecureNow MCP Guide
|
|
2
|
-
|
|
3
|
-
SecureNow ships an MCP server for agent clients such as Codex and Claude.
|
|
4
|
-
|
|
5
|
-
## Local stdio MCP
|
|
6
|
-
|
|
7
|
-
Use this when the agent is running on the same machine as your project:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npx securenow login
|
|
11
|
-
codex mcp add securenow -- npx securenow mcp
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
You can also run the server directly:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npx -p securenow securenow-mcp
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
The local MCP server reads the same project-local `.securenow/credentials.json`
|
|
21
|
-
as the CLI and SDK. No production deployment is required.
|
|
22
|
-
|
|
23
|
-
## Environment Scope
|
|
24
|
-
|
|
25
|
-
MCP tools use the same environment model as the CLI and UI: one app key across
|
|
26
|
-
local, preview, staging, and production, separated by
|
|
27
|
-
`config.runtime.deploymentEnvironment`. Investigation tools default to
|
|
28
|
-
`production`. Pass `environment: "local"`, `"staging"`, `"preview"`, or
|
|
29
|
-
`"all"` only when that scope is intentional.
|
|
30
|
-
|
|
31
|
-
## Hosted MCP
|
|
32
|
-
|
|
33
|
-
For hosted clients, expose the secured API endpoint:
|
|
34
|
-
|
|
35
|
-
```text
|
|
36
|
-
https://api.securenow.ai/mcp
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
The hosted endpoint must be authenticated with `Authorization: Bearer ...`.
|
|
40
|
-
It accepts SecureNow JWT sessions and `snk_live_...` API keys through the same
|
|
41
|
-
API auth path used by the rest of SecureNow.
|
|
42
|
-
|
|
43
|
-
## Security Model
|
|
44
|
-
|
|
45
|
-
- Read tools require the matching `*:read` scope.
|
|
46
|
-
- Write tools require the matching `*:write` scope or `applications:write` for
|
|
47
|
-
app firewall settings.
|
|
48
|
-
- Write tools also require `confirm: true` and a non-empty `reason`.
|
|
49
|
-
- Full tokens and API keys are never returned by tools or resources.
|
|
50
|
-
- Hosted MCP validates browser origins and rate-limits requests.
|
|
51
|
-
- Tool calls are proxied through existing API routes so tenant isolation and
|
|
52
|
-
scope enforcement stay centralized.
|
|
53
|
-
|
|
54
|
-
## Tools
|
|
55
|
-
|
|
56
|
-
The MCP exposes applications, traces, logs, firewall, IP intelligence,
|
|
57
|
-
forensics, notifications, blocklist, allowlist, trusted IPs, analytics, bundled
|
|
58
|
-
docs resources, and setup prompts.
|
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
# Next.js Body Capture - Choosing the Right Approach
|
|
2
|
-
|
|
3
|
-
## 🎯 Two Approaches Available
|
|
4
|
-
|
|
5
|
-
SecureNow offers two ways to capture request bodies in Next.js:
|
|
6
|
-
|
|
7
|
-
1. **Wrapper Approach** (Recommended ✅)
|
|
8
|
-
2. **Middleware Approach** (Use with caution ⚠️)
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
## ✅ Wrapper Approach (RECOMMENDED)
|
|
13
|
-
|
|
14
|
-
### How It Works
|
|
15
|
-
|
|
16
|
-
Wrap individual API route handlers to capture bodies **inside the handler**:
|
|
17
|
-
|
|
18
|
-
```typescript
|
|
19
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
20
|
-
|
|
21
|
-
export const POST = withSecureNow(async (request: Request) => {
|
|
22
|
-
const body = await request.json();
|
|
23
|
-
return Response.json({ success: true });
|
|
24
|
-
});
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### ✅ Pros
|
|
28
|
-
|
|
29
|
-
- **Zero middleware conflicts** - Doesn't interfere with NextAuth or other middleware
|
|
30
|
-
- **Never blocks requests** - Runs after routing is complete
|
|
31
|
-
- **Per-route control** - Wrap only the routes you need
|
|
32
|
-
- **Non-invasive** - Your middleware stays unchanged
|
|
33
|
-
- **Safe** - Runs inside your handler, can't prevent handler execution
|
|
34
|
-
- **Background capture** - Doesn't delay responses
|
|
35
|
-
|
|
36
|
-
### ❌ Cons
|
|
37
|
-
|
|
38
|
-
- Requires wrapping each route individually (but only the ones you want!)
|
|
39
|
-
- Slightly more verbose (one extra line per route)
|
|
40
|
-
|
|
41
|
-
### When to Use
|
|
42
|
-
|
|
43
|
-
- ✅ You have NextAuth or other middleware
|
|
44
|
-
- ✅ You want zero conflicts
|
|
45
|
-
- ✅ You want fine-grained control
|
|
46
|
-
- ✅ You prioritize reliability
|
|
47
|
-
- ✅ **This is the recommended approach for most users**
|
|
48
|
-
|
|
49
|
-
### Setup
|
|
50
|
-
|
|
51
|
-
**Step 1:** Enable in .env.local
|
|
52
|
-
```bash
|
|
53
|
-
SECURENOW_CAPTURE_BODY=1
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
**Step 2:** Wrap your API routes
|
|
57
|
-
```typescript
|
|
58
|
-
// app/api/login/route.ts
|
|
59
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
60
|
-
|
|
61
|
-
export const POST = withSecureNow(async (request: Request) => {
|
|
62
|
-
const body = await request.json();
|
|
63
|
-
// Your logic...
|
|
64
|
-
return Response.json({ success: true });
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
**That's it!** No middleware.ts needed.
|
|
69
|
-
|
|
70
|
-
📚 **Full guide:** See `NEXTJS-WRAPPER-APPROACH.md`
|
|
71
|
-
|
|
72
|
-
---
|
|
73
|
-
|
|
74
|
-
## ⚠️ Middleware Approach (Use with Caution)
|
|
75
|
-
|
|
76
|
-
### How It Works
|
|
77
|
-
|
|
78
|
-
Export SecureNow's middleware to capture bodies **before your handlers**:
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
// middleware.ts
|
|
82
|
-
export { middleware } from 'securenow/nextjs-middleware';
|
|
83
|
-
|
|
84
|
-
export const config = {
|
|
85
|
-
matcher: '/api/:path*',
|
|
86
|
-
};
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### ✅ Pros
|
|
90
|
-
|
|
91
|
-
- One-time setup (no per-route wrapping)
|
|
92
|
-
- Applies to all routes automatically
|
|
93
|
-
|
|
94
|
-
### ❌ Cons
|
|
95
|
-
|
|
96
|
-
- **Can conflict with NextAuth** and other middleware
|
|
97
|
-
- **May block requests** from reaching handlers
|
|
98
|
-
- **All-or-nothing** - applies to all matched routes
|
|
99
|
-
- **Runs before routing** - can interfere with request flow
|
|
100
|
-
- **May cause "Response body disturbed or locked" errors**
|
|
101
|
-
|
|
102
|
-
### When to Use
|
|
103
|
-
|
|
104
|
-
- You have no other middleware
|
|
105
|
-
- You want to capture ALL routes
|
|
106
|
-
- You're okay with potential conflicts
|
|
107
|
-
- **Not recommended if you use NextAuth or have complex middleware**
|
|
108
|
-
|
|
109
|
-
### Known Issues
|
|
110
|
-
|
|
111
|
-
**Conflicts with NextAuth:**
|
|
112
|
-
```typescript
|
|
113
|
-
// ❌ This can cause conflicts
|
|
114
|
-
export { middleware } from 'securenow/nextjs-middleware';
|
|
115
|
-
|
|
116
|
-
// If you also use NextAuth, you'll need complex middleware composition
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
**Solution:** Use the wrapper approach instead!
|
|
120
|
-
|
|
121
|
-
📚 **Full guide:** See `NEXTJS-BODY-CAPTURE.md` (but consider wrapper approach first)
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
## 🔄 Comparison Table
|
|
126
|
-
|
|
127
|
-
| Feature | Wrapper Approach | Middleware Approach |
|
|
128
|
-
|---------|-----------------|---------------------|
|
|
129
|
-
| **Setup complexity** | Per-route wrapping | One-time setup |
|
|
130
|
-
| **Middleware conflicts** | ✅ None | ⚠️ Possible |
|
|
131
|
-
| **NextAuth compatibility** | ✅ Perfect | ❌ Can conflict |
|
|
132
|
-
| **Request blocking** | ✅ Never | ⚠️ Possible |
|
|
133
|
-
| **Control granularity** | ✅ Per-route | ❌ All-or-nothing |
|
|
134
|
-
| **Error impact** | ✅ Isolated | ⚠️ Can block all routes |
|
|
135
|
-
| **Recommended for** | ✅ Most users | ⚠️ Simple apps only |
|
|
136
|
-
|
|
137
|
-
---
|
|
138
|
-
|
|
139
|
-
## 🎯 Our Recommendation
|
|
140
|
-
|
|
141
|
-
### For Most Users (Especially with NextAuth)
|
|
142
|
-
|
|
143
|
-
**Use the Wrapper Approach:**
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
// middleware.ts - Your auth logic (no securenow!)
|
|
147
|
-
export async function middleware(request) {
|
|
148
|
-
// Just your middleware - no securenow imports
|
|
149
|
-
const token = await getToken({ req: request });
|
|
150
|
-
if (!token) return NextResponse.redirect('/login');
|
|
151
|
-
return NextResponse.next();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// app/api/protected/route.ts - Wrap individual routes
|
|
155
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
156
|
-
|
|
157
|
-
export const POST = withSecureNow(async (request: Request) => {
|
|
158
|
-
// Your handler - no conflicts!
|
|
159
|
-
const body = await request.json();
|
|
160
|
-
return Response.json({ success: true });
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
**Why?**
|
|
165
|
-
- ✅ Zero conflicts
|
|
166
|
-
- ✅ Your middleware stays clean
|
|
167
|
-
- ✅ Per-route control
|
|
168
|
-
- ✅ Never blocks requests
|
|
169
|
-
|
|
170
|
-
### For Simple Apps (No Other Middleware)
|
|
171
|
-
|
|
172
|
-
**You can use Middleware Approach:**
|
|
173
|
-
|
|
174
|
-
```typescript
|
|
175
|
-
// middleware.ts
|
|
176
|
-
export { middleware } from 'securenow/nextjs-middleware';
|
|
177
|
-
|
|
178
|
-
export const config = {
|
|
179
|
-
matcher: '/api/:path*',
|
|
180
|
-
};
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
**Why?**
|
|
184
|
-
- ✅ One-time setup
|
|
185
|
-
- ✅ Auto-applies to all routes
|
|
186
|
-
- ⚠️ But be aware of potential conflicts if you add other middleware later
|
|
187
|
-
|
|
188
|
-
---
|
|
189
|
-
|
|
190
|
-
## 📊 Real-World Scenarios
|
|
191
|
-
|
|
192
|
-
### Scenario 1: NextAuth + SecureNow
|
|
193
|
-
|
|
194
|
-
**❌ Middleware Approach - Can Cause Issues:**
|
|
195
|
-
```typescript
|
|
196
|
-
// middleware.ts
|
|
197
|
-
import { getToken } from 'next-auth/jwt';
|
|
198
|
-
import { middleware as securenowMiddleware } from 'securenow/nextjs-middleware';
|
|
199
|
-
|
|
200
|
-
export async function middleware(request) {
|
|
201
|
-
// Complex composition needed - prone to conflicts
|
|
202
|
-
await securenowMiddleware(request);
|
|
203
|
-
const token = await getToken({ req: request });
|
|
204
|
-
// ...
|
|
205
|
-
}
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
**✅ Wrapper Approach - Clean & Safe:**
|
|
209
|
-
```typescript
|
|
210
|
-
// middleware.ts - Just NextAuth
|
|
211
|
-
export async function middleware(request) {
|
|
212
|
-
const token = await getToken({ req: request });
|
|
213
|
-
if (!token) return NextResponse.redirect('/login');
|
|
214
|
-
return NextResponse.next();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// app/api/*/route.ts - Add SecureNow per route
|
|
218
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
219
|
-
export const POST = withSecureNow(handler);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### Scenario 2: Rate Limiting + SecureNow
|
|
223
|
-
|
|
224
|
-
**❌ Middleware Approach:**
|
|
225
|
-
```typescript
|
|
226
|
-
// Multiple middleware = conflicts
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
**✅ Wrapper Approach:**
|
|
230
|
-
```typescript
|
|
231
|
-
// middleware.ts - Just rate limiting
|
|
232
|
-
export async function middleware(request) {
|
|
233
|
-
await checkRateLimit(request);
|
|
234
|
-
return NextResponse.next();
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// routes - Add SecureNow
|
|
238
|
-
export const POST = withSecureNow(handler);
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### Scenario 3: Simple API (No Other Middleware)
|
|
242
|
-
|
|
243
|
-
**✅ Either Approach Works:**
|
|
244
|
-
|
|
245
|
-
**Option A - Wrapper:**
|
|
246
|
-
```typescript
|
|
247
|
-
export const POST = withSecureNow(handler);
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
**Option B - Middleware:**
|
|
251
|
-
```typescript
|
|
252
|
-
export { middleware } from 'securenow/nextjs-middleware';
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
Both work fine when you have no other middleware!
|
|
256
|
-
|
|
257
|
-
---
|
|
258
|
-
|
|
259
|
-
## 🚀 Migration Guide
|
|
260
|
-
|
|
261
|
-
### From Middleware to Wrapper
|
|
262
|
-
|
|
263
|
-
**Before:**
|
|
264
|
-
```typescript
|
|
265
|
-
// middleware.ts
|
|
266
|
-
export { middleware } from 'securenow/nextjs-middleware';
|
|
267
|
-
export const config = { matcher: '/api/:path*' };
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
**After:**
|
|
271
|
-
```typescript
|
|
272
|
-
// middleware.ts - Delete securenow import!
|
|
273
|
-
// (Keep your other middleware like NextAuth)
|
|
274
|
-
|
|
275
|
-
// app/api/login/route.ts
|
|
276
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
277
|
-
export const POST = withSecureNow(async (request) => {
|
|
278
|
-
// Your handler
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
// app/api/register/route.ts
|
|
282
|
-
import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
283
|
-
export const POST = withSecureNow(async (request) => {
|
|
284
|
-
// Your handler
|
|
285
|
-
});
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
**Result:** No more conflicts! 🎉
|
|
289
|
-
|
|
290
|
-
---
|
|
291
|
-
|
|
292
|
-
## ✅ Summary
|
|
293
|
-
|
|
294
|
-
### Quick Decision Guide
|
|
295
|
-
|
|
296
|
-
**Do you have NextAuth or other middleware?**
|
|
297
|
-
- Yes → Use **Wrapper Approach** ✅
|
|
298
|
-
- No → Either works, but wrapper is safer
|
|
299
|
-
|
|
300
|
-
**Do you want per-route control?**
|
|
301
|
-
- Yes → Use **Wrapper Approach** ✅
|
|
302
|
-
- No → Middleware works
|
|
303
|
-
|
|
304
|
-
**Do you prioritize zero conflicts?**
|
|
305
|
-
- Yes → Use **Wrapper Approach** ✅
|
|
306
|
-
- Not critical → Middleware works
|
|
307
|
-
|
|
308
|
-
**Do you experience "Response body disturbed" errors?**
|
|
309
|
-
- Yes → Switch to **Wrapper Approach** ✅
|
|
310
|
-
|
|
311
|
-
### Bottom Line
|
|
312
|
-
|
|
313
|
-
**For 90% of users:** Use the **Wrapper Approach**
|
|
314
|
-
|
|
315
|
-
It's safer, more flexible, and conflict-free!
|
|
316
|
-
|
|
317
|
-
📚 **Full documentation:**
|
|
318
|
-
- Wrapper: `NEXTJS-WRAPPER-APPROACH.md`
|
|
319
|
-
- Middleware: `NEXTJS-BODY-CAPTURE.md`
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|