@robelest/convex-auth 0.0.2 → 0.0.3-preview.1

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 (173) hide show
  1. package/dist/bin.cjs +1 -1
  2. package/dist/client/index.d.ts +33 -9
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js +79 -13
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/component/_generated/component.d.ts +48 -0
  7. package/dist/component/_generated/component.d.ts.map +1 -1
  8. package/dist/component/index.d.ts +10 -4
  9. package/dist/component/index.d.ts.map +1 -1
  10. package/dist/component/index.js +8 -3
  11. package/dist/component/index.js.map +1 -1
  12. package/dist/component/public.d.ts +163 -3
  13. package/dist/component/public.d.ts.map +1 -1
  14. package/dist/component/public.js +124 -0
  15. package/dist/component/public.js.map +1 -1
  16. package/dist/component/schema.d.ts +81 -2
  17. package/dist/component/schema.d.ts.map +1 -1
  18. package/dist/component/schema.js +45 -0
  19. package/dist/component/schema.js.map +1 -1
  20. package/dist/providers/anonymous.d.ts +3 -0
  21. package/dist/providers/anonymous.d.ts.map +1 -1
  22. package/dist/providers/anonymous.js +3 -0
  23. package/dist/providers/anonymous.js.map +1 -1
  24. package/dist/providers/credentials.d.ts +3 -0
  25. package/dist/providers/credentials.d.ts.map +1 -1
  26. package/dist/providers/credentials.js +3 -0
  27. package/dist/providers/credentials.js.map +1 -1
  28. package/dist/providers/email.d.ts +3 -0
  29. package/dist/providers/email.d.ts.map +1 -1
  30. package/dist/providers/email.js +3 -0
  31. package/dist/providers/email.js.map +1 -1
  32. package/dist/providers/passkey.d.ts +7 -1
  33. package/dist/providers/passkey.d.ts.map +1 -1
  34. package/dist/providers/passkey.js +7 -1
  35. package/dist/providers/passkey.js.map +1 -1
  36. package/dist/providers/password.d.ts +3 -0
  37. package/dist/providers/password.d.ts.map +1 -1
  38. package/dist/providers/password.js +3 -0
  39. package/dist/providers/password.js.map +1 -1
  40. package/dist/providers/phone.d.ts +3 -0
  41. package/dist/providers/phone.d.ts.map +1 -1
  42. package/dist/providers/phone.js +3 -0
  43. package/dist/providers/phone.js.map +1 -1
  44. package/dist/providers/totp.d.ts +8 -0
  45. package/dist/providers/totp.d.ts.map +1 -1
  46. package/dist/providers/totp.js +8 -0
  47. package/dist/providers/totp.js.map +1 -1
  48. package/dist/server/convex-auth.d.ts +185 -25
  49. package/dist/server/convex-auth.d.ts.map +1 -1
  50. package/dist/server/convex-auth.js +317 -58
  51. package/dist/server/convex-auth.js.map +1 -1
  52. package/dist/server/email-templates.d.ts +18 -0
  53. package/dist/server/email-templates.d.ts.map +1 -0
  54. package/dist/server/email-templates.js +74 -0
  55. package/dist/server/email-templates.js.map +1 -0
  56. package/dist/server/errors.d.ts +146 -0
  57. package/dist/server/errors.d.ts.map +1 -0
  58. package/dist/server/errors.js +176 -0
  59. package/dist/server/errors.js.map +1 -0
  60. package/dist/server/implementation/apiKey.d.ts +74 -0
  61. package/dist/server/implementation/apiKey.d.ts.map +1 -0
  62. package/dist/server/implementation/apiKey.js +139 -0
  63. package/dist/server/implementation/apiKey.js.map +1 -0
  64. package/dist/server/implementation/index.d.ts +151 -14
  65. package/dist/server/implementation/index.d.ts.map +1 -1
  66. package/dist/server/implementation/index.js +216 -24
  67. package/dist/server/implementation/index.js.map +1 -1
  68. package/dist/server/implementation/mutations/createAccountFromCredentials.d.ts.map +1 -1
  69. package/dist/server/implementation/mutations/createAccountFromCredentials.js +2 -1
  70. package/dist/server/implementation/mutations/createAccountFromCredentials.js.map +1 -1
  71. package/dist/server/implementation/mutations/createVerificationCode.d.ts +2 -2
  72. package/dist/server/implementation/mutations/index.d.ts +6 -6
  73. package/dist/server/implementation/mutations/modifyAccount.d.ts.map +1 -1
  74. package/dist/server/implementation/mutations/modifyAccount.js +2 -1
  75. package/dist/server/implementation/mutations/modifyAccount.js.map +1 -1
  76. package/dist/server/implementation/mutations/userOAuth.d.ts.map +1 -1
  77. package/dist/server/implementation/mutations/userOAuth.js +2 -1
  78. package/dist/server/implementation/mutations/userOAuth.js.map +1 -1
  79. package/dist/server/implementation/mutations/verifierSignature.d.ts.map +1 -1
  80. package/dist/server/implementation/mutations/verifierSignature.js +2 -1
  81. package/dist/server/implementation/mutations/verifierSignature.js.map +1 -1
  82. package/dist/server/implementation/passkey.d.ts.map +1 -1
  83. package/dist/server/implementation/passkey.js +28 -29
  84. package/dist/server/implementation/passkey.js.map +1 -1
  85. package/dist/server/implementation/provider.d.ts.map +1 -1
  86. package/dist/server/implementation/provider.js +5 -4
  87. package/dist/server/implementation/provider.js.map +1 -1
  88. package/dist/server/implementation/redirects.d.ts.map +1 -1
  89. package/dist/server/implementation/redirects.js +2 -1
  90. package/dist/server/implementation/redirects.js.map +1 -1
  91. package/dist/server/implementation/refreshTokens.d.ts.map +1 -1
  92. package/dist/server/implementation/refreshTokens.js +2 -1
  93. package/dist/server/implementation/refreshTokens.js.map +1 -1
  94. package/dist/server/implementation/signIn.d.ts.map +1 -1
  95. package/dist/server/implementation/signIn.js +8 -18
  96. package/dist/server/implementation/signIn.js.map +1 -1
  97. package/dist/server/implementation/totp.d.ts.map +1 -1
  98. package/dist/server/implementation/totp.js +16 -17
  99. package/dist/server/implementation/totp.js.map +1 -1
  100. package/dist/server/implementation/users.d.ts.map +1 -1
  101. package/dist/server/implementation/users.js +3 -2
  102. package/dist/server/implementation/users.js.map +1 -1
  103. package/dist/server/index.d.ts +157 -3
  104. package/dist/server/index.d.ts.map +1 -1
  105. package/dist/server/index.js +180 -17
  106. package/dist/server/index.js.map +1 -1
  107. package/dist/server/oauth/authorizationUrl.d.ts.map +1 -1
  108. package/dist/server/oauth/authorizationUrl.js +2 -1
  109. package/dist/server/oauth/authorizationUrl.js.map +1 -1
  110. package/dist/server/oauth/callback.d.ts.map +1 -1
  111. package/dist/server/oauth/callback.js +5 -4
  112. package/dist/server/oauth/callback.js.map +1 -1
  113. package/dist/server/oauth/checks.d.ts.map +1 -1
  114. package/dist/server/oauth/checks.js +2 -1
  115. package/dist/server/oauth/checks.js.map +1 -1
  116. package/dist/server/oauth/convexAuth.d.ts.map +1 -1
  117. package/dist/server/oauth/convexAuth.js +3 -2
  118. package/dist/server/oauth/convexAuth.js.map +1 -1
  119. package/dist/server/provider_utils.d.ts +2 -0
  120. package/dist/server/provider_utils.d.ts.map +1 -1
  121. package/dist/server/types.d.ts +240 -5
  122. package/dist/server/types.d.ts.map +1 -1
  123. package/dist/server/utils.d.ts.map +1 -1
  124. package/dist/server/utils.js +2 -1
  125. package/dist/server/utils.js.map +1 -1
  126. package/dist/server/version.d.ts +2 -0
  127. package/dist/server/version.d.ts.map +1 -0
  128. package/dist/server/version.js +3 -0
  129. package/dist/server/version.js.map +1 -0
  130. package/package.json +7 -2
  131. package/src/cli/index.ts +1 -1
  132. package/src/cli/utils.ts +248 -0
  133. package/src/client/index.ts +105 -15
  134. package/src/component/_generated/component.ts +61 -0
  135. package/src/component/index.ts +11 -2
  136. package/src/component/public.ts +142 -0
  137. package/src/component/schema.ts +52 -0
  138. package/src/providers/anonymous.ts +3 -0
  139. package/src/providers/credentials.ts +3 -0
  140. package/src/providers/email.ts +3 -0
  141. package/src/providers/passkey.ts +8 -1
  142. package/src/providers/password.ts +3 -0
  143. package/src/providers/phone.ts +3 -0
  144. package/src/providers/totp.ts +9 -0
  145. package/src/server/convex-auth.ts +385 -73
  146. package/src/server/email-templates.ts +77 -0
  147. package/src/server/errors.ts +269 -0
  148. package/src/server/implementation/apiKey.ts +186 -0
  149. package/src/server/implementation/index.ts +288 -28
  150. package/src/server/implementation/mutations/createAccountFromCredentials.ts +2 -1
  151. package/src/server/implementation/mutations/modifyAccount.ts +2 -3
  152. package/src/server/implementation/mutations/userOAuth.ts +2 -1
  153. package/src/server/implementation/mutations/verifierSignature.ts +2 -1
  154. package/src/server/implementation/passkey.ts +33 -35
  155. package/src/server/implementation/provider.ts +5 -8
  156. package/src/server/implementation/redirects.ts +2 -3
  157. package/src/server/implementation/refreshTokens.ts +2 -1
  158. package/src/server/implementation/signIn.ts +9 -18
  159. package/src/server/implementation/totp.ts +18 -21
  160. package/src/server/implementation/users.ts +4 -7
  161. package/src/server/index.ts +240 -37
  162. package/src/server/oauth/authorizationUrl.ts +2 -1
  163. package/src/server/oauth/callback.ts +5 -4
  164. package/src/server/oauth/checks.ts +3 -1
  165. package/src/server/oauth/convexAuth.ts +6 -3
  166. package/src/server/types.ts +254 -5
  167. package/src/server/utils.ts +3 -1
  168. package/src/server/version.ts +2 -0
  169. package/dist/server/portal.d.ts +0 -116
  170. package/dist/server/portal.d.ts.map +0 -1
  171. package/dist/server/portal.js +0 -294
  172. package/dist/server/portal.js.map +0 -1
  173. package/src/server/portal.ts +0 -375
@@ -1,294 +0,0 @@
1
- import { queryGeneric, mutationGeneric, internalMutationGeneric, } from "convex/server";
2
- import { v } from "convex/values";
3
- import { registerStaticRoutes } from "@convex-dev/self-hosting";
4
- // ============================================================================
5
- // Helpers
6
- // ============================================================================
7
- /**
8
- * Check if the authenticated user is a portal admin.
9
- * Portal admins are identified by having an accepted invite
10
- * with `role: "portalAdmin"`.
11
- */
12
- async function requirePortalAdmin(ctx, authComponent, userId) {
13
- const invites = await ctx.runQuery(authComponent.public.inviteList, {
14
- status: "accepted",
15
- });
16
- const isAdmin = invites.some((invite) => invite.role === "portalAdmin" && invite.acceptedByUserId === userId);
17
- if (!isAdmin) {
18
- throw new Error("Not authorized: portal admin access required");
19
- }
20
- }
21
- // ============================================================================
22
- // Portal() factory
23
- // ============================================================================
24
- /**
25
- * Configure the Convex Auth Portal. Returns all the functions needed to
26
- * serve the portal admin UI, manage invite links, and query auth data.
27
- *
28
- * The portal dogfoods the same `Auth()` instance as your app. Portal admins
29
- * sign in via email magic link and are identified by accepted invites with
30
- * `role: "portalAdmin"`.
31
- *
32
- * ```ts filename="convex/portal.ts"
33
- * import { Portal } from "@robelest/convex-auth/component";
34
- * import { auth } from "./auth";
35
- * import { components } from "./_generated/api";
36
- *
37
- * export const {
38
- * hosting, getCurrentDeployment,
39
- * portalQuery, portalMutation,
40
- * validateInvite, acceptInvite, createPortalInvite,
41
- * portal,
42
- * } = Portal(components.auth, components.selfHosting, auth);
43
- * ```
44
- *
45
- * ## Setup
46
- *
47
- * 1. Configure an email provider in your `Auth()` config (e.g. Resend).
48
- * 2. Generate an admin invite link:
49
- * `npx @robelest/convex-auth portal link [--prod]`
50
- * 3. Visit the link, enter your email, click the magic link, and you're in.
51
- *
52
- * The portal URL is auto-derived from `CONVEX_SITE_URL` (always set by Convex).
53
- * Override with `options.portalUrl` if you need a custom URL.
54
- */
55
- export function Portal(authComponent, selfHostingComponent, auth, options) {
56
- const portalUrl = options?.portalUrl ??
57
- (process.env.CONVEX_SITE_URL
58
- ? `${process.env.CONVEX_SITE_URL.replace(/\/$/, "")}/portal`
59
- : "/portal");
60
- return {
61
- // ---- Self-hosting: combined internal mutation for CLI ----
62
- /**
63
- * Combined internal mutation for self-hosting operations.
64
- * Used by the CLI (`@robelest/convex-auth portal upload`) to
65
- * upload static assets and manage deployments.
66
- */
67
- hosting: internalMutationGeneric({
68
- args: {
69
- action: v.string(),
70
- path: v.optional(v.string()),
71
- storageId: v.optional(v.string()),
72
- blobId: v.optional(v.string()),
73
- contentType: v.optional(v.string()),
74
- deploymentId: v.optional(v.string()),
75
- currentDeploymentId: v.optional(v.string()),
76
- limit: v.optional(v.number()),
77
- },
78
- handler: async (ctx, args) => {
79
- switch (args.action) {
80
- case "generateUploadUrl": {
81
- return await ctx.storage.generateUploadUrl();
82
- }
83
- case "recordAsset": {
84
- const { oldStorageId, oldBlobId } = await ctx.runMutation(selfHostingComponent.lib.recordAsset, {
85
- path: args.path,
86
- ...(args.storageId ? { storageId: args.storageId } : {}),
87
- ...(args.blobId ? { blobId: args.blobId } : {}),
88
- contentType: args.contentType,
89
- deploymentId: args.deploymentId,
90
- });
91
- if (oldStorageId) {
92
- try {
93
- await ctx.storage.delete(oldStorageId);
94
- }
95
- catch {
96
- // Ignore — old file may have been in different storage
97
- }
98
- }
99
- return oldBlobId ?? null;
100
- }
101
- case "gcOldAssets": {
102
- const { storageIds, blobIds } = await ctx.runMutation(selfHostingComponent.lib.gcOldAssets, { currentDeploymentId: args.currentDeploymentId });
103
- for (const storageId of storageIds) {
104
- try {
105
- await ctx.storage.delete(storageId);
106
- }
107
- catch {
108
- // Ignore
109
- }
110
- }
111
- await ctx.runMutation(selfHostingComponent.lib.setCurrentDeployment, { deploymentId: args.currentDeploymentId });
112
- return { deleted: storageIds.length, blobIds };
113
- }
114
- case "listAssets": {
115
- return await ctx.runQuery(selfHostingComponent.lib.listAssets, {
116
- limit: args.limit,
117
- });
118
- }
119
- default:
120
- throw new Error(`Unknown hosting action: ${args.action}`);
121
- }
122
- },
123
- }),
124
- // ---- Deployment query (public, for client live-reload) ----
125
- getCurrentDeployment: queryGeneric({
126
- args: {},
127
- handler: async (ctx) => {
128
- return await ctx.runQuery(selfHostingComponent.lib.getCurrentDeployment, {});
129
- },
130
- }),
131
- // ---- Invite management ----
132
- /**
133
- * Validate an invite token. Returns the invite if valid and pending,
134
- * or `null` otherwise. Used by the portal UI to check if an invite
135
- * link is valid before showing the registration form.
136
- */
137
- validateInvite: queryGeneric({
138
- args: { tokenHash: v.string() },
139
- handler: async (ctx, { tokenHash }) => {
140
- const invite = await ctx.runQuery(authComponent.public.inviteGetByTokenHash, { tokenHash });
141
- if (!invite || invite.status !== "pending") {
142
- return null;
143
- }
144
- if (invite.expiresTime && invite.expiresTime < Date.now()) {
145
- return null;
146
- }
147
- return { _id: invite._id, role: invite.role };
148
- },
149
- }),
150
- /**
151
- * Accept a portal invite. Must be called by an authenticated user.
152
- * Marks the invite as accepted and records the accepting user's ID.
153
- *
154
- * The portal UI calls this after the user has signed in via magic link
155
- * following an invite link.
156
- */
157
- acceptInvite: mutationGeneric({
158
- args: { tokenHash: v.string() },
159
- handler: async (ctx, { tokenHash }) => {
160
- const userId = await auth.user.require(ctx);
161
- const invite = await ctx.runQuery(authComponent.public.inviteGetByTokenHash, { tokenHash });
162
- if (!invite) {
163
- throw new Error("Invalid invite token");
164
- }
165
- if (invite.status !== "pending") {
166
- throw new Error(`Invite already ${invite.status}`);
167
- }
168
- if (invite.expiresTime && invite.expiresTime < Date.now()) {
169
- throw new Error("Invite has expired");
170
- }
171
- await ctx.runMutation(authComponent.public.inviteAccept, {
172
- inviteId: invite._id,
173
- acceptedByUserId: userId,
174
- });
175
- },
176
- }),
177
- /**
178
- * Create a portal admin invite. Internal mutation called by the CLI
179
- * (`npx @robelest/convex-auth portal link`).
180
- */
181
- createPortalInvite: internalMutationGeneric({
182
- args: { tokenHash: v.string() },
183
- handler: async (ctx, { tokenHash }) => {
184
- await ctx.runMutation(authComponent.public.inviteCreate, {
185
- tokenHash,
186
- role: "portalAdmin",
187
- status: "pending",
188
- });
189
- return { portalUrl };
190
- },
191
- }),
192
- // ---- Portal data query (auth-gated) ----
193
- /**
194
- * Combined portal query for all auth data reads.
195
- * Requires the caller to be an authenticated portal admin.
196
- *
197
- * Actions:
198
- * - `listUsers` — List all users
199
- * - `listSessions` — List all sessions
200
- * - `getUser` — Get a single user by ID (requires `userId`)
201
- * - `getUserSessions` — List sessions for a user (requires `userId`)
202
- * - `getUserAccounts` — List auth accounts for a user (requires `userId`)
203
- * - `isAdmin` — Check if the current user is a portal admin
204
- */
205
- portalQuery: queryGeneric({
206
- args: {
207
- action: v.string(),
208
- userId: v.optional(v.string()),
209
- },
210
- handler: async (ctx, { action, userId }) => {
211
- const currentUserId = await auth.user.require(ctx);
212
- // Allow isAdmin check without admin requirement
213
- if (action === "isAdmin") {
214
- try {
215
- await requirePortalAdmin(ctx, authComponent, currentUserId);
216
- return true;
217
- }
218
- catch {
219
- return false;
220
- }
221
- }
222
- await requirePortalAdmin(ctx, authComponent, currentUserId);
223
- switch (action) {
224
- case "listUsers":
225
- return await ctx.runQuery(authComponent.public.userList);
226
- case "listSessions":
227
- return await ctx.runQuery(authComponent.public.sessionList);
228
- case "getUser":
229
- return await ctx.runQuery(authComponent.public.userGetById, {
230
- userId: userId,
231
- });
232
- case "getUserSessions":
233
- return await ctx.runQuery(authComponent.public.sessionListByUser, {
234
- userId: userId,
235
- });
236
- case "getUserAccounts": {
237
- const accounts = await ctx.runQuery(authComponent.public.accountListByUser, { userId: userId });
238
- // Strip secrets — never send password hashes to the frontend
239
- return accounts.map(({ secret: _, ...rest }) => rest);
240
- }
241
- default:
242
- throw new Error(`Unknown portal query action: ${action}`);
243
- }
244
- },
245
- }),
246
- // ---- Portal mutation (auth-gated) ----
247
- /**
248
- * Combined portal mutation for all auth data writes.
249
- * Requires the caller to be an authenticated portal admin.
250
- *
251
- * Actions:
252
- * - `revokeSession` — Revoke (delete) a session (requires `sessionId`)
253
- */
254
- portalMutation: mutationGeneric({
255
- args: {
256
- action: v.string(),
257
- sessionId: v.optional(v.string()),
258
- },
259
- handler: async (ctx, { action, sessionId }) => {
260
- const currentUserId = await auth.user.require(ctx);
261
- await requirePortalAdmin(ctx, authComponent, currentUserId);
262
- switch (action) {
263
- case "revokeSession":
264
- await ctx.runMutation(authComponent.public.sessionDelete, {
265
- sessionId: sessionId,
266
- });
267
- return;
268
- default:
269
- throw new Error(`Unknown portal mutation action: ${action}`);
270
- }
271
- },
272
- }),
273
- // ---- Portal namespace ----
274
- portal: {
275
- /**
276
- * The URL where the portal is served. Used by the Svelte client
277
- * as the `redirectTo` for magic link sign-in.
278
- */
279
- portalUrl,
280
- /**
281
- * Register HTTP routes that serve the portal static UI.
282
- */
283
- addHttpRoutes: (http, opts) => {
284
- const prefix = opts?.pathPrefix ?? "/portal";
285
- // Static file serving
286
- registerStaticRoutes(http, selfHostingComponent, {
287
- pathPrefix: prefix,
288
- spaFallback: opts?.spaFallback ?? true,
289
- });
290
- },
291
- },
292
- };
293
- }
294
- //# sourceMappingURL=portal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"portal.js","sourceRoot":"","sources":["../../src/server/portal.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,eAAe,EACf,u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,oBAAoB,CAAC,IAAI,EAAE,oBAAoB,EAAE;oBAC/C,UAAU,EAAE,MAAM;oBAClB,WAAW,EAAE,IAAI,EAAE,WAAW,IAAI,IAAI;iBACvC,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -1,375 +0,0 @@
1
- import {
2
- queryGeneric,
3
- mutationGeneric,
4
- internalMutationGeneric,
5
- } from "convex/server";
6
- import type { HttpRouter } from "convex/server";
7
- import { v } from "convex/values";
8
- import type { ComponentApi as AuthComponentApi } from "../component/_generated/component.js";
9
- import { registerStaticRoutes } from "@convex-dev/self-hosting";
10
-
11
- // ============================================================================
12
- // Helpers
13
- // ============================================================================
14
-
15
- /**
16
- * Check if the authenticated user is a portal admin.
17
- * Portal admins are identified by having an accepted invite
18
- * with `role: "portalAdmin"`.
19
- */
20
- async function requirePortalAdmin(
21
- ctx: any,
22
- authComponent: AuthComponentApi,
23
- userId: string,
24
- ): Promise<void> {
25
- const invites = await ctx.runQuery(authComponent.public.inviteList, {
26
- status: "accepted",
27
- });
28
- const isAdmin = invites.some(
29
- (invite: any) =>
30
- invite.role === "portalAdmin" && invite.acceptedByUserId === userId,
31
- );
32
- if (!isAdmin) {
33
- throw new Error("Not authorized: portal admin access required");
34
- }
35
- }
36
-
37
- // ============================================================================
38
- // Portal() factory
39
- // ============================================================================
40
-
41
- /**
42
- * Configure the Convex Auth Portal. Returns all the functions needed to
43
- * serve the portal admin UI, manage invite links, and query auth data.
44
- *
45
- * The portal dogfoods the same `Auth()` instance as your app. Portal admins
46
- * sign in via email magic link and are identified by accepted invites with
47
- * `role: "portalAdmin"`.
48
- *
49
- * ```ts filename="convex/portal.ts"
50
- * import { Portal } from "@robelest/convex-auth/component";
51
- * import { auth } from "./auth";
52
- * import { components } from "./_generated/api";
53
- *
54
- * export const {
55
- * hosting, getCurrentDeployment,
56
- * portalQuery, portalMutation,
57
- * validateInvite, acceptInvite, createPortalInvite,
58
- * portal,
59
- * } = Portal(components.auth, components.selfHosting, auth);
60
- * ```
61
- *
62
- * ## Setup
63
- *
64
- * 1. Configure an email provider in your `Auth()` config (e.g. Resend).
65
- * 2. Generate an admin invite link:
66
- * `npx @robelest/convex-auth portal link [--prod]`
67
- * 3. Visit the link, enter your email, click the magic link, and you're in.
68
- *
69
- * The portal URL is auto-derived from `CONVEX_SITE_URL` (always set by Convex).
70
- * Override with `options.portalUrl` if you need a custom URL.
71
- */
72
- export function Portal(
73
- authComponent: AuthComponentApi,
74
- selfHostingComponent: any,
75
- auth: any,
76
- options?: { portalUrl?: string },
77
- ) {
78
- const portalUrl =
79
- options?.portalUrl ??
80
- (process.env.CONVEX_SITE_URL
81
- ? `${process.env.CONVEX_SITE_URL.replace(/\/$/, "")}/portal`
82
- : "/portal");
83
-
84
- return {
85
- // ---- Self-hosting: combined internal mutation for CLI ----
86
-
87
- /**
88
- * Combined internal mutation for self-hosting operations.
89
- * Used by the CLI (`@robelest/convex-auth portal upload`) to
90
- * upload static assets and manage deployments.
91
- */
92
- hosting: internalMutationGeneric({
93
- args: {
94
- action: v.string(),
95
- path: v.optional(v.string()),
96
- storageId: v.optional(v.string()),
97
- blobId: v.optional(v.string()),
98
- contentType: v.optional(v.string()),
99
- deploymentId: v.optional(v.string()),
100
- currentDeploymentId: v.optional(v.string()),
101
- limit: v.optional(v.number()),
102
- },
103
- handler: async (ctx: any, args: any) => {
104
- switch (args.action) {
105
- case "generateUploadUrl": {
106
- return await ctx.storage.generateUploadUrl();
107
- }
108
-
109
- case "recordAsset": {
110
- const { oldStorageId, oldBlobId } = await ctx.runMutation(
111
- selfHostingComponent.lib.recordAsset,
112
- {
113
- path: args.path,
114
- ...(args.storageId ? { storageId: args.storageId } : {}),
115
- ...(args.blobId ? { blobId: args.blobId } : {}),
116
- contentType: args.contentType,
117
- deploymentId: args.deploymentId,
118
- },
119
- );
120
- if (oldStorageId) {
121
- try {
122
- await ctx.storage.delete(oldStorageId);
123
- } catch {
124
- // Ignore — old file may have been in different storage
125
- }
126
- }
127
- return oldBlobId ?? null;
128
- }
129
-
130
- case "gcOldAssets": {
131
- const { storageIds, blobIds } = await ctx.runMutation(
132
- selfHostingComponent.lib.gcOldAssets,
133
- { currentDeploymentId: args.currentDeploymentId },
134
- );
135
- for (const storageId of storageIds) {
136
- try {
137
- await ctx.storage.delete(storageId);
138
- } catch {
139
- // Ignore
140
- }
141
- }
142
- await ctx.runMutation(
143
- selfHostingComponent.lib.setCurrentDeployment,
144
- { deploymentId: args.currentDeploymentId },
145
- );
146
- return { deleted: storageIds.length, blobIds };
147
- }
148
-
149
- case "listAssets": {
150
- return await ctx.runQuery(selfHostingComponent.lib.listAssets, {
151
- limit: args.limit,
152
- });
153
- }
154
-
155
- default:
156
- throw new Error(`Unknown hosting action: ${args.action}`);
157
- }
158
- },
159
- }),
160
-
161
- // ---- Deployment query (public, for client live-reload) ----
162
-
163
- getCurrentDeployment: queryGeneric({
164
- args: {},
165
- handler: async (ctx: any) => {
166
- return await ctx.runQuery(
167
- selfHostingComponent.lib.getCurrentDeployment,
168
- {},
169
- );
170
- },
171
- }),
172
-
173
- // ---- Invite management ----
174
-
175
- /**
176
- * Validate an invite token. Returns the invite if valid and pending,
177
- * or `null` otherwise. Used by the portal UI to check if an invite
178
- * link is valid before showing the registration form.
179
- */
180
- validateInvite: queryGeneric({
181
- args: { tokenHash: v.string() },
182
- handler: async (ctx: any, { tokenHash }: { tokenHash: string }) => {
183
- const invite = await ctx.runQuery(
184
- authComponent.public.inviteGetByTokenHash,
185
- { tokenHash },
186
- );
187
- if (!invite || invite.status !== "pending") {
188
- return null;
189
- }
190
- if (invite.expiresTime && invite.expiresTime < Date.now()) {
191
- return null;
192
- }
193
- return { _id: invite._id, role: invite.role };
194
- },
195
- }),
196
-
197
- /**
198
- * Accept a portal invite. Must be called by an authenticated user.
199
- * Marks the invite as accepted and records the accepting user's ID.
200
- *
201
- * The portal UI calls this after the user has signed in via magic link
202
- * following an invite link.
203
- */
204
- acceptInvite: mutationGeneric({
205
- args: { tokenHash: v.string() },
206
- handler: async (ctx: any, { tokenHash }: { tokenHash: string }) => {
207
- const userId = await auth.user.require(ctx);
208
-
209
- const invite = await ctx.runQuery(
210
- authComponent.public.inviteGetByTokenHash,
211
- { tokenHash },
212
- );
213
- if (!invite) {
214
- throw new Error("Invalid invite token");
215
- }
216
- if (invite.status !== "pending") {
217
- throw new Error(`Invite already ${invite.status}`);
218
- }
219
- if (invite.expiresTime && invite.expiresTime < Date.now()) {
220
- throw new Error("Invite has expired");
221
- }
222
-
223
- await ctx.runMutation(authComponent.public.inviteAccept, {
224
- inviteId: invite._id,
225
- acceptedByUserId: userId,
226
- });
227
- },
228
- }),
229
-
230
- /**
231
- * Create a portal admin invite. Internal mutation called by the CLI
232
- * (`npx @robelest/convex-auth portal link`).
233
- */
234
- createPortalInvite: internalMutationGeneric({
235
- args: { tokenHash: v.string() },
236
- handler: async (ctx: any, { tokenHash }: { tokenHash: string }) => {
237
- await ctx.runMutation(authComponent.public.inviteCreate, {
238
- tokenHash,
239
- role: "portalAdmin",
240
- status: "pending" as const,
241
- });
242
- return { portalUrl };
243
- },
244
- }),
245
-
246
- // ---- Portal data query (auth-gated) ----
247
-
248
- /**
249
- * Combined portal query for all auth data reads.
250
- * Requires the caller to be an authenticated portal admin.
251
- *
252
- * Actions:
253
- * - `listUsers` — List all users
254
- * - `listSessions` — List all sessions
255
- * - `getUser` — Get a single user by ID (requires `userId`)
256
- * - `getUserSessions` — List sessions for a user (requires `userId`)
257
- * - `getUserAccounts` — List auth accounts for a user (requires `userId`)
258
- * - `isAdmin` — Check if the current user is a portal admin
259
- */
260
- portalQuery: queryGeneric({
261
- args: {
262
- action: v.string(),
263
- userId: v.optional(v.string()),
264
- },
265
- handler: async (
266
- ctx: any,
267
- { action, userId }: { action: string; userId?: string },
268
- ) => {
269
- const currentUserId = await auth.user.require(ctx);
270
-
271
- // Allow isAdmin check without admin requirement
272
- if (action === "isAdmin") {
273
- try {
274
- await requirePortalAdmin(ctx, authComponent, currentUserId);
275
- return true;
276
- } catch {
277
- return false;
278
- }
279
- }
280
-
281
- await requirePortalAdmin(ctx, authComponent, currentUserId);
282
-
283
- switch (action) {
284
- case "listUsers":
285
- return await ctx.runQuery(authComponent.public.userList);
286
-
287
- case "listSessions":
288
- return await ctx.runQuery(authComponent.public.sessionList);
289
-
290
- case "getUser":
291
- return await ctx.runQuery(authComponent.public.userGetById, {
292
- userId: userId!,
293
- });
294
-
295
- case "getUserSessions":
296
- return await ctx.runQuery(authComponent.public.sessionListByUser, {
297
- userId: userId!,
298
- });
299
-
300
- case "getUserAccounts": {
301
- const accounts = await ctx.runQuery(
302
- authComponent.public.accountListByUser,
303
- { userId: userId! },
304
- );
305
- // Strip secrets — never send password hashes to the frontend
306
- return accounts.map(({ secret: _, ...rest }: any) => rest);
307
- }
308
-
309
- default:
310
- throw new Error(`Unknown portal query action: ${action}`);
311
- }
312
- },
313
- }),
314
-
315
- // ---- Portal mutation (auth-gated) ----
316
-
317
- /**
318
- * Combined portal mutation for all auth data writes.
319
- * Requires the caller to be an authenticated portal admin.
320
- *
321
- * Actions:
322
- * - `revokeSession` — Revoke (delete) a session (requires `sessionId`)
323
- */
324
- portalMutation: mutationGeneric({
325
- args: {
326
- action: v.string(),
327
- sessionId: v.optional(v.string()),
328
- },
329
- handler: async (
330
- ctx: any,
331
- { action, sessionId }: { action: string; sessionId?: string },
332
- ) => {
333
- const currentUserId = await auth.user.require(ctx);
334
- await requirePortalAdmin(ctx, authComponent, currentUserId);
335
-
336
- switch (action) {
337
- case "revokeSession":
338
- await ctx.runMutation(authComponent.public.sessionDelete, {
339
- sessionId: sessionId!,
340
- });
341
- return;
342
-
343
- default:
344
- throw new Error(`Unknown portal mutation action: ${action}`);
345
- }
346
- },
347
- }),
348
-
349
- // ---- Portal namespace ----
350
-
351
- portal: {
352
- /**
353
- * The URL where the portal is served. Used by the Svelte client
354
- * as the `redirectTo` for magic link sign-in.
355
- */
356
- portalUrl,
357
-
358
- /**
359
- * Register HTTP routes that serve the portal static UI.
360
- */
361
- addHttpRoutes: (
362
- http: HttpRouter,
363
- opts?: { pathPrefix?: string; spaFallback?: boolean },
364
- ) => {
365
- const prefix = opts?.pathPrefix ?? "/portal";
366
-
367
- // Static file serving
368
- registerStaticRoutes(http, selfHostingComponent, {
369
- pathPrefix: prefix,
370
- spaFallback: opts?.spaFallback ?? true,
371
- });
372
- },
373
- },
374
- };
375
- }