@vvedantb/server 0.16.0 → 0.17.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/CHANGELOG.md +6 -0
- package/dist/convex/clerkUserAuth.d.ts +1 -0
- package/dist/convex/clerkUserAuth.js +8 -0
- package/docs/convex.md +3 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#255](https://github.com/vvedantb/packages/pull/255) [`c7f99a2`](https://github.com/vvedantb/packages/commit/c7f99a2b2818ed5c0d0b2aba256e6d6b8e0033c2) Thanks [@vvedantb](https://github.com/vvedantb)! - Add `requireCurrentUser` to `createClerkUserAuth`. It resolves the caller's row from the Clerk identity and throws `new Error(message)` (default the config's `unauthenticatedMessage`) where `getCurrentUser` would return `null`, returning the row itself rather than its id. This is the row-returning counterpart to the existing `requireCurrentUserId`, for handlers that need the user document and otherwise hand-write the identity lookup and null check.
|
|
8
|
+
|
|
3
9
|
## 0.16.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -100,6 +100,7 @@ export declare function createClerkUserAuth<DataModel extends ClerkUserDataModel
|
|
|
100
100
|
getCurrentUser: <Range>(ctx: ClerkUserQueryCtx<Table, Index, Field, DataModel[Table]["document"], Range>) => Promise<DataModel[Table]["document"] | null>;
|
|
101
101
|
getCurrentUserId: <Range>(ctx: ClerkUserQueryCtx<Table, Index, Field, DataModel[Table]["document"], Range>) => Promise<DataModel[Table]["document"]["_id"] | null>;
|
|
102
102
|
requireCurrentUserId: <Range>(ctx: ClerkUserQueryCtx<Table, Index, Field, DataModel[Table]["document"], Range>, message?: string) => Promise<DataModel[Table]["document"]["_id"]>;
|
|
103
|
+
requireCurrentUser: <Range>(ctx: ClerkUserQueryCtx<Table, Index, Field, DataModel[Table]["document"], Range>, message?: string) => Promise<DataModel[Table]["document"]>;
|
|
103
104
|
isCurrentUserAdmin: <Range>(ctx: ClerkUserQueryCtx<Table, Index, Field, DataModel[Table]["document"], Range>) => Promise<boolean>;
|
|
104
105
|
ensureUser: <Range, Identity extends ClerkUserIdentityLike>(ctx: ClerkUserMutationCtx<Table, Index, Field, DataModel[Table]["document"], Range, Identity>, buildFields: (identity: Identity) => ClerkUserInsert<DataModel[Table]["document"]>, options?: EnsureClerkUserOptions<DataModel[Table]["document"], Identity>) => Promise<EnsureClerkUserResult<DataModel[Table]["document"]["_id"]>>;
|
|
105
106
|
};
|
|
@@ -46,6 +46,13 @@ export function createClerkUserAuth() {
|
|
|
46
46
|
throw new Error(message);
|
|
47
47
|
return userId;
|
|
48
48
|
}
|
|
49
|
+
/** `getCurrentUser`, throwing `new Error(message)` where that returns `null`. */
|
|
50
|
+
async function requireCurrentUser(ctx, message = unauthenticatedMessage) {
|
|
51
|
+
const user = await getCurrentUser(ctx);
|
|
52
|
+
if (user === null)
|
|
53
|
+
throw new Error(message);
|
|
54
|
+
return user;
|
|
55
|
+
}
|
|
49
56
|
async function isCurrentUserAdmin(ctx) {
|
|
50
57
|
const adminField = config.adminField;
|
|
51
58
|
if (adminField === undefined) {
|
|
@@ -82,6 +89,7 @@ export function createClerkUserAuth() {
|
|
|
82
89
|
getCurrentUser,
|
|
83
90
|
getCurrentUserId,
|
|
84
91
|
requireCurrentUserId,
|
|
92
|
+
requireCurrentUser,
|
|
85
93
|
isCurrentUserAdmin,
|
|
86
94
|
ensureUser,
|
|
87
95
|
};
|
package/docs/convex.md
CHANGED
|
@@ -70,7 +70,7 @@ expectMcpToolCount(toolNames, 2);
|
|
|
70
70
|
| `jsonResponse(data, status, extraHeaders?)` | `JSON.stringify(data)` with `Content-Type: application/json`; `extraHeaders` spread after it. |
|
|
71
71
|
| `createOriginAllowlistCors(allowedOrigins)` | `{ headers(origin), preflightHeaders(origin) }` for credentialed browser CORS on a fixed origin list. |
|
|
72
72
|
| `verifyClerkAccessToken(clerk, token, options?)`, `consoleClerkVerifyLogger` | Clerk bearer verification core: `{ userId, tokenType, auth }` or `null`. See below. |
|
|
73
|
-
| `createClerkUserAuth<DataModel>()(config)` | Clerk-user lookups for queries and mutations: `getCurrentUserId`, `requireCurrentUserId`, `getCurrentUser`, `isCurrentUserAdmin`, `getUserByClerkId`, `ensureUser`. See below. |
|
|
73
|
+
| `createClerkUserAuth<DataModel>()(config)` | Clerk-user lookups for queries and mutations: `getCurrentUserId`, `requireCurrentUserId`, `getCurrentUser`, `requireCurrentUser`, `isCurrentUserAdmin`, `getUserByClerkId`, `ensureUser`. See below. |
|
|
74
74
|
| `hasVPlan`, `hasVPro`, `parseVProCompUserIds` | V Pro checks: the session `pla` claim, stored plan slugs and a complimentary id list. See [V Pro plan check](#v-pro-plan-check). |
|
|
75
75
|
| `verifyClerkWebhook`, `parseClerkBillingEvent`, `createVProModule<DataModel>()(config)` | Clerk Billing webhook: verify, read the plan change, write it to the users row. See [Clerk Billing webhook](#clerk-billing-webhook-v-pro). |
|
|
76
76
|
| `createAuthFunctions<DataModel, UserId>(config)` | `authQuery`/`authMutation`/`authAction` on convex-helpers, with `ctx.userId` and an optional maintenance gate. See [Auth function builders](#auth-function-builders). |
|
|
@@ -218,6 +218,7 @@ export const ensureUserExists = mutation({
|
|
|
218
218
|
| `getCurrentUser(ctx)` | `null` when there is no identity or `subject` is empty; otherwise `getUserByClerkId(ctx, identity.subject)`. |
|
|
219
219
|
| `getCurrentUserId(ctx)` | `(await getCurrentUser(ctx))?._id ?? null`. |
|
|
220
220
|
| `requireCurrentUserId(ctx, message?)` | `getCurrentUserId`, but throws `new Error(message)` instead of returning `null`. `message` defaults to the config's `unauthenticatedMessage`, default `"Not authenticated"` (the message in about 120 throw sites across the apps). |
|
|
221
|
+
| `requireCurrentUser(ctx, message?)` | `getCurrentUser`, but throws `new Error(message)` instead of returning `null`, and returns the row (not the id). `message` defaults like `requireCurrentUserId`. Use it where a handler needs the row itself, not just its `_id`. |
|
|
221
222
|
| `isCurrentUserAdmin(ctx)` | `user[adminField] === true`; `false` with no identity or no row. Throws if `adminField` is not set. |
|
|
222
223
|
| `ensureUser(ctx, buildFields, options?)` | No identity: throws `options.unauthenticatedMessage` (default the config's). Empty `subject`: throws `options.missingSubjectMessage` (default `"Clerk user ID is required"`). Existing row: `ctx.db.patch(row._id, fields)` when `options.patch(row, identity)` returns fields, then `{ userId, wasCreated: false }`. No row: `ctx.db.insert(table, buildFields(identity))` and `{ userId, wasCreated: true }`. `buildFields` must set the Clerk id field itself. `identity` is your ctx's identity type (Convex `UserIdentity`). |
|
|
223
224
|
|
|
@@ -236,7 +237,7 @@ Stays in the apps: legacy email lookups, migration-table lookups, API-key actors
|
|
|
236
237
|
### Differences between the copies
|
|
237
238
|
|
|
238
239
|
- **Lookup end.** app-a to app-d use `.first()`; app-e uses `.unique()`.
|
|
239
|
-
- **Missing identity.** app-a's `getCurrentUserId` throws `"Not authenticated"` for no identity, empty `subject` and no row; app-b, app-c, app-d and app-e return `null`. The kit has both (`requireCurrentUserId`, `getCurrentUserId`).
|
|
240
|
+
- **Missing identity.** app-a's `getCurrentUserId` throws `"Not authenticated"` for no identity, empty `subject` and no row; app-b, app-c, app-d and app-e return `null`. The kit has both (`requireCurrentUserId`, `getCurrentUserId`), and `requireCurrentUser` for the throwing variant that returns the row.
|
|
240
241
|
- **Empty `subject`.** app-b still tries its email lookup; app-d and app-e return `null`; app-a throws. Convex always sets `subject`, so this is unreachable; the kit returns `null`.
|
|
241
242
|
- **Email fallback.** app-b and app-d look up `by_email` when the Clerk id finds nothing (app-c looks up by email only). Legacy, so it stays in the apps; compose it after `getCurrentUserId`.
|
|
242
243
|
- **Admin flag.** app-b `admin === true`, app-d `isAdmin === true`, app-c `user?.isAdmin || false` (the same for a `boolean | undefined` field). A non-boolean truthy value is not admin in the kit.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvedantb/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared Convex backend helpers, MCP server scaffolding and Node-side MCP OAuth client helpers from one root import: query-limit errors, Clerk auth and token verification, an MCP bearer-token cache, Expo push token storage, batched send and receipts, Clerk-user lookups and ensure-user for Convex functions, V Pro plan checks and Clerk Billing webhook helpers, bearer tokens, env, CORS, JSON responses, MCP route registration, a bearer-auth MCP HTTP handler, OAuth metadata, a JSON-RPC dispatcher, explorer tools, WebCrypto primitives, Convex optimistic-update helpers, MCP OAuth client helpers (provider, sign-in, refresh, revocation, loopback redirect server) for desktop apps, a QuickJS code sandbox that bridges script tool calls to host handlers, and a pure artifacts core (Zod contract, state limits and ops, quiz and flashcards pages, a sandboxed document wrapper with CSP and a guest state bridge); and, from the `@vvedantb/server/ai` subpath, Vercel AI Gateway helpers (service tier provider options, cost from provider metadata, error classification).",
|
|
6
6
|
"type": "module",
|