@productcraft/auth 1.0.0 → 1.1.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.
- package/README.md +33 -2
- package/api.md +547 -230
- package/dist/index.cjs +868 -231
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37246 -0
- package/dist/index.d.ts +1058 -263
- package/dist/index.js +868 -231
- package/dist/index.js.map +1 -1
- package/dist/testing.d.cts +32 -0
- package/manifest.json +210 -210
- package/package.json +25 -8
package/README.md
CHANGED
|
@@ -16,13 +16,34 @@ Node 20.3 or later (ESM and CommonJS), Bun, Deno and edge runtimes. Or install t
|
|
|
16
16
|
|
|
17
17
|
| Scope | Who calls it | Credential |
|
|
18
18
|
|---|---|---|
|
|
19
|
-
| `auth.consumer(slug)`: sign-in, sign-up, refresh, `me`, MFA, passkeys, tenants | Your app's backend or browser, for its end users |
|
|
19
|
+
| `auth.consumer(slug)`: sign-in, sign-up, refresh, `me`, MFA, passkeys, tenants | Your app's backend or browser, for its end users | Depends on the route (below) |
|
|
20
20
|
| `auth.admin.app(appId)` (alias `auth.app(appId)`): end users, roles, keys, config, webhooks | Your backend or tooling | A workspace API key (`PRODUCTCRAFT_API_KEY`) or a platform session |
|
|
21
21
|
|
|
22
|
-
`auth.consumer()` defaults its slug to `PRODUCTCRAFT_AUTH_APP` and never reads `PRODUCTCRAFT_API_KEY`: routes that act for an end user must not carry your workspace key by accident.
|
|
22
|
+
`auth.consumer()` defaults its slug to `PRODUCTCRAFT_AUTH_APP` and never reads `PRODUCTCRAFT_API_KEY`: routes that act for an end user must not carry your workspace key by accident. Pass the credential a route needs explicitly. Each method's reference entry in [api.md](./api.md#credentials) names it, and a wrong one answers 401:
|
|
23
|
+
|
|
24
|
+
| `auth.consumer(slug)` routes | Credential | How to pass it |
|
|
25
|
+
|---|---|---|
|
|
26
|
+
| `signin`, `refresh`, `verifyEmail`, `resetPassword`, `passkey.*`, `mfa.*`, `oauth.token`, `verify.*`, `wellKnown.*` | None | `auth.consumer(slug)` |
|
|
27
|
+
| `me.*`, `switchTenant`, `tenantApiKeys` create/list/rotate/delete | The end user's access token | `new Auth({ auth: { type: "bearer", token: accessToken } }).consumer(slug)` |
|
|
28
|
+
| `users.*`, `roles.*`, `permissions.*` (your backend managing the app's users) | An app API key (`hdk_live_…`, from `admin.app(id).apiKeys.create`), an M2M token, or an end user whose role holds the permission. **Not** a workspace key | `new Auth({ auth: { type: "bearer", token: process.env.AUTH_APP_API_KEY } }).consumer(slug)` |
|
|
29
|
+
| `tenants.*`, `users.factors.*`, `tenantApiKeys.introspect`/`status`/`config`, `requestVerification`, `sendVerificationEmail`, `requestPasswordReset`, `sendPasswordResetEmail`, `signup` (when the app requires it) | A workspace API key | `new Auth({ auth: { type: "bearer", token: process.env.PRODUCTCRAFT_API_KEY } }).consumer(slug)` |
|
|
30
|
+
|
|
31
|
+
**`consumer(slug).users` or `admin.app(id).endUsers`?** Both manage the same end users. Use `admin.app(id).endUsers` (and `.roles`, `.permissions`, `.tenants`) from your own tooling with your workspace API key: it covers more (search, export, import, bulk changes, sessions). Use `consumer(slug).users` from your product's backend when it should hold only an app-scoped credential (an app API key or M2M token), never your workspace key.
|
|
23
32
|
|
|
24
33
|
## Quick start
|
|
25
34
|
|
|
35
|
+
A new app has sign-up turned off (`signup` answers 403 "Signup is disabled for this app"). Turn it on once, with your workspace API key:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { Auth } from "@productcraft/auth";
|
|
39
|
+
|
|
40
|
+
const auth = new Auth(); // reads PRODUCTCRAFT_API_KEY
|
|
41
|
+
const created = await auth.apps.create({ slug: "my-app", display_name: "My App", workspace_id: "5d1e2f3a-4b5c-4d6e-8f9a-0b1c2d3e4f5a" });
|
|
42
|
+
await auth.admin.app(created.id).authConfig.update({ signup_enabled: true });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then sign end users up and in:
|
|
46
|
+
|
|
26
47
|
```ts
|
|
27
48
|
import { Auth, AuthenticationError } from "@productcraft/auth";
|
|
28
49
|
|
|
@@ -218,6 +239,16 @@ app.post("/webhooks", async (c) => {
|
|
|
218
239
|
});
|
|
219
240
|
```
|
|
220
241
|
|
|
242
|
+
Testing a handler: `webhooks.sign()` signs an event the way ProductCraft does, so a test needs no live delivery and no hand-rolled HMAC.
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
import { Auth } from "@productcraft/auth";
|
|
246
|
+
const auth = new Auth({ webhookSecret: "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw" });
|
|
247
|
+
const { payload, headers } = await auth.webhooks.sign({ payload: { id: "evt_test_1", type: "app.slug_changed", created_at: new Date().toISOString(), data: {} } });
|
|
248
|
+
const event = await auth.webhooks.unwrap({ payload, headers });
|
|
249
|
+
console.log(event.type);
|
|
250
|
+
```
|
|
251
|
+
|
|
221
252
|
## Raw responses
|
|
222
253
|
|
|
223
254
|
Every returned object has a non-enumerable `lastResponse` (`requestId`, `status`, `headers`). `.withResponse()` adds the `Response` and parsed rate-limit headers.
|