@anomira/node-sdk 0.2.0 → 0.2.2
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/README.md +25 -1
- package/dist/index.cjs +1172 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -6
- package/dist/index.d.ts +92 -6
- package/dist/index.js +1172 -18
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,7 +38,31 @@ ANOMIRA_APP_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
|
38
38
|
|
|
39
39
|
## Express
|
|
40
40
|
|
|
41
|
-
The middleware goes **after** `express.json()` and **
|
|
41
|
+
The middleware goes **after** `express.json()` and **after your auth middleware** so it can read the authenticated user, and **before** your routes.
|
|
42
|
+
|
|
43
|
+
### How user identity is captured automatically
|
|
44
|
+
|
|
45
|
+
The SDK automatically extracts the authenticated user ID from the request — no configuration required. It tries the following sources in order:
|
|
46
|
+
|
|
47
|
+
| Priority | Source | Set by |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| 1 | `req.user.id` / `.sub` / `.userId` / `._id` / `.uid` | Passport.js, express-jwt v6, Firebase Admin, @fastify/jwt |
|
|
50
|
+
| 2 | `req.auth.sub` / `.id` / `.userId` | express-jwt v7+ |
|
|
51
|
+
| 3 | `req.userId` / `req.accountId` / `req.customerId` | Custom middleware |
|
|
52
|
+
| 4 | `req.session.userId` / `req.session.user.id` | express-session |
|
|
53
|
+
| 5 | JWT decode from `Authorization: Bearer ...` | **Automatic fallback — works even without explicit auth middleware** |
|
|
54
|
+
|
|
55
|
+
Tier 5 is the safety net: if your auth middleware hasn't set `req.user` yet, the SDK decodes the JWT token in the `Authorization` header itself (without verifying the signature — it only reads the `sub` / `id` claim). This means user tracking works even if middleware registration order is incorrect.
|
|
56
|
+
|
|
57
|
+
**The only requirement:** if you use a custom auth pattern not listed above, pass `getUserId`:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const anomira = new Anomira({
|
|
61
|
+
apiKey: process.env.ANOMIRA_API_KEY!,
|
|
62
|
+
appId: process.env.ANOMIRA_APP_ID!,
|
|
63
|
+
getUserId: (req) => (req as any).myCustomField?.userId,
|
|
64
|
+
});
|
|
65
|
+
```
|
|
42
66
|
|
|
43
67
|
```ts
|
|
44
68
|
// app.ts (TypeScript)
|