@robelest/convex-auth 0.0.4-preview.22 → 0.0.4-preview.23

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 (306) hide show
  1. package/dist/authorization/index.d.ts +1 -1
  2. package/dist/authorization/index.js +1 -1
  3. package/dist/authorization/index.js.map +1 -1
  4. package/dist/client/index.d.ts +1 -2
  5. package/dist/client/index.d.ts.map +1 -1
  6. package/dist/client/index.js +36 -39
  7. package/dist/client/index.js.map +1 -1
  8. package/dist/component/client/index.d.ts +1 -2
  9. package/dist/component/model.d.ts +9 -9
  10. package/dist/component/model.d.ts.map +1 -1
  11. package/dist/component/public/enterprise/audit.d.ts.map +1 -1
  12. package/dist/component/public/enterprise/audit.js.map +1 -1
  13. package/dist/component/public/enterprise/core.d.ts.map +1 -1
  14. package/dist/component/public/enterprise/core.js.map +1 -1
  15. package/dist/component/public/enterprise/domains.d.ts.map +1 -1
  16. package/dist/component/public/enterprise/domains.js.map +1 -1
  17. package/dist/component/public/enterprise/scim.d.ts.map +1 -1
  18. package/dist/component/public/enterprise/scim.js.map +1 -1
  19. package/dist/component/public/enterprise/secrets.d.ts.map +1 -1
  20. package/dist/component/public/enterprise/secrets.js.map +1 -1
  21. package/dist/component/public/enterprise/webhooks.d.ts.map +1 -1
  22. package/dist/component/public/enterprise/webhooks.js.map +1 -1
  23. package/dist/component/public/factors/devices.d.ts.map +1 -1
  24. package/dist/component/public/factors/devices.js.map +1 -1
  25. package/dist/component/public/factors/passkeys.d.ts.map +1 -1
  26. package/dist/component/public/factors/passkeys.js.map +1 -1
  27. package/dist/component/public/factors/totp.d.ts.map +1 -1
  28. package/dist/component/public/factors/totp.js.map +1 -1
  29. package/dist/component/public/groups/core.js.map +1 -1
  30. package/dist/component/public/groups/invites.d.ts.map +1 -1
  31. package/dist/component/public/groups/invites.js.map +1 -1
  32. package/dist/component/public/groups/members.d.ts.map +1 -1
  33. package/dist/component/public/groups/members.js.map +1 -1
  34. package/dist/component/public/identity/accounts.d.ts.map +1 -1
  35. package/dist/component/public/identity/accounts.js.map +1 -1
  36. package/dist/component/public/identity/codes.d.ts.map +1 -1
  37. package/dist/component/public/identity/codes.js.map +1 -1
  38. package/dist/component/public/identity/sessions.d.ts.map +1 -1
  39. package/dist/component/public/identity/sessions.js.map +1 -1
  40. package/dist/component/public/identity/tokens.d.ts.map +1 -1
  41. package/dist/component/public/identity/tokens.js.map +1 -1
  42. package/dist/component/public/identity/users.d.ts.map +1 -1
  43. package/dist/component/public/identity/users.js.map +1 -1
  44. package/dist/component/public/identity/verifiers.d.ts.map +1 -1
  45. package/dist/component/public/identity/verifiers.js.map +1 -1
  46. package/dist/component/public/security/keys.d.ts.map +1 -1
  47. package/dist/component/public/security/keys.js.map +1 -1
  48. package/dist/component/public/security/limits.d.ts.map +1 -1
  49. package/dist/component/public/security/limits.js.map +1 -1
  50. package/dist/component/schema.d.ts +42 -42
  51. package/dist/component/server/auth.d.ts +37 -40
  52. package/dist/component/server/auth.d.ts.map +1 -1
  53. package/dist/component/server/auth.js +57 -23
  54. package/dist/component/server/auth.js.map +1 -1
  55. package/dist/component/server/core.js +116 -235
  56. package/dist/component/server/core.js.map +1 -1
  57. package/dist/component/server/crypto.js +25 -7
  58. package/dist/component/server/crypto.js.map +1 -1
  59. package/dist/component/server/device.js +58 -15
  60. package/dist/component/server/device.js.map +1 -1
  61. package/dist/component/server/enterprise/domain.js +148 -59
  62. package/dist/component/server/enterprise/domain.js.map +1 -1
  63. package/dist/component/server/enterprise/http.js +36 -15
  64. package/dist/component/server/enterprise/http.js.map +1 -1
  65. package/dist/component/server/enterprise/oidc.js +1 -1
  66. package/dist/component/server/http.js +26 -21
  67. package/dist/component/server/http.js.map +1 -1
  68. package/dist/component/server/identity.js +5 -2
  69. package/dist/component/server/identity.js.map +1 -1
  70. package/dist/component/server/limits.js +21 -30
  71. package/dist/component/server/limits.js.map +1 -1
  72. package/dist/component/server/mutations/account.js +12 -10
  73. package/dist/component/server/mutations/account.js.map +1 -1
  74. package/dist/component/server/mutations/code.js +5 -2
  75. package/dist/component/server/mutations/code.js.map +1 -1
  76. package/dist/component/server/mutations/invalidate.js +1 -1
  77. package/dist/component/server/mutations/invalidate.js.map +1 -1
  78. package/dist/component/server/mutations/oauth.js +10 -4
  79. package/dist/component/server/mutations/oauth.js.map +1 -1
  80. package/dist/component/server/mutations/refresh.js +2 -2
  81. package/dist/component/server/mutations/refresh.js.map +1 -1
  82. package/dist/component/server/mutations/register.js +46 -42
  83. package/dist/component/server/mutations/register.js.map +1 -1
  84. package/dist/component/server/mutations/retrieve.js +21 -25
  85. package/dist/component/server/mutations/retrieve.js.map +1 -1
  86. package/dist/component/server/mutations/signature.js +10 -4
  87. package/dist/component/server/mutations/signature.js.map +1 -1
  88. package/dist/component/server/mutations/signout.js.map +1 -1
  89. package/dist/component/server/mutations/store.js +9 -24
  90. package/dist/component/server/mutations/store.js.map +1 -1
  91. package/dist/component/server/mutations/verifier.js.map +1 -1
  92. package/dist/component/server/mutations/verify.js +1 -1
  93. package/dist/component/server/mutations/verify.js.map +1 -1
  94. package/dist/component/server/oauth.js +53 -16
  95. package/dist/component/server/oauth.js.map +1 -1
  96. package/dist/component/server/passkey.js +115 -31
  97. package/dist/component/server/passkey.js.map +1 -1
  98. package/dist/component/server/redirects.js +9 -3
  99. package/dist/component/server/redirects.js.map +1 -1
  100. package/dist/component/server/refresh.js +10 -7
  101. package/dist/component/server/refresh.js.map +1 -1
  102. package/dist/component/server/runtime.d.ts +1 -1
  103. package/dist/component/server/runtime.d.ts.map +1 -1
  104. package/dist/component/server/runtime.js +62 -20
  105. package/dist/component/server/runtime.js.map +1 -1
  106. package/dist/component/server/signin.js +34 -10
  107. package/dist/component/server/signin.js.map +1 -1
  108. package/dist/component/server/totp.js +79 -19
  109. package/dist/component/server/totp.js.map +1 -1
  110. package/dist/component/server/types.d.ts +12 -20
  111. package/dist/component/server/types.d.ts.map +1 -1
  112. package/dist/component/server/types.js.map +1 -1
  113. package/dist/component/server/users.js +6 -3
  114. package/dist/component/server/users.js.map +1 -1
  115. package/dist/component/server/utils.js +10 -4
  116. package/dist/component/server/utils.js.map +1 -1
  117. package/dist/core/types.d.ts +14 -22
  118. package/dist/core/types.d.ts.map +1 -1
  119. package/dist/factors/device.js +8 -9
  120. package/dist/factors/device.js.map +1 -1
  121. package/dist/factors/passkey.js +18 -21
  122. package/dist/factors/passkey.js.map +1 -1
  123. package/dist/providers/password.js +66 -81
  124. package/dist/providers/password.js.map +1 -1
  125. package/dist/runtime/invite.js +2 -8
  126. package/dist/runtime/invite.js.map +1 -1
  127. package/dist/server/auth.d.ts +37 -40
  128. package/dist/server/auth.d.ts.map +1 -1
  129. package/dist/server/auth.js +57 -23
  130. package/dist/server/auth.js.map +1 -1
  131. package/dist/server/core.d.ts +71 -159
  132. package/dist/server/core.d.ts.map +1 -1
  133. package/dist/server/core.js +116 -235
  134. package/dist/server/core.js.map +1 -1
  135. package/dist/server/crypto.d.ts.map +1 -1
  136. package/dist/server/crypto.js +25 -7
  137. package/dist/server/crypto.js.map +1 -1
  138. package/dist/server/device.js +58 -15
  139. package/dist/server/device.js.map +1 -1
  140. package/dist/server/enterprise/domain.d.ts +0 -8
  141. package/dist/server/enterprise/domain.d.ts.map +1 -1
  142. package/dist/server/enterprise/domain.js +148 -59
  143. package/dist/server/enterprise/domain.js.map +1 -1
  144. package/dist/server/enterprise/http.d.ts.map +1 -1
  145. package/dist/server/enterprise/http.js +35 -14
  146. package/dist/server/enterprise/http.js.map +1 -1
  147. package/dist/server/http.d.ts +2 -2
  148. package/dist/server/http.d.ts.map +1 -1
  149. package/dist/server/http.js +25 -20
  150. package/dist/server/http.js.map +1 -1
  151. package/dist/server/identity.js +5 -2
  152. package/dist/server/identity.js.map +1 -1
  153. package/dist/server/index.d.ts +2 -2
  154. package/dist/server/limits.js +21 -30
  155. package/dist/server/limits.js.map +1 -1
  156. package/dist/server/mounts.d.ts +24 -62
  157. package/dist/server/mounts.d.ts.map +1 -1
  158. package/dist/server/mounts.js +45 -106
  159. package/dist/server/mounts.js.map +1 -1
  160. package/dist/server/mutations/account.d.ts +8 -9
  161. package/dist/server/mutations/account.d.ts.map +1 -1
  162. package/dist/server/mutations/account.js +11 -9
  163. package/dist/server/mutations/account.js.map +1 -1
  164. package/dist/server/mutations/code.d.ts +12 -12
  165. package/dist/server/mutations/code.d.ts.map +1 -1
  166. package/dist/server/mutations/code.js +5 -2
  167. package/dist/server/mutations/code.js.map +1 -1
  168. package/dist/server/mutations/invalidate.d.ts +4 -4
  169. package/dist/server/mutations/invalidate.d.ts.map +1 -1
  170. package/dist/server/mutations/invalidate.js.map +1 -1
  171. package/dist/server/mutations/oauth.d.ts +14 -12
  172. package/dist/server/mutations/oauth.d.ts.map +1 -1
  173. package/dist/server/mutations/oauth.js +9 -3
  174. package/dist/server/mutations/oauth.js.map +1 -1
  175. package/dist/server/mutations/refresh.d.ts +3 -3
  176. package/dist/server/mutations/refresh.d.ts.map +1 -1
  177. package/dist/server/mutations/refresh.js +1 -1
  178. package/dist/server/mutations/refresh.js.map +1 -1
  179. package/dist/server/mutations/register.d.ts +11 -11
  180. package/dist/server/mutations/register.d.ts.map +1 -1
  181. package/dist/server/mutations/register.js +45 -41
  182. package/dist/server/mutations/register.js.map +1 -1
  183. package/dist/server/mutations/retrieve.d.ts +6 -6
  184. package/dist/server/mutations/retrieve.d.ts.map +1 -1
  185. package/dist/server/mutations/retrieve.js +20 -24
  186. package/dist/server/mutations/retrieve.js.map +1 -1
  187. package/dist/server/mutations/signature.d.ts +6 -7
  188. package/dist/server/mutations/signature.d.ts.map +1 -1
  189. package/dist/server/mutations/signature.js +9 -3
  190. package/dist/server/mutations/signature.js.map +1 -1
  191. package/dist/server/mutations/signin.d.ts +5 -5
  192. package/dist/server/mutations/signin.d.ts.map +1 -1
  193. package/dist/server/mutations/signout.js.map +1 -1
  194. package/dist/server/mutations/store.d.ts +83 -83
  195. package/dist/server/mutations/store.js +8 -23
  196. package/dist/server/mutations/store.js.map +1 -1
  197. package/dist/server/mutations/verifier.js.map +1 -1
  198. package/dist/server/mutations/verify.d.ts +7 -7
  199. package/dist/server/mutations/verify.d.ts.map +1 -1
  200. package/dist/server/mutations/verify.js.map +1 -1
  201. package/dist/server/oauth.js +53 -16
  202. package/dist/server/oauth.js.map +1 -1
  203. package/dist/server/passkey.d.ts +2 -2
  204. package/dist/server/passkey.d.ts.map +1 -1
  205. package/dist/server/passkey.js +114 -30
  206. package/dist/server/passkey.js.map +1 -1
  207. package/dist/server/redirects.js +9 -3
  208. package/dist/server/redirects.js.map +1 -1
  209. package/dist/server/refresh.js +10 -7
  210. package/dist/server/refresh.js.map +1 -1
  211. package/dist/server/runtime.d.ts +7 -7
  212. package/dist/server/runtime.d.ts.map +1 -1
  213. package/dist/server/runtime.js +61 -19
  214. package/dist/server/runtime.js.map +1 -1
  215. package/dist/server/signin.js +34 -10
  216. package/dist/server/signin.js.map +1 -1
  217. package/dist/server/ssr.d.ts.map +1 -1
  218. package/dist/server/ssr.js +175 -184
  219. package/dist/server/ssr.js.map +1 -1
  220. package/dist/server/totp.js +78 -18
  221. package/dist/server/totp.js.map +1 -1
  222. package/dist/server/types.d.ts +13 -21
  223. package/dist/server/types.d.ts.map +1 -1
  224. package/dist/server/types.js.map +1 -1
  225. package/dist/server/users.js +6 -3
  226. package/dist/server/users.js.map +1 -1
  227. package/dist/server/utils.js +10 -4
  228. package/dist/server/utils.js.map +1 -1
  229. package/package.json +1 -5
  230. package/src/authorization/index.ts +1 -1
  231. package/src/client/core/types.ts +14 -14
  232. package/src/client/factors/device.ts +10 -12
  233. package/src/client/factors/passkey.ts +23 -26
  234. package/src/client/index.ts +54 -64
  235. package/src/client/runtime/invite.ts +5 -7
  236. package/src/component/index.ts +1 -1
  237. package/src/component/public/enterprise/audit.ts +6 -1
  238. package/src/component/public/enterprise/core.ts +1 -0
  239. package/src/component/public/enterprise/domains.ts +5 -1
  240. package/src/component/public/enterprise/scim.ts +1 -0
  241. package/src/component/public/enterprise/secrets.ts +1 -0
  242. package/src/component/public/enterprise/webhooks.ts +1 -0
  243. package/src/component/public/factors/devices.ts +1 -0
  244. package/src/component/public/factors/passkeys.ts +1 -0
  245. package/src/component/public/factors/totp.ts +1 -0
  246. package/src/component/public/groups/core.ts +1 -1
  247. package/src/component/public/groups/invites.ts +7 -1
  248. package/src/component/public/groups/members.ts +1 -0
  249. package/src/component/public/identity/accounts.ts +1 -0
  250. package/src/component/public/identity/codes.ts +1 -0
  251. package/src/component/public/identity/sessions.ts +1 -0
  252. package/src/component/public/identity/tokens.ts +1 -0
  253. package/src/component/public/identity/users.ts +1 -0
  254. package/src/component/public/identity/verifiers.ts +1 -0
  255. package/src/component/public/security/keys.ts +1 -0
  256. package/src/component/public/security/limits.ts +1 -0
  257. package/src/providers/password.ts +89 -110
  258. package/src/server/auth.ts +92 -70
  259. package/src/server/core.ts +197 -233
  260. package/src/server/crypto.ts +31 -29
  261. package/src/server/device.ts +65 -32
  262. package/src/server/enterprise/domain.ts +158 -170
  263. package/src/server/enterprise/http.ts +46 -39
  264. package/src/server/http.ts +36 -30
  265. package/src/server/identity.ts +5 -5
  266. package/src/server/index.ts +1 -1
  267. package/src/server/limits.ts +53 -80
  268. package/src/server/mounts.ts +47 -74
  269. package/src/server/mutations/account.ts +22 -36
  270. package/src/server/mutations/code.ts +6 -6
  271. package/src/server/mutations/invalidate.ts +1 -1
  272. package/src/server/mutations/oauth.ts +14 -8
  273. package/src/server/mutations/refresh.ts +5 -4
  274. package/src/server/mutations/register.ts +87 -132
  275. package/src/server/mutations/retrieve.ts +44 -44
  276. package/src/server/mutations/signature.ts +13 -6
  277. package/src/server/mutations/signout.ts +1 -1
  278. package/src/server/mutations/store.ts +16 -31
  279. package/src/server/mutations/verifier.ts +1 -1
  280. package/src/server/mutations/verify.ts +3 -5
  281. package/src/server/oauth.ts +60 -69
  282. package/src/server/passkey.ts +567 -517
  283. package/src/server/redirects.ts +10 -6
  284. package/src/server/refresh.ts +14 -18
  285. package/src/server/runtime.ts +70 -55
  286. package/src/server/signin.ts +44 -37
  287. package/src/server/ssr.ts +390 -407
  288. package/src/server/totp.ts +85 -35
  289. package/src/server/types.ts +19 -22
  290. package/src/server/users.ts +7 -6
  291. package/src/server/utils.ts +10 -12
  292. package/dist/component/server/authError.js +0 -34
  293. package/dist/component/server/authError.js.map +0 -1
  294. package/dist/component/server/errors.d.ts +0 -1
  295. package/dist/component/server/errors.js +0 -137
  296. package/dist/component/server/errors.js.map +0 -1
  297. package/dist/server/authError.d.ts +0 -46
  298. package/dist/server/authError.d.ts.map +0 -1
  299. package/dist/server/authError.js +0 -34
  300. package/dist/server/authError.js.map +0 -1
  301. package/dist/server/errors.d.ts +0 -177
  302. package/dist/server/errors.d.ts.map +0 -1
  303. package/dist/server/errors.js +0 -212
  304. package/dist/server/errors.js.map +0 -1
  305. package/src/server/authError.ts +0 -44
  306. package/src/server/errors.ts +0 -290
@@ -1 +1 @@
1
- {"version":3,"file":"tokens.js","names":[],"sources":["../../../../src/component/public/identity/tokens.ts"],"sourcesContent":["import { v } from \"convex/values\";\nimport { mutation, query } from \"../../functions\";\nimport { vRefreshTokenDoc } from \"../../model\";\n\n/**\n * Create a new refresh token for a session.\n *\n * Inserts a document into the `RefreshToken` table. Refresh tokens are used to\n * obtain new access tokens without requiring the user to re-authenticate. When\n * a refresh token is rotated, the new token references the old one via\n * `parentRefreshTokenId` to form a token chain for replay detection.\n *\n * @param args.sessionId - The document ID of the session this refresh token belongs to.\n * @param args.expirationTime - The Unix timestamp (in milliseconds) at which this refresh token expires.\n * @param args.parentRefreshTokenId - The document ID of the parent refresh token that was\n * exchanged to create this one. Omitted for the initial token in a session.\n * @returns The document ID of the newly created refresh token.\n *\n * @example\n * ```ts\n * const tokenId = await ctx.runMutation(\n * component.identity.tokens.refreshTokenCreate,\n * {\n * sessionId: session._id,\n * expirationTime: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days\n * },\n * );\n * ```\n */\nexport const refreshTokenCreate = mutation({\n args: {\n sessionId: v.id(\"Session\"),\n expirationTime: v.number(),\n parentRefreshTokenId: v.optional(v.id(\"RefreshToken\")),\n },\n returns: v.id(\"RefreshToken\"),\n handler: async (ctx, args) => {\n return await ctx.db.insert(\"RefreshToken\", args as any);\n },\n});\n\n/**\n * Retrieve a single refresh token by its Convex document ID.\n *\n * Performs a direct point lookup on the `RefreshToken` table. Returns `null` if\n * the token has been deleted or never existed.\n *\n * @param args.refreshTokenId - The Convex document ID (`Id<\"RefreshToken\">`) of the token to retrieve.\n * @returns The refresh token document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const token = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetById,\n * { refreshTokenId: storedTokenId },\n * );\n * if (token !== null && token.expirationTime > Date.now()) {\n * console.log(\"Refresh token is still valid\");\n * }\n * ```\n */\nexport const refreshTokenGetById = query({\n args: { refreshTokenId: v.id(\"RefreshToken\") },\n returns: v.union(vRefreshTokenDoc, v.null()),\n handler: async (ctx, { refreshTokenId }) => {\n return await ctx.db.get(\"RefreshToken\", refreshTokenId);\n },\n});\n\n/**\n * Patch a refresh token document with partial data.\n *\n * Merges the provided fields into the existing refresh token document. This is\n * primarily used to record `firstUsedTime` when a refresh token is first\n * exchanged, marking it as consumed for replay detection.\n *\n * @param args.refreshTokenId - The document ID of the refresh token to update.\n * @param args.data - A partial object containing the fields to merge (e.g. `{ firstUsedTime: number }`).\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Mark the refresh token as used\n * await ctx.runMutation(\n * component.identity.tokens.refreshTokenPatch,\n * {\n * refreshTokenId: token._id,\n * data: { firstUsedTime: Date.now() },\n * },\n * );\n * ```\n */\nexport const refreshTokenPatch = mutation({\n args: { refreshTokenId: v.id(\"RefreshToken\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { refreshTokenId, data }) => {\n await ctx.db.patch(\"RefreshToken\", refreshTokenId, data);\n return null;\n },\n});\n\n/**\n * Get child tokens that were created by exchanging a specific parent token.\n *\n * Queries the `RefreshToken` table using the `session_id_parent_refresh_token_id`\n * index to find all tokens whose `parentRefreshTokenId` matches the provided\n * parent. This is used for replay detection: if a parent token has more than\n * one child, it indicates a potential token reuse attack.\n *\n * @param args.sessionId - The document ID of the session the tokens belong to.\n * @param args.parentRefreshTokenId - The document ID of the parent refresh token whose children to retrieve.\n * @returns An array of refresh token documents that were derived from the specified parent token.\n *\n * @example\n * ```ts\n * const children = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetChildren,\n * {\n * sessionId: session._id,\n * parentRefreshTokenId: parentToken._id,\n * },\n * );\n * if (children.length > 1) {\n * console.warn(\"Possible token reuse detected!\");\n * }\n * ```\n */\nexport const refreshTokenGetChildren = query({\n args: {\n sessionId: v.id(\"Session\"),\n parentRefreshTokenId: v.id(\"RefreshToken\"),\n },\n returns: v.array(vRefreshTokenDoc),\n handler: async (ctx, { sessionId, parentRefreshTokenId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q\n .eq(\"sessionId\", sessionId as any)\n .eq(\"parentRefreshTokenId\", parentRefreshTokenId as any),\n )\n .collect();\n },\n});\n\n/**\n * List all refresh tokens belonging to a specific session.\n *\n * Queries the `RefreshToken` table using the `session_id_parent_refresh_token_id`\n * index to efficiently retrieve every refresh token associated with the given\n * session, including both active and consumed tokens.\n *\n * @param args.sessionId - The document ID of the session whose refresh tokens should be retrieved.\n * @returns An array of all refresh token documents for the specified session.\n *\n * @example\n * ```ts\n * const tokens = await ctx.runQuery(\n * component.identity.tokens.refreshTokenListBySession,\n * { sessionId: session._id },\n * );\n * console.log(`Session has ${tokens.length} refresh token(s)`);\n * ```\n */\nexport const refreshTokenListBySession = query({\n args: { sessionId: v.id(\"Session\") },\n returns: v.array(vRefreshTokenDoc),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q.eq(\"sessionId\", sessionId as any),\n )\n .collect();\n },\n});\n\n/**\n * Delete all refresh tokens for a session.\n *\n * Queries the `RefreshToken` table for all tokens belonging to the given session\n * and deletes them in parallel. This is typically called when a session is\n * revoked or when token reuse is detected, effectively invalidating the entire\n * token chain for that session.\n *\n * @param args.sessionId - The document ID of the session whose refresh tokens should be deleted.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Invalidate all tokens for a compromised session\n * await ctx.runMutation(\n * component.identity.tokens.refreshTokenDeleteAll,\n * { sessionId: session._id },\n * );\n * ```\n */\nexport const refreshTokenDeleteAll = mutation({\n args: { sessionId: v.id(\"Session\") },\n returns: v.null(),\n handler: async (ctx, { sessionId }) => {\n const tokens = await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q.eq(\"sessionId\", sessionId as any),\n )\n .collect();\n await Promise.all(\n tokens.map((token) => ctx.db.delete(\"RefreshToken\", token._id)),\n );\n return null;\n },\n});\n\n/**\n * Get the active (unused) refresh token for a session.\n *\n * Queries the `RefreshToken` table using the `session_id_first_used` index to\n * find the most recently created token for the session that has not yet been\n * exchanged (i.e. `firstUsedTime` is `undefined`). This represents the current\n * valid refresh token the client should be holding.\n *\n * @param args.sessionId - The document ID of the session whose active refresh token should be retrieved.\n * @returns The most recent unused refresh token document, or `null` if no active token exists\n * (e.g. all tokens have been consumed or the session has no tokens).\n *\n * @example\n * ```ts\n * const activeToken = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetActive,\n * { sessionId: session._id },\n * );\n * if (activeToken !== null) {\n * console.log(`Active token expires at: ${activeToken.expirationTime}`);\n * }\n * ```\n */\nexport const refreshTokenGetActive = query({\n args: { sessionId: v.id(\"Session\") },\n returns: v.union(vRefreshTokenDoc, v.null()),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_first_used\", (q) =>\n q.eq(\"sessionId\", sessionId as any).eq(\"firstUsedTime\", undefined),\n )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B,CAC3D,CACA,SAAS;;CAEf,CAAC;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,4BAA4B,MAAM;CAC7C,MAAM,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE;CACpC,SAAS,EAAE,MAAM,iBAAiB;CAClC,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,eAAe,CACrB,UAAU,uCAAuC,MAChD,EAAE,GAAG,aAAa,UAAiB,CACpC,CACA,SAAS;;CAEf,CAAC;;;;;;;;;;;;;;;;;;;;;AAsBF,MAAa,wBAAwB,SAAS;CAC5C,MAAM,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE;CACpC,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,gBAAgB;EACrC,MAAM,SAAS,MAAM,IAAI,GACtB,MAAM,eAAe,CACrB,UAAU,uCAAuC,MAChD,EAAE,GAAG,aAAa,UAAiB,CACpC,CACA,SAAS;AACZ,QAAM,QAAQ,IACZ,OAAO,KAAK,UAAU,IAAI,GAAG,OAAO,gBAAgB,MAAM,IAAI,CAAC,CAChE;AACD,SAAO;;CAEV,CAAC;;;;;;;;;;;;;;;;;;;;;;;;AAyBF,MAAa,wBAAwB,MAAM;CACzC,MAAM,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE;CACpC,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,eAAe,CACrB,UAAU,0BAA0B,MACnC,EAAE,GAAG,aAAa,UAAiB,CAAC,GAAG,iBAAiB,OAAU,CACnE,CACA,MAAM,OAAO,CACb,OAAO;;CAEb,CAAC"}
1
+ {"version":3,"file":"tokens.js","names":[],"sources":["../../../../src/component/public/identity/tokens.ts"],"sourcesContent":["import { v } from \"convex/values\";\n\nimport { mutation, query } from \"../../functions\";\nimport { vRefreshTokenDoc } from \"../../model\";\n\n/**\n * Create a new refresh token for a session.\n *\n * Inserts a document into the `RefreshToken` table. Refresh tokens are used to\n * obtain new access tokens without requiring the user to re-authenticate. When\n * a refresh token is rotated, the new token references the old one via\n * `parentRefreshTokenId` to form a token chain for replay detection.\n *\n * @param args.sessionId - The document ID of the session this refresh token belongs to.\n * @param args.expirationTime - The Unix timestamp (in milliseconds) at which this refresh token expires.\n * @param args.parentRefreshTokenId - The document ID of the parent refresh token that was\n * exchanged to create this one. Omitted for the initial token in a session.\n * @returns The document ID of the newly created refresh token.\n *\n * @example\n * ```ts\n * const tokenId = await ctx.runMutation(\n * component.identity.tokens.refreshTokenCreate,\n * {\n * sessionId: session._id,\n * expirationTime: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days\n * },\n * );\n * ```\n */\nexport const refreshTokenCreate = mutation({\n args: {\n sessionId: v.id(\"Session\"),\n expirationTime: v.number(),\n parentRefreshTokenId: v.optional(v.id(\"RefreshToken\")),\n },\n returns: v.id(\"RefreshToken\"),\n handler: async (ctx, args) => {\n return await ctx.db.insert(\"RefreshToken\", args as any);\n },\n});\n\n/**\n * Retrieve a single refresh token by its Convex document ID.\n *\n * Performs a direct point lookup on the `RefreshToken` table. Returns `null` if\n * the token has been deleted or never existed.\n *\n * @param args.refreshTokenId - The Convex document ID (`Id<\"RefreshToken\">`) of the token to retrieve.\n * @returns The refresh token document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const token = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetById,\n * { refreshTokenId: storedTokenId },\n * );\n * if (token !== null && token.expirationTime > Date.now()) {\n * console.log(\"Refresh token is still valid\");\n * }\n * ```\n */\nexport const refreshTokenGetById = query({\n args: { refreshTokenId: v.id(\"RefreshToken\") },\n returns: v.union(vRefreshTokenDoc, v.null()),\n handler: async (ctx, { refreshTokenId }) => {\n return await ctx.db.get(\"RefreshToken\", refreshTokenId);\n },\n});\n\n/**\n * Patch a refresh token document with partial data.\n *\n * Merges the provided fields into the existing refresh token document. This is\n * primarily used to record `firstUsedTime` when a refresh token is first\n * exchanged, marking it as consumed for replay detection.\n *\n * @param args.refreshTokenId - The document ID of the refresh token to update.\n * @param args.data - A partial object containing the fields to merge (e.g. `{ firstUsedTime: number }`).\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Mark the refresh token as used\n * await ctx.runMutation(\n * component.identity.tokens.refreshTokenPatch,\n * {\n * refreshTokenId: token._id,\n * data: { firstUsedTime: Date.now() },\n * },\n * );\n * ```\n */\nexport const refreshTokenPatch = mutation({\n args: { refreshTokenId: v.id(\"RefreshToken\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { refreshTokenId, data }) => {\n await ctx.db.patch(\"RefreshToken\", refreshTokenId, data);\n return null;\n },\n});\n\n/**\n * Get child tokens that were created by exchanging a specific parent token.\n *\n * Queries the `RefreshToken` table using the `session_id_parent_refresh_token_id`\n * index to find all tokens whose `parentRefreshTokenId` matches the provided\n * parent. This is used for replay detection: if a parent token has more than\n * one child, it indicates a potential token reuse attack.\n *\n * @param args.sessionId - The document ID of the session the tokens belong to.\n * @param args.parentRefreshTokenId - The document ID of the parent refresh token whose children to retrieve.\n * @returns An array of refresh token documents that were derived from the specified parent token.\n *\n * @example\n * ```ts\n * const children = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetChildren,\n * {\n * sessionId: session._id,\n * parentRefreshTokenId: parentToken._id,\n * },\n * );\n * if (children.length > 1) {\n * console.warn(\"Possible token reuse detected!\");\n * }\n * ```\n */\nexport const refreshTokenGetChildren = query({\n args: {\n sessionId: v.id(\"Session\"),\n parentRefreshTokenId: v.id(\"RefreshToken\"),\n },\n returns: v.array(vRefreshTokenDoc),\n handler: async (ctx, { sessionId, parentRefreshTokenId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q\n .eq(\"sessionId\", sessionId as any)\n .eq(\"parentRefreshTokenId\", parentRefreshTokenId as any),\n )\n .collect();\n },\n});\n\n/**\n * List all refresh tokens belonging to a specific session.\n *\n * Queries the `RefreshToken` table using the `session_id_parent_refresh_token_id`\n * index to efficiently retrieve every refresh token associated with the given\n * session, including both active and consumed tokens.\n *\n * @param args.sessionId - The document ID of the session whose refresh tokens should be retrieved.\n * @returns An array of all refresh token documents for the specified session.\n *\n * @example\n * ```ts\n * const tokens = await ctx.runQuery(\n * component.identity.tokens.refreshTokenListBySession,\n * { sessionId: session._id },\n * );\n * console.log(`Session has ${tokens.length} refresh token(s)`);\n * ```\n */\nexport const refreshTokenListBySession = query({\n args: { sessionId: v.id(\"Session\") },\n returns: v.array(vRefreshTokenDoc),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q.eq(\"sessionId\", sessionId as any),\n )\n .collect();\n },\n});\n\n/**\n * Delete all refresh tokens for a session.\n *\n * Queries the `RefreshToken` table for all tokens belonging to the given session\n * and deletes them in parallel. This is typically called when a session is\n * revoked or when token reuse is detected, effectively invalidating the entire\n * token chain for that session.\n *\n * @param args.sessionId - The document ID of the session whose refresh tokens should be deleted.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Invalidate all tokens for a compromised session\n * await ctx.runMutation(\n * component.identity.tokens.refreshTokenDeleteAll,\n * { sessionId: session._id },\n * );\n * ```\n */\nexport const refreshTokenDeleteAll = mutation({\n args: { sessionId: v.id(\"Session\") },\n returns: v.null(),\n handler: async (ctx, { sessionId }) => {\n const tokens = await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_parent_refresh_token_id\", (q) =>\n q.eq(\"sessionId\", sessionId as any),\n )\n .collect();\n await Promise.all(\n tokens.map((token) => ctx.db.delete(\"RefreshToken\", token._id)),\n );\n return null;\n },\n});\n\n/**\n * Get the active (unused) refresh token for a session.\n *\n * Queries the `RefreshToken` table using the `session_id_first_used` index to\n * find the most recently created token for the session that has not yet been\n * exchanged (i.e. `firstUsedTime` is `undefined`). This represents the current\n * valid refresh token the client should be holding.\n *\n * @param args.sessionId - The document ID of the session whose active refresh token should be retrieved.\n * @returns The most recent unused refresh token document, or `null` if no active token exists\n * (e.g. all tokens have been consumed or the session has no tokens).\n *\n * @example\n * ```ts\n * const activeToken = await ctx.runQuery(\n * component.identity.tokens.refreshTokenGetActive,\n * { sessionId: session._id },\n * );\n * if (activeToken !== null) {\n * console.log(`Active token expires at: ${activeToken.expirationTime}`);\n * }\n * ```\n */\nexport const refreshTokenGetActive = query({\n args: { sessionId: v.id(\"Session\") },\n returns: v.union(vRefreshTokenDoc, v.null()),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db\n .query(\"RefreshToken\")\n .withIndex(\"session_id_first_used\", (q) =>\n q.eq(\"sessionId\", sessionId as any).eq(\"firstUsedTime\", undefined),\n )\n .order(\"desc\")\n .first();\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,MAAa,qBAAqB,SAAS;CACzC,MAAM;EACJ,WAAW,EAAE,GAAG,UAAU;EAC1B,gBAAgB,EAAE,QAAQ;EAC1B,sBAAsB,EAAE,SAAS,EAAE,GAAG,eAAe,CAAC;EACvD;CACD,SAAS,EAAE,GAAG,eAAe;CAC7B,SAAS,OAAO,KAAK,SAAS;AAC5B,SAAO,MAAM,IAAI,GAAG,OAAO,gBAAgB,KAAY;;CAE1D,CAAC;;;;;;;;;;;;;;;;;;;;;AAsBF,MAAa,sBAAsB,MAAM;CACvC,MAAM,EAAE,gBAAgB,EAAE,GAAG,eAAe,EAAE;CAC9C,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,qBAAqB;AAC1C,SAAO,MAAM,IAAI,GAAG,IAAI,gBAAgB,eAAe;;CAE1D,CAAC;;;;;;;;;;;;;;;;;;;;;;;;AAyBF,MAAa,oBAAoB,SAAS;CACxC,MAAM;EAAE,gBAAgB,EAAE,GAAG,eAAe;EAAE,MAAM,EAA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;;;;;;;;;;;;;;;;;;AAyBF,MAAa,wBAAwB,MAAM;CACzC,MAAM,EAAE,WAAW,EAAE,GAAG,UAAU,EAAE;CACpC,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,eAAe,CACrB,UAAU,0BAA0B,MACnC,EAAE,GAAG,aAAa,UAAiB,CAAC,GAAG,iBAAiB,OAAU,CACnE,CACA,MAAM,OAAO,CACb,OAAO;;CAEb,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"users.d.ts","names":[],"sources":["../../../../src/component/public/identity/users.ts"],"mappings":";;;;;;;;;;;;;;;;AA2CA;;;;;AA6FA;;;;;AA+BA;;;;;AAqCA;;;;;AAuCA;;;;;AAmCA;cA3Oa,QAAA;;;;AA8Qb;;;;;AA2BA;;;;;;;;;;;;cA5Ma,WAAA;;;;;;;;;;;;;;;;;;;;;;;;cA+BA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;cAqCA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;cAuCA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,SAAA;;;;;;;;;;;;;;;;;;;cA2BA,UAAA"}
1
+ {"version":3,"file":"users.d.ts","names":[],"sources":["../../../../src/component/public/identity/users.ts"],"mappings":";;;;;;;;;;;;;;;;AA4CA;;;;;AA6FA;;;;;AA+BA;;;;;AAqCA;;;;;AAuCA;;;;;AAmCA;cA3Oa,QAAA;;;;AA8Qb;;;;;AA2BA;;;;;;;;;;;;cA5Ma,WAAA;;;;;;;;;;;;;;;;;;;;;;;;cA+BA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;cAqCA,uBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;cAuCA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,SAAA;;;;;;;;;;;;;;;;;;;cA2BA,UAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"users.js","names":[],"sources":["../../../../src/component/public/identity/users.ts"],"sourcesContent":["import { v } from \"convex/values\";\nimport { mutation, query } from \"../../functions\";\nimport { vPaginated, vUserDoc } from \"../../model\";\n\n/**\n * List users with optional filtering, sorting, and cursor-based pagination.\n *\n * Supports filtering by `email`, `phone`, `isAnonymous`, and `name`. When an\n * `email` or `phone` filter is provided, the corresponding database index is\n * used for efficient lookup; other filters are applied in-memory. Results are\n * returned as a paginated response `{ items, nextCursor }` -- pass `nextCursor`\n * back as `cursor` to fetch the next page, or receive `null` when all results\n * have been exhausted.\n *\n * @param args.where - Optional filter object. Fields: `email` (exact match),\n * `phone` (exact match), `isAnonymous` (boolean), `name` (exact match).\n * @param args.limit - Maximum number of users to return per page (1--100, default 50).\n * @param args.cursor - An opaque cursor string from a previous response's `nextCursor`\n * to continue pagination, or `null` / omitted to start from the beginning.\n * @param args.orderBy - The field to sort results by. One of `\"_creationTime\"`,\n * `\"name\"`, `\"email\"`, or `\"phone\"`. Defaults to `\"_creationTime\"`.\n * @param args.order - Sort direction: `\"asc\"` or `\"desc\"` (default `\"desc\"`).\n * @returns An object with `items` (array of user documents) and `nextCursor`\n * (`string | null`) for fetching subsequent pages.\n *\n * @example\n * ```ts\n * // Fetch the first page of non-anonymous users\n * const page1 = await ctx.runQuery(\n * component.identity.users.userList,\n * { where: { isAnonymous: false }, limit: 20 },\n * );\n * console.log(page1.items);\n *\n * // Fetch the next page\n * if (page1.nextCursor !== null) {\n * const page2 = await ctx.runQuery(\n * component.identity.users.userList,\n * { where: { isAnonymous: false }, limit: 20, cursor: page1.nextCursor },\n * );\n * }\n * ```\n */\nexport const userList = query({\n args: {\n where: v.optional(\n v.object({\n email: v.optional(v.string()),\n phone: v.optional(v.string()),\n isAnonymous: v.optional(v.boolean()),\n name: v.optional(v.string()),\n }),\n ),\n limit: v.optional(v.number()),\n cursor: v.optional(v.union(v.string(), v.null())),\n orderBy: v.optional(\n v.union(\n v.literal(\"_creationTime\"),\n v.literal(\"name\"),\n v.literal(\"email\"),\n v.literal(\"phone\"),\n ),\n ),\n order: v.optional(v.union(v.literal(\"asc\"), v.literal(\"desc\"))),\n },\n returns: vPaginated(vUserDoc),\n handler: async (ctx, args) => {\n const where = args.where ?? {};\n const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);\n const order = args.order ?? \"desc\";\n\n // Pick index based on where fields\n let q;\n if (where.email !== undefined) {\n q = ctx.db\n .query(\"User\")\n .withIndex(\"email\", (idx) => idx.eq(\"email\", where.email!));\n } else if (where.phone !== undefined) {\n q = ctx.db\n .query(\"User\")\n .withIndex(\"phone\", (idx) => idx.eq(\"phone\", where.phone!));\n } else {\n q = ctx.db.query(\"User\");\n }\n\n // Apply remaining filters\n if (where.isAnonymous !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"isAnonymous\"), where.isAnonymous!));\n }\n if (where.name !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"name\"), where.name!));\n }\n // email/phone filters when not used as index\n if (where.email !== undefined && where.phone !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"phone\"), where.phone!));\n }\n\n q = q.order(order);\n\n // Cursor-based pagination: skip past the cursor ID\n const all = await q.collect();\n let startIdx = 0;\n if (args.cursor) {\n const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);\n if (cursorIdx !== -1) {\n startIdx = cursorIdx + 1;\n }\n }\n const page = all.slice(startIdx, startIdx + limit + 1);\n const hasMore = page.length > limit;\n const items = hasMore ? page.slice(0, limit) : page;\n const nextCursor = hasMore ? items[items.length - 1]._id : null;\n return { items, nextCursor };\n },\n});\n\n/**\n * Retrieve a single user by their Convex document ID.\n *\n * Performs a direct point lookup on the `User` table. Returns `null` if the\n * user has been deleted or never existed.\n *\n * @param args.userId - The Convex document ID (`Id<\"User\">`) of the user to retrieve.\n * @returns The user document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userGetById,\n * { userId: session.userId },\n * );\n * if (user !== null) {\n * console.log(`Name: ${user.name}, Email: ${user.email}`);\n * }\n * ```\n */\nexport const userGetById = query({\n args: { userId: v.id(\"User\") },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { userId }) => {\n return await ctx.db.get(\"User\", userId);\n },\n});\n\n/**\n * Find a user by their verified email address.\n *\n * Queries the `User` table using the `email_verified` index to locate users\n * whose `email` matches and whose `emailVerificationTime` is set. If exactly\n * one user is found, that document is returned. Returns `null` if no user has\n * this email verified or if multiple users share the same verified email\n * (an ambiguous state that should not occur in normal operation).\n *\n * @param args.email - The verified email address to search for (case-sensitive, exact match).\n * @returns The matching user document if exactly one verified user is found, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userFindByVerifiedEmail,\n * { email: \"alice@example.com\" },\n * );\n * if (user !== null) {\n * console.log(`Found verified user: ${user._id}`);\n * }\n * ```\n */\nexport const userFindByVerifiedEmail = query({\n args: { email: v.string() },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { email }) => {\n const users = await ctx.db\n .query(\"User\")\n .withIndex(\"email_verified\", (q) =>\n q.eq(\"email\", email).gt(\"emailVerificationTime\", undefined),\n )\n .take(2);\n return users.length === 1 ? users[0] : null;\n },\n});\n\n/**\n * Find a user by their verified phone number.\n *\n * Queries the `User` table using the `phone_verified` index to locate users\n * whose `phone` matches and whose `phoneVerificationTime` is set. If exactly\n * one user is found, that document is returned. Returns `null` if no user has\n * this phone verified or if multiple users share the same verified phone\n * (an ambiguous state that should not occur in normal operation).\n *\n * @param args.phone - The verified phone number to search for (exact match, e.g. `\"+15551234567\"`).\n * @returns The matching user document if exactly one verified user is found, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userFindByVerifiedPhone,\n * { phone: \"+15551234567\" },\n * );\n * if (user !== null) {\n * console.log(`Found verified user: ${user._id}`);\n * }\n * ```\n */\nexport const userFindByVerifiedPhone = query({\n args: { phone: v.string() },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { phone }) => {\n const users = await ctx.db\n .query(\"User\")\n .withIndex(\"phone_verified\", (q) =>\n q.eq(\"phone\", phone).gt(\"phoneVerificationTime\", undefined),\n )\n .take(2);\n return users.length === 1 ? users[0] : null;\n },\n});\n\n/**\n * Insert a new user document into the `User` table.\n *\n * Creates a brand-new user record. The `data` argument should conform to the\n * User table schema (e.g. `name`, `email`, `phone`, `isAnonymous`, `image`,\n * `extend`), but is typed as `any` to allow flexible extension.\n *\n * @param args.data - The user document fields to insert. Typically includes `name`,\n * `email`, `isAnonymous`, and any custom fields under `extend`.\n * @returns The document ID of the newly created user.\n *\n * @example\n * ```ts\n * const userId = await ctx.runMutation(\n * component.identity.users.userInsert,\n * {\n * data: {\n * name: \"Alice\",\n * email: \"alice@example.com\",\n * isAnonymous: false,\n * },\n * },\n * );\n * ```\n */\nexport const userInsert = mutation({\n args: { data: v.any() },\n returns: v.id(\"User\"),\n handler: async (ctx, { data }) => {\n return await ctx.db.insert(\"User\", data);\n },\n});\n\n/**\n * Insert a new user or update an existing one (upsert).\n *\n * When `userId` is provided and refers to an existing user, the document is\n * patched with the supplied `data` and the same `userId` is returned. When\n * `userId` is omitted or `undefined`, a new user document is inserted and its\n * generated ID is returned. This is the primary mechanism used during sign-in\n * flows to either create or refresh user profile data.\n *\n * @param args.userId - The document ID of an existing user to update. If `undefined`,\n * a new user is created instead.\n * @param args.data - The user document fields to insert or merge. Accepts the same\n * shape as the User table schema.\n * @returns The document ID of the created or updated user.\n *\n * @example\n * ```ts\n * // Create a new user if none exists, or update the existing one\n * const userId = await ctx.runMutation(\n * component.identity.users.userUpsert,\n * {\n * userId: existingUserId ?? undefined,\n * data: { name: \"Alice\", email: \"alice@example.com\" },\n * },\n * );\n * ```\n */\nexport const userUpsert = mutation({\n args: { userId: v.optional(v.id(\"User\")), data: v.any() },\n returns: v.id(\"User\"),\n handler: async (ctx, { userId, data }) => {\n if (userId !== undefined) {\n await ctx.db.patch(\"User\", userId, data);\n return userId;\n }\n return await ctx.db.insert(\"User\", data);\n },\n});\n\n/**\n * Patch an existing user document with partial data.\n *\n * Merges the provided fields into the existing user document. Fields not\n * included in `data` are left unchanged. Useful for updating profile\n * information such as `name`, `email`, or custom `extend` fields without\n * overwriting the entire document.\n *\n * @param args.userId - The document ID of the user to update.\n * @param args.data - A partial object containing the fields to merge into the user document.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * component.identity.users.userPatch,\n * {\n * userId: user._id,\n * data: { name: \"Alice Smith\", image: \"https://example.com/avatar.png\" },\n * },\n * );\n * ```\n */\nexport const userPatch = mutation({\n args: { userId: v.id(\"User\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { userId, data }) => {\n await ctx.db.patch(\"User\", userId, data);\n return null;\n },\n});\n\n/**\n * Delete a user document by ID.\n *\n * Removes the user from the `User` table. This is a no-op if the user does not\n * exist (i.e. was already deleted). Callers should ensure that related resources\n * such as accounts, sessions, and refresh tokens are cleaned up separately.\n *\n * @param args.userId - The document ID of the user to delete.\n * @returns `null` on success (including when the user was already absent).\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * component.identity.users.userDelete,\n * { userId: user._id },\n * );\n * ```\n */\nexport const userDelete = mutation({\n args: { userId: v.id(\"User\") },\n returns: v.null(),\n handler: async (ctx, { userId }) => {\n if ((await ctx.db.get(\"User\", userId)) !== null) {\n await ctx.db.delete(\"User\", userId);\n }\n return null;\n },\n});\n\n// ============================================================================\n// Accounts\n// ============================================================================\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAa,WAAW,MAAM;CAC5B,MAAM;EACJ,OAAO,EAAE,SACP,EAAE,OAAO;GACP,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAC;GAC7B,OAAO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;;;;;;;;;;;;;;AAoBF,MAAa,aAAa,SAAS;CACjC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE;CAC9B,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,aAAa;AAClC,MAAK,MAAM,IAAI,GAAG,IAAI,QAAQ,OAAO,KAAM,KACzC,OAAM,IAAI,GAAG,OAAO,QAAQ,OAAO;AAErC,SAAO;;CAEV,CAAC"}
1
+ {"version":3,"file":"users.js","names":[],"sources":["../../../../src/component/public/identity/users.ts"],"sourcesContent":["import { v } from \"convex/values\";\n\nimport { mutation, query } from \"../../functions\";\nimport { vPaginated, vUserDoc } from \"../../model\";\n\n/**\n * List users with optional filtering, sorting, and cursor-based pagination.\n *\n * Supports filtering by `email`, `phone`, `isAnonymous`, and `name`. When an\n * `email` or `phone` filter is provided, the corresponding database index is\n * used for efficient lookup; other filters are applied in-memory. Results are\n * returned as a paginated response `{ items, nextCursor }` -- pass `nextCursor`\n * back as `cursor` to fetch the next page, or receive `null` when all results\n * have been exhausted.\n *\n * @param args.where - Optional filter object. Fields: `email` (exact match),\n * `phone` (exact match), `isAnonymous` (boolean), `name` (exact match).\n * @param args.limit - Maximum number of users to return per page (1--100, default 50).\n * @param args.cursor - An opaque cursor string from a previous response's `nextCursor`\n * to continue pagination, or `null` / omitted to start from the beginning.\n * @param args.orderBy - The field to sort results by. One of `\"_creationTime\"`,\n * `\"name\"`, `\"email\"`, or `\"phone\"`. Defaults to `\"_creationTime\"`.\n * @param args.order - Sort direction: `\"asc\"` or `\"desc\"` (default `\"desc\"`).\n * @returns An object with `items` (array of user documents) and `nextCursor`\n * (`string | null`) for fetching subsequent pages.\n *\n * @example\n * ```ts\n * // Fetch the first page of non-anonymous users\n * const page1 = await ctx.runQuery(\n * component.identity.users.userList,\n * { where: { isAnonymous: false }, limit: 20 },\n * );\n * console.log(page1.items);\n *\n * // Fetch the next page\n * if (page1.nextCursor !== null) {\n * const page2 = await ctx.runQuery(\n * component.identity.users.userList,\n * { where: { isAnonymous: false }, limit: 20, cursor: page1.nextCursor },\n * );\n * }\n * ```\n */\nexport const userList = query({\n args: {\n where: v.optional(\n v.object({\n email: v.optional(v.string()),\n phone: v.optional(v.string()),\n isAnonymous: v.optional(v.boolean()),\n name: v.optional(v.string()),\n }),\n ),\n limit: v.optional(v.number()),\n cursor: v.optional(v.union(v.string(), v.null())),\n orderBy: v.optional(\n v.union(\n v.literal(\"_creationTime\"),\n v.literal(\"name\"),\n v.literal(\"email\"),\n v.literal(\"phone\"),\n ),\n ),\n order: v.optional(v.union(v.literal(\"asc\"), v.literal(\"desc\"))),\n },\n returns: vPaginated(vUserDoc),\n handler: async (ctx, args) => {\n const where = args.where ?? {};\n const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);\n const order = args.order ?? \"desc\";\n\n // Pick index based on where fields\n let q;\n if (where.email !== undefined) {\n q = ctx.db\n .query(\"User\")\n .withIndex(\"email\", (idx) => idx.eq(\"email\", where.email!));\n } else if (where.phone !== undefined) {\n q = ctx.db\n .query(\"User\")\n .withIndex(\"phone\", (idx) => idx.eq(\"phone\", where.phone!));\n } else {\n q = ctx.db.query(\"User\");\n }\n\n // Apply remaining filters\n if (where.isAnonymous !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"isAnonymous\"), where.isAnonymous!));\n }\n if (where.name !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"name\"), where.name!));\n }\n // email/phone filters when not used as index\n if (where.email !== undefined && where.phone !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"phone\"), where.phone!));\n }\n\n q = q.order(order);\n\n // Cursor-based pagination: skip past the cursor ID\n const all = await q.collect();\n let startIdx = 0;\n if (args.cursor) {\n const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);\n if (cursorIdx !== -1) {\n startIdx = cursorIdx + 1;\n }\n }\n const page = all.slice(startIdx, startIdx + limit + 1);\n const hasMore = page.length > limit;\n const items = hasMore ? page.slice(0, limit) : page;\n const nextCursor = hasMore ? items[items.length - 1]._id : null;\n return { items, nextCursor };\n },\n});\n\n/**\n * Retrieve a single user by their Convex document ID.\n *\n * Performs a direct point lookup on the `User` table. Returns `null` if the\n * user has been deleted or never existed.\n *\n * @param args.userId - The Convex document ID (`Id<\"User\">`) of the user to retrieve.\n * @returns The user document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userGetById,\n * { userId: session.userId },\n * );\n * if (user !== null) {\n * console.log(`Name: ${user.name}, Email: ${user.email}`);\n * }\n * ```\n */\nexport const userGetById = query({\n args: { userId: v.id(\"User\") },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { userId }) => {\n return await ctx.db.get(\"User\", userId);\n },\n});\n\n/**\n * Find a user by their verified email address.\n *\n * Queries the `User` table using the `email_verified` index to locate users\n * whose `email` matches and whose `emailVerificationTime` is set. If exactly\n * one user is found, that document is returned. Returns `null` if no user has\n * this email verified or if multiple users share the same verified email\n * (an ambiguous state that should not occur in normal operation).\n *\n * @param args.email - The verified email address to search for (case-sensitive, exact match).\n * @returns The matching user document if exactly one verified user is found, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userFindByVerifiedEmail,\n * { email: \"alice@example.com\" },\n * );\n * if (user !== null) {\n * console.log(`Found verified user: ${user._id}`);\n * }\n * ```\n */\nexport const userFindByVerifiedEmail = query({\n args: { email: v.string() },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { email }) => {\n const users = await ctx.db\n .query(\"User\")\n .withIndex(\"email_verified\", (q) =>\n q.eq(\"email\", email).gt(\"emailVerificationTime\", undefined),\n )\n .take(2);\n return users.length === 1 ? users[0] : null;\n },\n});\n\n/**\n * Find a user by their verified phone number.\n *\n * Queries the `User` table using the `phone_verified` index to locate users\n * whose `phone` matches and whose `phoneVerificationTime` is set. If exactly\n * one user is found, that document is returned. Returns `null` if no user has\n * this phone verified or if multiple users share the same verified phone\n * (an ambiguous state that should not occur in normal operation).\n *\n * @param args.phone - The verified phone number to search for (exact match, e.g. `\"+15551234567\"`).\n * @returns The matching user document if exactly one verified user is found, or `null` otherwise.\n *\n * @example\n * ```ts\n * const user = await ctx.runQuery(\n * component.identity.users.userFindByVerifiedPhone,\n * { phone: \"+15551234567\" },\n * );\n * if (user !== null) {\n * console.log(`Found verified user: ${user._id}`);\n * }\n * ```\n */\nexport const userFindByVerifiedPhone = query({\n args: { phone: v.string() },\n returns: v.union(vUserDoc, v.null()),\n handler: async (ctx, { phone }) => {\n const users = await ctx.db\n .query(\"User\")\n .withIndex(\"phone_verified\", (q) =>\n q.eq(\"phone\", phone).gt(\"phoneVerificationTime\", undefined),\n )\n .take(2);\n return users.length === 1 ? users[0] : null;\n },\n});\n\n/**\n * Insert a new user document into the `User` table.\n *\n * Creates a brand-new user record. The `data` argument should conform to the\n * User table schema (e.g. `name`, `email`, `phone`, `isAnonymous`, `image`,\n * `extend`), but is typed as `any` to allow flexible extension.\n *\n * @param args.data - The user document fields to insert. Typically includes `name`,\n * `email`, `isAnonymous`, and any custom fields under `extend`.\n * @returns The document ID of the newly created user.\n *\n * @example\n * ```ts\n * const userId = await ctx.runMutation(\n * component.identity.users.userInsert,\n * {\n * data: {\n * name: \"Alice\",\n * email: \"alice@example.com\",\n * isAnonymous: false,\n * },\n * },\n * );\n * ```\n */\nexport const userInsert = mutation({\n args: { data: v.any() },\n returns: v.id(\"User\"),\n handler: async (ctx, { data }) => {\n return await ctx.db.insert(\"User\", data);\n },\n});\n\n/**\n * Insert a new user or update an existing one (upsert).\n *\n * When `userId` is provided and refers to an existing user, the document is\n * patched with the supplied `data` and the same `userId` is returned. When\n * `userId` is omitted or `undefined`, a new user document is inserted and its\n * generated ID is returned. This is the primary mechanism used during sign-in\n * flows to either create or refresh user profile data.\n *\n * @param args.userId - The document ID of an existing user to update. If `undefined`,\n * a new user is created instead.\n * @param args.data - The user document fields to insert or merge. Accepts the same\n * shape as the User table schema.\n * @returns The document ID of the created or updated user.\n *\n * @example\n * ```ts\n * // Create a new user if none exists, or update the existing one\n * const userId = await ctx.runMutation(\n * component.identity.users.userUpsert,\n * {\n * userId: existingUserId ?? undefined,\n * data: { name: \"Alice\", email: \"alice@example.com\" },\n * },\n * );\n * ```\n */\nexport const userUpsert = mutation({\n args: { userId: v.optional(v.id(\"User\")), data: v.any() },\n returns: v.id(\"User\"),\n handler: async (ctx, { userId, data }) => {\n if (userId !== undefined) {\n await ctx.db.patch(\"User\", userId, data);\n return userId;\n }\n return await ctx.db.insert(\"User\", data);\n },\n});\n\n/**\n * Patch an existing user document with partial data.\n *\n * Merges the provided fields into the existing user document. Fields not\n * included in `data` are left unchanged. Useful for updating profile\n * information such as `name`, `email`, or custom `extend` fields without\n * overwriting the entire document.\n *\n * @param args.userId - The document ID of the user to update.\n * @param args.data - A partial object containing the fields to merge into the user document.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * component.identity.users.userPatch,\n * {\n * userId: user._id,\n * data: { name: \"Alice Smith\", image: \"https://example.com/avatar.png\" },\n * },\n * );\n * ```\n */\nexport const userPatch = mutation({\n args: { userId: v.id(\"User\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { userId, data }) => {\n await ctx.db.patch(\"User\", userId, data);\n return null;\n },\n});\n\n/**\n * Delete a user document by ID.\n *\n * Removes the user from the `User` table. This is a no-op if the user does not\n * exist (i.e. was already deleted). Callers should ensure that related resources\n * such as accounts, sessions, and refresh tokens are cleaned up separately.\n *\n * @param args.userId - The document ID of the user to delete.\n * @returns `null` on success (including when the user was already absent).\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * component.identity.users.userDelete,\n * { userId: user._id },\n * );\n * ```\n */\nexport const userDelete = mutation({\n args: { userId: v.id(\"User\") },\n returns: v.null(),\n handler: async (ctx, { userId }) => {\n if ((await ctx.db.get(\"User\", userId)) !== null) {\n await ctx.db.delete(\"User\", userId);\n }\n return null;\n },\n});\n\n// ============================================================================\n// Accounts\n// ============================================================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,GAAG,OAAO;EAAE,MAAM,EAAE,KAAK;EAAE;CAC7C,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,QAAQ,WAAW;AACxC,QAAM,IAAI,GAAG,MAAM,QAAQ,QAAQ,KAAK;AACxC,SAAO;;CAEV,CAAC;;;;;;;;;;;;;;;;;;;AAoBF,MAAa,aAAa,SAAS;CACjC,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE;CAC9B,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,aAAa;AAClC,MAAK,MAAM,IAAI,GAAG,IAAI,QAAQ,OAAO,KAAM,KACzC,OAAM,IAAI,GAAG,OAAO,QAAQ,OAAO;AAErC,SAAO;;CAEV,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"verifiers.d.ts","names":[],"sources":["../../../../src/component/public/identity/verifiers.ts"],"mappings":";;;;;;;;;;;;;AAwBA;;;;;AA4BA;;;;;cA5Ba,cAAA;;;;;AA6Fb;;;;;AA4BA;;;;;;;;;;;cA7Fa,eAAA;;;;;;;;;;;;;;;;;;;;;;;cA8BA,sBAAA;;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,aAAA;;;;;;;;;;;;;;;;;;;;cA4BA,cAAA"}
1
+ {"version":3,"file":"verifiers.d.ts","names":[],"sources":["../../../../src/component/public/identity/verifiers.ts"],"mappings":";;;;;;;;;;;;;AAyBA;;;;;AA4BA;;;;;cA5Ba,cAAA;;;;;AA6Fb;;;;;AA4BA;;;;;;;;;;;cA7Fa,eAAA;;;;;;;;;;;;;;;;;;;;;;;cA8BA,sBAAA;;;;;;;;;;;;;;;;;;;;;;;;;cAmCA,aAAA;;;;;;;;;;;;;;;;;;;;cA4BA,cAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"verifiers.js","names":[],"sources":["../../../../src/component/public/identity/verifiers.ts"],"sourcesContent":["import { v } from \"convex/values\";\nimport { mutation, query } from \"../../functions\";\nimport { vAuthVerifierDoc } from \"../../model\";\n\n/**\n * Create a new PKCE verifier, optionally linked to a session.\n *\n * Inserts a document into the `AuthVerifier` table. Verifiers are used during\n * OAuth/OIDC flows to implement the PKCE (Proof Key for Code Exchange) pattern,\n * preventing authorization code interception attacks. The verifier can optionally\n * be linked to an existing session for session-aware flows.\n *\n * @param args.sessionId - An optional session document ID to associate with the verifier.\n * When provided, the verifier is scoped to the given session.\n * @returns The document ID of the newly created verifier.\n *\n * @example\n * ```ts\n * const verifierId = await ctx.runMutation(\n * component.identity.verifiers.verifierCreate,\n * { sessionId: session._id },\n * );\n * ```\n */\nexport const verifierCreate = mutation({\n args: { sessionId: v.optional(v.id(\"Session\")) },\n returns: v.id(\"AuthVerifier\"),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db.insert(\"AuthVerifier\", { sessionId: sessionId as any });\n },\n});\n\n/**\n * Retrieve a single verifier by its Convex document ID.\n *\n * Performs a direct point lookup on the `AuthVerifier` table. Returns `null` if\n * the verifier has been deleted or never existed.\n *\n * @param args.verifierId - The Convex document ID (`Id<\"AuthVerifier\">`) of the verifier to retrieve.\n * @returns The verifier document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const verifier = await ctx.runQuery(\n * component.identity.verifiers.verifierGetById,\n * { verifierId: storedVerifierId },\n * );\n * if (verifier !== null) {\n * console.log(`Verifier signature: ${verifier.signature}`);\n * }\n * ```\n */\nexport const verifierGetById = query({\n args: { verifierId: v.id(\"AuthVerifier\") },\n returns: v.union(vAuthVerifierDoc, v.null()),\n handler: async (ctx, { verifierId }) => {\n return await ctx.db.get(\"AuthVerifier\", verifierId);\n },\n});\n\n/**\n * Look up a verifier by its cryptographic signature.\n *\n * Queries the `AuthVerifier` table using the `signature` index to find the\n * unique verifier matching the given signature string. This is the primary\n * lookup used during the OAuth callback phase to correlate the incoming\n * authorization response with the original PKCE challenge.\n *\n * @param args.signature - The cryptographic signature string to search for (exact match).\n * @returns The matching verifier document, or `null` if no verifier has the given signature.\n *\n * @example\n * ```ts\n * const verifier = await ctx.runQuery(\n * component.identity.verifiers.verifierGetBySignature,\n * { signature: incomingStateParam },\n * );\n * if (verifier === null) {\n * throw new Error(\"Invalid or expired OAuth state\");\n * }\n * ```\n */\nexport const verifierGetBySignature = query({\n args: { signature: v.string() },\n returns: v.union(vAuthVerifierDoc, v.null()),\n handler: async (ctx, { signature }) => {\n return await ctx.db\n .query(\"AuthVerifier\")\n .withIndex(\"signature\", (q) => q.eq(\"signature\", signature))\n .unique();\n },\n});\n\n/**\n * Patch a verifier document with partial data.\n *\n * Merges the provided fields into the existing verifier document. This is\n * typically used to set the `signature` field after the verifier is initially\n * created, or to associate a `sessionId` with an existing verifier.\n *\n * @param args.verifierId - The document ID of the verifier to update.\n * @param args.data - A partial object containing the fields to merge into the verifier document\n * (e.g. `{ signature: string }` or `{ sessionId: Id<\"Session\"> }`).\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Set the PKCE signature on the verifier\n * await ctx.runMutation(\n * component.identity.verifiers.verifierPatch,\n * {\n * verifierId: verifier._id,\n * data: { signature: generatedSignature },\n * },\n * );\n * ```\n */\nexport const verifierPatch = mutation({\n args: { verifierId: v.id(\"AuthVerifier\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { verifierId, data }) => {\n await ctx.db.patch(\"AuthVerifier\", verifierId, data);\n return null;\n },\n});\n\n/**\n * Delete a verifier document permanently.\n *\n * Removes the verifier from the `AuthVerifier` table. This is typically called\n * after a successful OAuth callback to clean up the consumed PKCE state, or\n * to expire stale verifiers that were never completed.\n *\n * @param args.verifierId - The document ID of the verifier to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Clean up the verifier after a successful OAuth exchange\n * await ctx.runMutation(\n * component.identity.verifiers.verifierDelete,\n * { verifierId: verifier._id },\n * );\n * ```\n */\nexport const verifierDelete = mutation({\n args: { verifierId: v.id(\"AuthVerifier\") },\n returns: v.null(),\n handler: async (ctx, { verifierId }) => {\n await ctx.db.delete(\"AuthVerifier\", verifierId);\n return null;\n },\n});\n\n// ============================================================================\n// Verification Codes\n// ============================================================================\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,iBAAiB,SAAS;CACrC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,UAAU,CAAC,EAAE;CAChD,SAAS,EAAE,GAAG,eAAe;CAC7B,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GAAG,OAAO,gBAAgB,EAAa,WAAkB,CAAC;;CAE9E,CAAC;;;;;;;;;;;;;;;;;;;;;AAsBF,MAAa,kBAAkB,MAAM;CACnC,MAAM,EAAE,YAAY,EAAE,GAAG,eAAe,EAAE;CAC1C,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,iBAAiB;AACtC,SAAO,MAAM,IAAI,GAAG,IAAI,gBAAgB,WAAW;;CAEtD,CAAC;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,yBAAyB,MAAM;CAC1C,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE;CAC/B,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,eAAe,CACrB,UAAU,cAAc,MAAM,EAAE,GAAG,aAAa,UAAU,CAAC,CAC3D,QAAQ;;CAEd,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AA0BF,MAAa,gBAAgB,SAAS;CACpC,MAAM;EAAE,YAAY,EAAE,GAAG,eAAe;EAAE,MAAM,EAAE,KAAK;EAAE;CACzD,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,YAAY,WAAW;AAC5C,QAAM,IAAI,GAAG,MAAM,gBAAgB,YAAY,KAAK;AACpD,SAAO;;CAEV,CAAC;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,iBAAiB,SAAS;CACrC,MAAM,EAAE,YAAY,EAAE,GAAG,eAAe,EAAE;CAC1C,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,iBAAiB;AACtC,QAAM,IAAI,GAAG,OAAO,gBAAgB,WAAW;AAC/C,SAAO;;CAEV,CAAC"}
1
+ {"version":3,"file":"verifiers.js","names":[],"sources":["../../../../src/component/public/identity/verifiers.ts"],"sourcesContent":["import { v } from \"convex/values\";\n\nimport { mutation, query } from \"../../functions\";\nimport { vAuthVerifierDoc } from \"../../model\";\n\n/**\n * Create a new PKCE verifier, optionally linked to a session.\n *\n * Inserts a document into the `AuthVerifier` table. Verifiers are used during\n * OAuth/OIDC flows to implement the PKCE (Proof Key for Code Exchange) pattern,\n * preventing authorization code interception attacks. The verifier can optionally\n * be linked to an existing session for session-aware flows.\n *\n * @param args.sessionId - An optional session document ID to associate with the verifier.\n * When provided, the verifier is scoped to the given session.\n * @returns The document ID of the newly created verifier.\n *\n * @example\n * ```ts\n * const verifierId = await ctx.runMutation(\n * component.identity.verifiers.verifierCreate,\n * { sessionId: session._id },\n * );\n * ```\n */\nexport const verifierCreate = mutation({\n args: { sessionId: v.optional(v.id(\"Session\")) },\n returns: v.id(\"AuthVerifier\"),\n handler: async (ctx, { sessionId }) => {\n return await ctx.db.insert(\"AuthVerifier\", { sessionId: sessionId as any });\n },\n});\n\n/**\n * Retrieve a single verifier by its Convex document ID.\n *\n * Performs a direct point lookup on the `AuthVerifier` table. Returns `null` if\n * the verifier has been deleted or never existed.\n *\n * @param args.verifierId - The Convex document ID (`Id<\"AuthVerifier\">`) of the verifier to retrieve.\n * @returns The verifier document if it exists, or `null` otherwise.\n *\n * @example\n * ```ts\n * const verifier = await ctx.runQuery(\n * component.identity.verifiers.verifierGetById,\n * { verifierId: storedVerifierId },\n * );\n * if (verifier !== null) {\n * console.log(`Verifier signature: ${verifier.signature}`);\n * }\n * ```\n */\nexport const verifierGetById = query({\n args: { verifierId: v.id(\"AuthVerifier\") },\n returns: v.union(vAuthVerifierDoc, v.null()),\n handler: async (ctx, { verifierId }) => {\n return await ctx.db.get(\"AuthVerifier\", verifierId);\n },\n});\n\n/**\n * Look up a verifier by its cryptographic signature.\n *\n * Queries the `AuthVerifier` table using the `signature` index to find the\n * unique verifier matching the given signature string. This is the primary\n * lookup used during the OAuth callback phase to correlate the incoming\n * authorization response with the original PKCE challenge.\n *\n * @param args.signature - The cryptographic signature string to search for (exact match).\n * @returns The matching verifier document, or `null` if no verifier has the given signature.\n *\n * @example\n * ```ts\n * const verifier = await ctx.runQuery(\n * component.identity.verifiers.verifierGetBySignature,\n * { signature: incomingStateParam },\n * );\n * if (verifier === null) {\n * throw new Error(\"Invalid or expired OAuth state\");\n * }\n * ```\n */\nexport const verifierGetBySignature = query({\n args: { signature: v.string() },\n returns: v.union(vAuthVerifierDoc, v.null()),\n handler: async (ctx, { signature }) => {\n return await ctx.db\n .query(\"AuthVerifier\")\n .withIndex(\"signature\", (q) => q.eq(\"signature\", signature))\n .unique();\n },\n});\n\n/**\n * Patch a verifier document with partial data.\n *\n * Merges the provided fields into the existing verifier document. This is\n * typically used to set the `signature` field after the verifier is initially\n * created, or to associate a `sessionId` with an existing verifier.\n *\n * @param args.verifierId - The document ID of the verifier to update.\n * @param args.data - A partial object containing the fields to merge into the verifier document\n * (e.g. `{ signature: string }` or `{ sessionId: Id<\"Session\"> }`).\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Set the PKCE signature on the verifier\n * await ctx.runMutation(\n * component.identity.verifiers.verifierPatch,\n * {\n * verifierId: verifier._id,\n * data: { signature: generatedSignature },\n * },\n * );\n * ```\n */\nexport const verifierPatch = mutation({\n args: { verifierId: v.id(\"AuthVerifier\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { verifierId, data }) => {\n await ctx.db.patch(\"AuthVerifier\", verifierId, data);\n return null;\n },\n});\n\n/**\n * Delete a verifier document permanently.\n *\n * Removes the verifier from the `AuthVerifier` table. This is typically called\n * after a successful OAuth callback to clean up the consumed PKCE state, or\n * to expire stale verifiers that were never completed.\n *\n * @param args.verifierId - The document ID of the verifier to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Clean up the verifier after a successful OAuth exchange\n * await ctx.runMutation(\n * component.identity.verifiers.verifierDelete,\n * { verifierId: verifier._id },\n * );\n * ```\n */\nexport const verifierDelete = mutation({\n args: { verifierId: v.id(\"AuthVerifier\") },\n returns: v.null(),\n handler: async (ctx, { verifierId }) => {\n await ctx.db.delete(\"AuthVerifier\", verifierId);\n return null;\n },\n});\n\n// ============================================================================\n// Verification Codes\n// ============================================================================\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,iBAAiB,SAAS;CACrC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,UAAU,CAAC,EAAE;CAChD,SAAS,EAAE,GAAG,eAAe;CAC7B,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GAAG,OAAO,gBAAgB,EAAa,WAAkB,CAAC;;CAE9E,CAAC;;;;;;;;;;;;;;;;;;;;;AAsBF,MAAa,kBAAkB,MAAM;CACnC,MAAM,EAAE,YAAY,EAAE,GAAG,eAAe,EAAE;CAC1C,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,iBAAiB;AACtC,SAAO,MAAM,IAAI,GAAG,IAAI,gBAAgB,WAAW;;CAEtD,CAAC;;;;;;;;;;;;;;;;;;;;;;;AAwBF,MAAa,yBAAyB,MAAM;CAC1C,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE;CAC/B,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,eAAe,CACrB,UAAU,cAAc,MAAM,EAAE,GAAG,aAAa,UAAU,CAAC,CAC3D,QAAQ;;CAEd,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AA0BF,MAAa,gBAAgB,SAAS;CACpC,MAAM;EAAE,YAAY,EAAE,GAAG,eAAe;EAAE,MAAM,EAAE,KAAK;EAAE;CACzD,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,YAAY,WAAW;AAC5C,QAAM,IAAI,GAAG,MAAM,gBAAgB,YAAY,KAAK;AACpD,SAAO;;CAEV,CAAC;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,iBAAiB,SAAS;CACrC,MAAM,EAAE,YAAY,EAAE,GAAG,eAAe,EAAE;CAC1C,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,iBAAiB;AACtC,QAAM,IAAI,GAAG,OAAO,gBAAgB,WAAW;AAC/C,SAAO;;CAEV,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"keys.d.ts","names":[],"sources":["../../../../src/component/public/security/keys.ts"],"mappings":";;;;;;;;;;;;;;AAqDA;;;;;AAmDA;;;;;AAyDA;;;;;AAwFA;;;;;AAsDA;;;;;AA8CA;;;cAxSa,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;;cAmDA,iBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAyDA,OAAA;;;;;;;;;;;;;;;;;;;;;;;cAwFA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsDA,QAAA;;;;;;;;;;;;;;;;;;;;cA8CA,SAAA"}
1
+ {"version":3,"file":"keys.d.ts","names":[],"sources":["../../../../src/component/public/security/keys.ts"],"mappings":";;;;;;;;;;;;;;AAsDA;;;;;AAmDA;;;;;AAyDA;;;;;AAwFA;;;;;AAsDA;;;;;AA8CA;;;cAxSa,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;;cAmDA,iBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAyDA,OAAA;;;;;;;;;;;;;;;;;;;;;;;cAwFA,UAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAsDA,QAAA;;;;;;;;;;;;;;;;;;;;cA8CA,SAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"keys.js","names":[],"sources":["../../../../src/component/public/security/keys.ts"],"sourcesContent":["import { ConvexError, v } from \"convex/values\";\nimport { mutation, query } from \"../../functions\";\nimport {\n vApiKeyDoc,\n vApiKeyRateLimit,\n vApiKeyRateLimitState,\n vApiKeyScope,\n vPaginated,\n} from \"../../model\";\n\n// ============================================================================\n// API Keys\n// ============================================================================\n\n/**\n * Insert a new API key record into the `ApiKey` table.\n *\n * Creates an API key entry with the given metadata and scopes. The caller\n * is responsible for generating and hashing the raw key before passing it\n * here -- this function only stores the hash, never the plaintext key.\n * The `createdAt` timestamp and `revoked: false` flag are set automatically.\n *\n * @param userId - The `_id` of the `User` who owns this API key.\n * @param prefix - A short, visible prefix for the key (e.g. `\"sk_live_\"`)\n * that helps users identify which key was used without exposing the secret.\n * @param hashedKey - SHA-256 hash of the full API key string. Used for\n * constant-time lookup during Bearer token verification.\n * @param name - Human-readable name for the key (e.g. `\"Production Backend\"`).\n * @param scopes - Array of permission scopes, each containing a `resource`\n * name and an array of allowed `actions` (e.g.\n * `[{ resource: \"messages\", actions: [\"read\", \"write\"] }]`).\n * @param rateLimit - Optional rate limit configuration to apply per-key\n * (e.g. max requests per window).\n * @param expiresAt - Optional Unix timestamp (in milliseconds) after which\n * the key is no longer valid. Omit for non-expiring keys.\n * @param metadata - Optional arbitrary metadata to attach to the key record.\n * @returns The `_id` of the newly created `ApiKey` document.\n *\n * @example\n * ```ts\n * const keyId = await ctx.runMutation(\n * components.auth.security.keys.keyInsert,\n * {\n * userId: user._id,\n * prefix: \"sk_live_\",\n * hashedKey: await sha256(rawKey),\n * name: \"Production Backend\",\n * scopes: [{ resource: \"messages\", actions: [\"read\", \"write\"] }],\n * expiresAt: Date.now() + 90 * 24 * 60 * 60 * 1000,\n * },\n * );\n * ```\n */\nexport const keyInsert = mutation({\n args: {\n userId: v.id(\"User\"),\n prefix: v.string(),\n hashedKey: v.string(),\n name: v.string(),\n scopes: v.array(\n v.object({\n resource: v.string(),\n actions: v.array(v.string()),\n }),\n ),\n rateLimit: v.optional(vApiKeyRateLimit),\n expiresAt: v.optional(v.number()),\n metadata: v.optional(v.any()),\n },\n returns: v.id(\"ApiKey\"),\n handler: async (ctx, args) => {\n return await ctx.db.insert(\"ApiKey\", {\n ...args,\n createdAt: Date.now(),\n revoked: false,\n });\n },\n});\n\n/**\n * Look up an API key by its SHA-256 hash.\n *\n * Queries the `ApiKey` table using the `hashed_key` index. This is the\n * primary lookup path during Bearer token verification: the incoming\n * token is hashed and matched against stored hashes in constant time.\n * Returns the full key record including scopes, rate limit state, and\n * revocation status so the caller can perform authorization checks.\n *\n * @param hashedKey - SHA-256 hash of the API key string extracted from\n * the `Authorization: Bearer <token>` header.\n * @returns The matching `ApiKey` document (including rate limit state),\n * or `null` if no key matches the given hash.\n *\n * @example\n * ```ts\n * const apiKey = await ctx.runQuery(\n * components.auth.security.keys.keyGetByHashedKey,\n * { hashedKey: await sha256(bearerToken) },\n * );\n * if (apiKey === null || apiKey.revoked) {\n * throw new Error(\"Invalid or revoked API key\");\n * }\n * ```\n */\nexport const keyGetByHashedKey = query({\n args: { hashedKey: v.string() },\n returns: v.union(vApiKeyDoc, v.null()),\n handler: async (ctx, { hashedKey }) => {\n return await ctx.db\n .query(\"ApiKey\")\n .withIndex(\"hashed_key\", (q) => q.eq(\"hashedKey\", hashedKey))\n .first();\n },\n});\n\n/**\n * List API keys with optional filtering, sorting, and cursor-based pagination.\n *\n * Returns a paginated result `{ items, nextCursor }` from the `ApiKey`\n * table. Supports filtering by `userId`, `revoked` status, `name`, and\n * `prefix`. The page size is clamped between 1 and 100 (default 50).\n * Pass the returned `nextCursor` as `cursor` in a subsequent call to\n * fetch the next page.\n *\n * @param where - Optional filter object. All specified fields are\n * combined with AND logic:\n * - `userId` -- restrict to keys owned by this user.\n * - `revoked` -- `true` for revoked keys, `false` for active keys.\n * - `name` -- exact match on the key's human-readable name.\n * - `prefix` -- exact match on the key prefix string.\n * @param limit - Maximum number of items to return per page (1--100,\n * default `50`).\n * @param cursor - Opaque cursor string (an `ApiKey` document `_id`)\n * returned from a previous call. Pass `null` or omit for the first page.\n * @param orderBy - Field to sort by. One of `\"_creationTime\"`, `\"name\"`,\n * `\"lastUsedAt\"`, `\"expiresAt\"`, or `\"revoked\"`. Defaults to\n * `\"_creationTime\"`.\n * @param order - Sort direction, `\"asc\"` or `\"desc\"` (default `\"desc\"`).\n * @returns An object with `items` (array of `ApiKey` documents) and\n * `nextCursor` (string ID of the last item, or `null` if no more pages).\n *\n * @example\n * ```ts\n * // Fetch the first page of active keys for a user\n * const page = await ctx.runQuery(\n * components.auth.security.keys.keyList,\n * {\n * where: { userId: user._id, revoked: false },\n * limit: 20,\n * order: \"desc\",\n * },\n * );\n * // Fetch the next page\n * if (page.nextCursor) {\n * const page2 = await ctx.runQuery(\n * components.auth.security.keys.keyList,\n * { where: { userId: user._id, revoked: false }, cursor: page.nextCursor },\n * );\n * }\n * ```\n */\nexport const keyList = query({\n args: {\n where: v.optional(\n v.object({\n userId: v.optional(v.id(\"User\")),\n revoked: v.optional(v.boolean()),\n name: v.optional(v.string()),\n prefix: v.optional(v.string()),\n }),\n ),\n limit: v.optional(v.number()),\n cursor: v.optional(v.union(v.string(), v.null())),\n orderBy: v.optional(\n v.union(\n v.literal(\"_creationTime\"),\n v.literal(\"name\"),\n v.literal(\"lastUsedAt\"),\n v.literal(\"expiresAt\"),\n v.literal(\"revoked\"),\n ),\n ),\n order: v.optional(v.union(v.literal(\"asc\"), v.literal(\"desc\"))),\n },\n returns: vPaginated(vApiKeyDoc),\n handler: async (ctx, args) => {\n const where = args.where ?? {};\n const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);\n const order = args.order ?? \"desc\";\n\n let q;\n if (where.userId !== undefined) {\n q = ctx.db\n .query(\"ApiKey\")\n .withIndex(\"user_id\", (idx) => idx.eq(\"userId\", where.userId!));\n } else {\n q = ctx.db.query(\"ApiKey\");\n }\n\n if (where.revoked !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"revoked\"), where.revoked!));\n }\n if (where.name !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"name\"), where.name!));\n }\n if (where.prefix !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"prefix\"), where.prefix!));\n }\n\n q = q.order(order);\n\n const all = await q.collect();\n let startIdx = 0;\n if (args.cursor) {\n const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);\n if (cursorIdx !== -1) {\n startIdx = cursorIdx + 1;\n }\n }\n const page = all.slice(startIdx, startIdx + limit + 1);\n const hasMore = page.length > limit;\n const items = hasMore ? page.slice(0, limit) : page;\n const nextCursor = hasMore ? items[items.length - 1]._id : null;\n return { items, nextCursor };\n },\n});\n\n/**\n * Get a single API key by its document ID.\n *\n * Performs a direct document lookup on the `ApiKey` table. Useful when\n * you already have the key's `_id` (e.g. from a list query or a stored\n * reference) and need to retrieve its full details.\n *\n * @param keyId - The `_id` of the `ApiKey` document to retrieve.\n * @returns The `ApiKey` document, or `null` if no key exists with the\n * given ID.\n *\n * @example\n * ```ts\n * const apiKey = await ctx.runQuery(\n * components.auth.security.keys.keyGetById,\n * { keyId: storedKeyId },\n * );\n * if (apiKey !== null) {\n * console.log(apiKey.name, apiKey.scopes);\n * }\n * ```\n */\nexport const keyGetById = query({\n args: { keyId: v.id(\"ApiKey\") },\n returns: v.union(vApiKeyDoc, v.null()),\n handler: async (ctx, { keyId }) => {\n return await ctx.db.get(\"ApiKey\", keyId);\n },\n});\n\n/**\n * Patch an API key record with partial updates.\n *\n * Performs a partial update on the `ApiKey` document. Supports modifying\n * the key's name, scopes, rate limit configuration, rate limit state,\n * revocation flag, and last-used timestamp. Throws a `ConvexError` with\n * code `\"KEY_NOT_FOUND\"` if the key does not exist.\n *\n * @param keyId - The `_id` of the `ApiKey` document to update.\n * @param data - An object containing the fields to patch. All fields are\n * optional:\n * - `name` -- Updated human-readable name.\n * - `scopes` -- Replacement array of permission scopes.\n * - `rateLimit` -- Updated rate limit configuration.\n * - `rateLimitState` -- Updated rate limit tracking state (token\n * count, last refill time).\n * - `revoked` -- Set to `true` to revoke the key, `false` to\n * reinstate it.\n * - `lastUsedAt` -- Unix timestamp (in milliseconds) of the most\n * recent API call using this key.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Revoke an API key\n * await ctx.runMutation(\n * components.auth.security.keys.keyPatch,\n * {\n * keyId: apiKey._id,\n * data: { revoked: true },\n * },\n * );\n *\n * // Rename and update scopes\n * await ctx.runMutation(\n * components.auth.security.keys.keyPatch,\n * {\n * keyId: apiKey._id,\n * data: {\n * name: \"Read-Only Key\",\n * scopes: [{ resource: \"messages\", actions: [\"read\"] }],\n * },\n * },\n * );\n * ```\n */\nexport const keyPatch = mutation({\n args: {\n keyId: v.id(\"ApiKey\"),\n data: v.object({\n name: v.optional(v.string()),\n scopes: v.optional(v.array(vApiKeyScope)),\n rateLimit: v.optional(vApiKeyRateLimit),\n rateLimitState: v.optional(vApiKeyRateLimitState),\n revoked: v.optional(v.boolean()),\n lastUsedAt: v.optional(v.number()),\n }),\n },\n returns: v.null(),\n handler: async (ctx, { keyId, data }) => {\n const key = await ctx.db.get(\"ApiKey\", keyId);\n if (key === null) {\n throw new ConvexError({\n code: \"KEY_NOT_FOUND\",\n message: \"API key not found\",\n keyId,\n });\n }\n await ctx.db.patch(\"ApiKey\", keyId, data);\n return null;\n },\n});\n\n/**\n * Hard-delete an API key record from the `ApiKey` table.\n *\n * Permanently removes the API key document. Unlike revocation (which\n * keeps the record for audit purposes), this is an irreversible\n * deletion. Throws a `ConvexError` with code `\"KEY_NOT_FOUND\"` if the\n * key does not exist.\n *\n * @param keyId - The `_id` of the `ApiKey` document to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * components.auth.security.keys.keyDelete,\n * { keyId: apiKey._id },\n * );\n * ```\n */\nexport const keyDelete = mutation({\n args: { keyId: v.id(\"ApiKey\") },\n returns: v.null(),\n handler: async (ctx, { keyId }) => {\n const key = await ctx.db.get(\"ApiKey\", keyId);\n if (key === null) {\n throw new ConvexError({\n code: \"KEY_NOT_FOUND\",\n message: \"API key not found\",\n keyId,\n });\n }\n await ctx.db.delete(\"ApiKey\", keyId);\n return null;\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDA,MAAa,YAAY,SAAS;CAChC,MAAM;EACJ,QAAQ,EAAE,GAAG,OAAO;EACpB,QAAQ,EAAE,QAAQ;EAClB,WAAW,EAAE,QAAQ;EACrB,MAAM,EAAE,QAAQ;EAChB,QAAQ,EAAE,MACR,EAAE,OAAO;GACP,UAAU,EAAE,QAAQ;GACpB,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC;GAC7B,CAAC,CACH;EACD,WAAW,EAAE,SAAS,iBAAiB;EACvC,WAAW,EAAE,SAAS,EAAE,QAAQ,CAAC;EACjC,UAAU,EAAE,SAAS,EAAE,KAAK,CAAC;EAC9B;CACD,SAAS,EAAE,GAAG,SAAS;CACvB,SAAS,OAAO,KAAK,SAAS;AAC5B,SAAO,MAAM,IAAI,GAAG,OAAO,UAAU;GACnC,GAAG;GACH,WAAW,KAAK,KAAK;GACrB,SAAS;GACV,CAAC;;CAEL,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BF,MAAa,oBAAoB,MAAM;CACrC,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE;CAC/B,SAAS,EAAE,MAAM,YAAY,EAAE,MAAM,CAAC;CACtC,SAAS,OAAO,KAAK,EAAE,gBAAgB;AACrC,SAAO,MAAM,IAAI,GACd,MAAM,SAAS,CACf,UAAU,eAAe,MAAM,EAAE,GAAG,aAAa,UAAU,CAAC,CAC5D,OAAO;;CAEb,CAAC;;;;;;;;;;;;;;;;;;;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AS,EAAE,QAAQ,CAAC;GACnC,CAAC;EACH;CACD,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,OAAO,WAAW;AAEvC,MADY,MAAM,IAAI,GAAG,IAAI,UAAU,MAAM,KACjC,KACV,OAAM,IAAI,YAAY;GACpB,MAAM;GACN,SAAS;GACT;GACD,CAAC;AAEJ,QAAM,IAAI,GAAG,MAAM,UAAU,OAAO,KAAK;AACzC,SAAO;;CAEV,CAAC;;;;;;;;;;;;;;;;;;;;AAqBF,MAAa,YAAY,SAAS;CAChC,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,EAAE;CAC/B,SAAS,EAAE,MAAM;CACjB,SAAS,OAAO,KAAK,EAAE,YAAY;AAEjC,MADY,MAAM,IAAI,GAAG,IAAI,UAAU,MAAM,KACjC,KACV,OAAM,IAAI,YAAY;GACpB,MAAM;GACN,SAAS;GACT;GACD,CAAC;AAEJ,QAAM,IAAI,GAAG,OAAO,UAAU,MAAM;AACpC,SAAO;;CAEV,CAAC"}
1
+ {"version":3,"file":"keys.js","names":[],"sources":["../../../../src/component/public/security/keys.ts"],"sourcesContent":["import { ConvexError, v } from \"convex/values\";\n\nimport { mutation, query } from \"../../functions\";\nimport {\n vApiKeyDoc,\n vApiKeyRateLimit,\n vApiKeyRateLimitState,\n vApiKeyScope,\n vPaginated,\n} from \"../../model\";\n\n// ============================================================================\n// API Keys\n// ============================================================================\n\n/**\n * Insert a new API key record into the `ApiKey` table.\n *\n * Creates an API key entry with the given metadata and scopes. The caller\n * is responsible for generating and hashing the raw key before passing it\n * here -- this function only stores the hash, never the plaintext key.\n * The `createdAt` timestamp and `revoked: false` flag are set automatically.\n *\n * @param userId - The `_id` of the `User` who owns this API key.\n * @param prefix - A short, visible prefix for the key (e.g. `\"sk_live_\"`)\n * that helps users identify which key was used without exposing the secret.\n * @param hashedKey - SHA-256 hash of the full API key string. Used for\n * constant-time lookup during Bearer token verification.\n * @param name - Human-readable name for the key (e.g. `\"Production Backend\"`).\n * @param scopes - Array of permission scopes, each containing a `resource`\n * name and an array of allowed `actions` (e.g.\n * `[{ resource: \"messages\", actions: [\"read\", \"write\"] }]`).\n * @param rateLimit - Optional rate limit configuration to apply per-key\n * (e.g. max requests per window).\n * @param expiresAt - Optional Unix timestamp (in milliseconds) after which\n * the key is no longer valid. Omit for non-expiring keys.\n * @param metadata - Optional arbitrary metadata to attach to the key record.\n * @returns The `_id` of the newly created `ApiKey` document.\n *\n * @example\n * ```ts\n * const keyId = await ctx.runMutation(\n * components.auth.security.keys.keyInsert,\n * {\n * userId: user._id,\n * prefix: \"sk_live_\",\n * hashedKey: await sha256(rawKey),\n * name: \"Production Backend\",\n * scopes: [{ resource: \"messages\", actions: [\"read\", \"write\"] }],\n * expiresAt: Date.now() + 90 * 24 * 60 * 60 * 1000,\n * },\n * );\n * ```\n */\nexport const keyInsert = mutation({\n args: {\n userId: v.id(\"User\"),\n prefix: v.string(),\n hashedKey: v.string(),\n name: v.string(),\n scopes: v.array(\n v.object({\n resource: v.string(),\n actions: v.array(v.string()),\n }),\n ),\n rateLimit: v.optional(vApiKeyRateLimit),\n expiresAt: v.optional(v.number()),\n metadata: v.optional(v.any()),\n },\n returns: v.id(\"ApiKey\"),\n handler: async (ctx, args) => {\n return await ctx.db.insert(\"ApiKey\", {\n ...args,\n createdAt: Date.now(),\n revoked: false,\n });\n },\n});\n\n/**\n * Look up an API key by its SHA-256 hash.\n *\n * Queries the `ApiKey` table using the `hashed_key` index. This is the\n * primary lookup path during Bearer token verification: the incoming\n * token is hashed and matched against stored hashes in constant time.\n * Returns the full key record including scopes, rate limit state, and\n * revocation status so the caller can perform authorization checks.\n *\n * @param hashedKey - SHA-256 hash of the API key string extracted from\n * the `Authorization: Bearer <token>` header.\n * @returns The matching `ApiKey` document (including rate limit state),\n * or `null` if no key matches the given hash.\n *\n * @example\n * ```ts\n * const apiKey = await ctx.runQuery(\n * components.auth.security.keys.keyGetByHashedKey,\n * { hashedKey: await sha256(bearerToken) },\n * );\n * if (apiKey === null || apiKey.revoked) {\n * throw new Error(\"Invalid or revoked API key\");\n * }\n * ```\n */\nexport const keyGetByHashedKey = query({\n args: { hashedKey: v.string() },\n returns: v.union(vApiKeyDoc, v.null()),\n handler: async (ctx, { hashedKey }) => {\n return await ctx.db\n .query(\"ApiKey\")\n .withIndex(\"hashed_key\", (q) => q.eq(\"hashedKey\", hashedKey))\n .first();\n },\n});\n\n/**\n * List API keys with optional filtering, sorting, and cursor-based pagination.\n *\n * Returns a paginated result `{ items, nextCursor }` from the `ApiKey`\n * table. Supports filtering by `userId`, `revoked` status, `name`, and\n * `prefix`. The page size is clamped between 1 and 100 (default 50).\n * Pass the returned `nextCursor` as `cursor` in a subsequent call to\n * fetch the next page.\n *\n * @param where - Optional filter object. All specified fields are\n * combined with AND logic:\n * - `userId` -- restrict to keys owned by this user.\n * - `revoked` -- `true` for revoked keys, `false` for active keys.\n * - `name` -- exact match on the key's human-readable name.\n * - `prefix` -- exact match on the key prefix string.\n * @param limit - Maximum number of items to return per page (1--100,\n * default `50`).\n * @param cursor - Opaque cursor string (an `ApiKey` document `_id`)\n * returned from a previous call. Pass `null` or omit for the first page.\n * @param orderBy - Field to sort by. One of `\"_creationTime\"`, `\"name\"`,\n * `\"lastUsedAt\"`, `\"expiresAt\"`, or `\"revoked\"`. Defaults to\n * `\"_creationTime\"`.\n * @param order - Sort direction, `\"asc\"` or `\"desc\"` (default `\"desc\"`).\n * @returns An object with `items` (array of `ApiKey` documents) and\n * `nextCursor` (string ID of the last item, or `null` if no more pages).\n *\n * @example\n * ```ts\n * // Fetch the first page of active keys for a user\n * const page = await ctx.runQuery(\n * components.auth.security.keys.keyList,\n * {\n * where: { userId: user._id, revoked: false },\n * limit: 20,\n * order: \"desc\",\n * },\n * );\n * // Fetch the next page\n * if (page.nextCursor) {\n * const page2 = await ctx.runQuery(\n * components.auth.security.keys.keyList,\n * { where: { userId: user._id, revoked: false }, cursor: page.nextCursor },\n * );\n * }\n * ```\n */\nexport const keyList = query({\n args: {\n where: v.optional(\n v.object({\n userId: v.optional(v.id(\"User\")),\n revoked: v.optional(v.boolean()),\n name: v.optional(v.string()),\n prefix: v.optional(v.string()),\n }),\n ),\n limit: v.optional(v.number()),\n cursor: v.optional(v.union(v.string(), v.null())),\n orderBy: v.optional(\n v.union(\n v.literal(\"_creationTime\"),\n v.literal(\"name\"),\n v.literal(\"lastUsedAt\"),\n v.literal(\"expiresAt\"),\n v.literal(\"revoked\"),\n ),\n ),\n order: v.optional(v.union(v.literal(\"asc\"), v.literal(\"desc\"))),\n },\n returns: vPaginated(vApiKeyDoc),\n handler: async (ctx, args) => {\n const where = args.where ?? {};\n const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);\n const order = args.order ?? \"desc\";\n\n let q;\n if (where.userId !== undefined) {\n q = ctx.db\n .query(\"ApiKey\")\n .withIndex(\"user_id\", (idx) => idx.eq(\"userId\", where.userId!));\n } else {\n q = ctx.db.query(\"ApiKey\");\n }\n\n if (where.revoked !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"revoked\"), where.revoked!));\n }\n if (where.name !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"name\"), where.name!));\n }\n if (where.prefix !== undefined) {\n q = q.filter((f) => f.eq(f.field(\"prefix\"), where.prefix!));\n }\n\n q = q.order(order);\n\n const all = await q.collect();\n let startIdx = 0;\n if (args.cursor) {\n const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);\n if (cursorIdx !== -1) {\n startIdx = cursorIdx + 1;\n }\n }\n const page = all.slice(startIdx, startIdx + limit + 1);\n const hasMore = page.length > limit;\n const items = hasMore ? page.slice(0, limit) : page;\n const nextCursor = hasMore ? items[items.length - 1]._id : null;\n return { items, nextCursor };\n },\n});\n\n/**\n * Get a single API key by its document ID.\n *\n * Performs a direct document lookup on the `ApiKey` table. Useful when\n * you already have the key's `_id` (e.g. from a list query or a stored\n * reference) and need to retrieve its full details.\n *\n * @param keyId - The `_id` of the `ApiKey` document to retrieve.\n * @returns The `ApiKey` document, or `null` if no key exists with the\n * given ID.\n *\n * @example\n * ```ts\n * const apiKey = await ctx.runQuery(\n * components.auth.security.keys.keyGetById,\n * { keyId: storedKeyId },\n * );\n * if (apiKey !== null) {\n * console.log(apiKey.name, apiKey.scopes);\n * }\n * ```\n */\nexport const keyGetById = query({\n args: { keyId: v.id(\"ApiKey\") },\n returns: v.union(vApiKeyDoc, v.null()),\n handler: async (ctx, { keyId }) => {\n return await ctx.db.get(\"ApiKey\", keyId);\n },\n});\n\n/**\n * Patch an API key record with partial updates.\n *\n * Performs a partial update on the `ApiKey` document. Supports modifying\n * the key's name, scopes, rate limit configuration, rate limit state,\n * revocation flag, and last-used timestamp. Throws a `ConvexError` with\n * code `\"KEY_NOT_FOUND\"` if the key does not exist.\n *\n * @param keyId - The `_id` of the `ApiKey` document to update.\n * @param data - An object containing the fields to patch. All fields are\n * optional:\n * - `name` -- Updated human-readable name.\n * - `scopes` -- Replacement array of permission scopes.\n * - `rateLimit` -- Updated rate limit configuration.\n * - `rateLimitState` -- Updated rate limit tracking state (token\n * count, last refill time).\n * - `revoked` -- Set to `true` to revoke the key, `false` to\n * reinstate it.\n * - `lastUsedAt` -- Unix timestamp (in milliseconds) of the most\n * recent API call using this key.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Revoke an API key\n * await ctx.runMutation(\n * components.auth.security.keys.keyPatch,\n * {\n * keyId: apiKey._id,\n * data: { revoked: true },\n * },\n * );\n *\n * // Rename and update scopes\n * await ctx.runMutation(\n * components.auth.security.keys.keyPatch,\n * {\n * keyId: apiKey._id,\n * data: {\n * name: \"Read-Only Key\",\n * scopes: [{ resource: \"messages\", actions: [\"read\"] }],\n * },\n * },\n * );\n * ```\n */\nexport const keyPatch = mutation({\n args: {\n keyId: v.id(\"ApiKey\"),\n data: v.object({\n name: v.optional(v.string()),\n scopes: v.optional(v.array(vApiKeyScope)),\n rateLimit: v.optional(vApiKeyRateLimit),\n rateLimitState: v.optional(vApiKeyRateLimitState),\n revoked: v.optional(v.boolean()),\n lastUsedAt: v.optional(v.number()),\n }),\n },\n returns: v.null(),\n handler: async (ctx, { keyId, data }) => {\n const key = await ctx.db.get(\"ApiKey\", keyId);\n if (key === null) {\n throw new ConvexError({\n code: \"KEY_NOT_FOUND\",\n message: \"API key not found\",\n keyId,\n });\n }\n await ctx.db.patch(\"ApiKey\", keyId, data);\n return null;\n },\n});\n\n/**\n * Hard-delete an API key record from the `ApiKey` table.\n *\n * Permanently removes the API key document. Unlike revocation (which\n * keeps the record for audit purposes), this is an irreversible\n * deletion. Throws a `ConvexError` with code `\"KEY_NOT_FOUND\"` if the\n * key does not exist.\n *\n * @param keyId - The `_id` of the `ApiKey` document to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * await ctx.runMutation(\n * components.auth.security.keys.keyDelete,\n * { keyId: apiKey._id },\n * );\n * ```\n */\nexport const keyDelete = mutation({\n args: { keyId: v.id(\"ApiKey\") },\n returns: v.null(),\n handler: async (ctx, { keyId }) => {\n const key = await ctx.db.get(\"ApiKey\", keyId);\n if (key === null) {\n throw new ConvexError({\n code: \"KEY_NOT_FOUND\",\n message: \"API key not found\",\n keyId,\n });\n }\n await ctx.db.delete(\"ApiKey\", keyId);\n return null;\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsDA,MAAa,YAAY,SAAS;CAChC,MAAM;EACJ,QAAQ,EAAE,GAAG,OAAO;EACpB,QAAQ,EAAE,QAAQ;EAClB,WAAW,EAAE,QAAQ;EACrB,MAAM,EAAE,QAAQ;EAChB,QAAQ,EAAE,MACR,EAAE,OAAO;GACP,UAAU,EAAE,QAAQ;GACpB,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC;GAC7B,CAAC,CACH;EACD,WAAW,EAAE,SAAS,iBAAiB;EACvC,WAAW,EAAE,SAAS,EAAE,QAAQ,CAAC;EACjC,UAAU,EAAE,SAAS,EAAE,KAAK,CAAC;EAC9B;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;;;;AA4BA;;;;;AAgDA;;;;;AA+CA;;;;;cA/Fa,YAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgDA,eAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA+CA,cAAA;;;;;;;;;;;;;;;;;;;;;cAsCA,eAAA"}
1
+ {"version":3,"file":"limits.d.ts","names":[],"sources":["../../../../src/component/public/security/limits.ts"],"mappings":";;;;;;;;;;;;AA6BA;;;;;AAgDA;;;;;AA+CA;;;;;cA/Fa,YAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgDA,eAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA+CA,cAAA;;;;;;;;;;;;;;;;;;;;;cAsCA,eAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"limits.js","names":[],"sources":["../../../../src/component/public/security/limits.ts"],"sourcesContent":["import { v } from \"convex/values\";\nimport { mutation, query } from \"../../functions\";\nimport { vRateLimitResult } from \"../../model\";\n\n/**\n * Look up a rate limit entry by its string identifier.\n *\n * Queries the `RateLimit` table using the `by_identifier` unique index.\n * Returns the rate limit state with camelCase field names (`attemptsLeft`,\n * `lastAttemptTime`) mapped from the snake_case storage format. Used to\n * check whether an action should be allowed or throttled.\n *\n * @param identifier - Unique string identifying the rate limit bucket\n * (e.g. `\"login:user@example.com\"` or `\"api:sk_live_abc123\"`).\n * @returns The rate limit state object (including `attemptsLeft` and\n * `lastAttemptTime`), or `null` if no entry exists for the identifier.\n *\n * @example\n * ```ts\n * const limit = await ctx.runQuery(\n * components.auth.security.limits.rateLimitGet,\n * { identifier: `login:${email}` },\n * );\n * if (limit !== null && limit.attemptsLeft <= 0) {\n * throw new Error(\"Too many login attempts. Please try again later.\");\n * }\n * ```\n */\nexport const rateLimitGet = query({\n args: { identifier: v.string() },\n returns: v.union(vRateLimitResult, v.null()),\n handler: async (ctx, { identifier }) => {\n const row = await ctx.db\n .query(\"RateLimit\")\n .withIndex(\"by_identifier\", (q) => q.eq(\"identifier\", identifier))\n .unique();\n if (row === null) {\n return null;\n }\n return {\n ...row,\n attemptsLeft: row.attempts_left,\n lastAttemptTime: row.last_attempt_time,\n };\n },\n});\n\n/**\n * Create a new rate limit entry in the `RateLimit` table.\n *\n * Initializes a rate limit bucket for a given identifier. The entry\n * tracks remaining attempts and the timestamp of the last attempt,\n * storing them in snake_case format internally. Call this when the\n * first rate-limited action occurs for an identifier that does not\n * yet have an entry.\n *\n * @param identifier - Unique string identifying the rate limit bucket\n * (e.g. `\"login:user@example.com\"` or `\"otp:+15551234567\"`).\n * @param attemptsLeft - Number of remaining attempts before the action\n * is throttled.\n * @param lastAttemptTime - Unix timestamp (in milliseconds) of the\n * initial attempt.\n * @returns The `_id` of the newly created `RateLimit` document.\n *\n * @example\n * ```ts\n * const rateLimitId = await ctx.runMutation(\n * components.auth.security.limits.rateLimitCreate,\n * {\n * identifier: `login:${email}`,\n * attemptsLeft: 4, // 5 max minus this attempt\n * lastAttemptTime: Date.now(),\n * },\n * );\n * ```\n */\nexport const rateLimitCreate = mutation({\n args: {\n identifier: v.string(),\n attemptsLeft: v.number(),\n lastAttemptTime: v.number(),\n },\n returns: v.id(\"RateLimit\"),\n handler: async (ctx, { identifier, attemptsLeft, lastAttemptTime }) => {\n return await ctx.db.insert(\"RateLimit\", {\n identifier,\n attempts_left: attemptsLeft,\n last_attempt_time: lastAttemptTime,\n });\n },\n});\n\n/**\n * Patch a rate limit entry with partial data.\n *\n * Updates an existing `RateLimit` document with the provided fields.\n * Automatically maps camelCase field names (`attemptsLeft`,\n * `lastAttemptTime`) to the snake_case storage format before writing.\n * Typically called to decrement remaining attempts or to reset the\n * bucket after a cooldown window has elapsed.\n *\n * @param rateLimitId - The `_id` of the `RateLimit` document to update.\n * @param data - An object containing the fields to patch. Supports\n * camelCase names which are transparently converted:\n * - `attemptsLeft` -- Updated number of remaining attempts.\n * - `lastAttemptTime` -- Updated timestamp of the most recent attempt.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Decrement attempts after a failed login\n * await ctx.runMutation(\n * components.auth.security.limits.rateLimitPatch,\n * {\n * rateLimitId: limit._id,\n * data: {\n * attemptsLeft: limit.attemptsLeft - 1,\n * lastAttemptTime: Date.now(),\n * },\n * },\n * );\n * ```\n */\nexport const rateLimitPatch = mutation({\n args: { rateLimitId: v.id(\"RateLimit\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { rateLimitId, data }) => {\n const nextData: Record<string, unknown> = { ...data };\n if (nextData.attemptsLeft !== undefined) {\n nextData.attempts_left = nextData.attemptsLeft;\n delete nextData.attemptsLeft;\n }\n if (nextData.lastAttemptTime !== undefined) {\n nextData.last_attempt_time = nextData.lastAttemptTime;\n delete nextData.lastAttemptTime;\n }\n await ctx.db.patch(\"RateLimit\", rateLimitId, nextData);\n return null;\n },\n});\n\n/**\n * Delete a rate limit entry from the `RateLimit` table.\n *\n * Permanently removes the rate limit bucket. This effectively resets\n * rate limiting for the associated identifier, allowing the next\n * action to proceed without throttling. Useful for administrative\n * resets or cleanup of expired buckets.\n *\n * @param rateLimitId - The `_id` of the `RateLimit` document to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Admin resets a user's login rate limit\n * await ctx.runMutation(\n * components.auth.security.limits.rateLimitDelete,\n * { rateLimitId: limit._id },\n * );\n * ```\n */\nexport const rateLimitDelete = mutation({\n args: { rateLimitId: v.id(\"RateLimit\") },\n returns: v.null(),\n handler: async (ctx, { rateLimitId }) => {\n await ctx.db.delete(\"RateLimit\", rateLimitId);\n return null;\n },\n});\n\n// ============================================================================\n// Device Authorization (RFC 8628)\n// ============================================================================\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAa,eAAe,MAAM;CAChC,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE;CAChC,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,iBAAiB;EACtC,MAAM,MAAM,MAAM,IAAI,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CAAC"}
1
+ {"version":3,"file":"limits.js","names":[],"sources":["../../../../src/component/public/security/limits.ts"],"sourcesContent":["import { v } from \"convex/values\";\n\nimport { mutation, query } from \"../../functions\";\nimport { vRateLimitResult } from \"../../model\";\n\n/**\n * Look up a rate limit entry by its string identifier.\n *\n * Queries the `RateLimit` table using the `by_identifier` unique index.\n * Returns the rate limit state with camelCase field names (`attemptsLeft`,\n * `lastAttemptTime`) mapped from the snake_case storage format. Used to\n * check whether an action should be allowed or throttled.\n *\n * @param identifier - Unique string identifying the rate limit bucket\n * (e.g. `\"login:user@example.com\"` or `\"api:sk_live_abc123\"`).\n * @returns The rate limit state object (including `attemptsLeft` and\n * `lastAttemptTime`), or `null` if no entry exists for the identifier.\n *\n * @example\n * ```ts\n * const limit = await ctx.runQuery(\n * components.auth.security.limits.rateLimitGet,\n * { identifier: `login:${email}` },\n * );\n * if (limit !== null && limit.attemptsLeft <= 0) {\n * throw new Error(\"Too many login attempts. Please try again later.\");\n * }\n * ```\n */\nexport const rateLimitGet = query({\n args: { identifier: v.string() },\n returns: v.union(vRateLimitResult, v.null()),\n handler: async (ctx, { identifier }) => {\n const row = await ctx.db\n .query(\"RateLimit\")\n .withIndex(\"by_identifier\", (q) => q.eq(\"identifier\", identifier))\n .unique();\n if (row === null) {\n return null;\n }\n return {\n ...row,\n attemptsLeft: row.attempts_left,\n lastAttemptTime: row.last_attempt_time,\n };\n },\n});\n\n/**\n * Create a new rate limit entry in the `RateLimit` table.\n *\n * Initializes a rate limit bucket for a given identifier. The entry\n * tracks remaining attempts and the timestamp of the last attempt,\n * storing them in snake_case format internally. Call this when the\n * first rate-limited action occurs for an identifier that does not\n * yet have an entry.\n *\n * @param identifier - Unique string identifying the rate limit bucket\n * (e.g. `\"login:user@example.com\"` or `\"otp:+15551234567\"`).\n * @param attemptsLeft - Number of remaining attempts before the action\n * is throttled.\n * @param lastAttemptTime - Unix timestamp (in milliseconds) of the\n * initial attempt.\n * @returns The `_id` of the newly created `RateLimit` document.\n *\n * @example\n * ```ts\n * const rateLimitId = await ctx.runMutation(\n * components.auth.security.limits.rateLimitCreate,\n * {\n * identifier: `login:${email}`,\n * attemptsLeft: 4, // 5 max minus this attempt\n * lastAttemptTime: Date.now(),\n * },\n * );\n * ```\n */\nexport const rateLimitCreate = mutation({\n args: {\n identifier: v.string(),\n attemptsLeft: v.number(),\n lastAttemptTime: v.number(),\n },\n returns: v.id(\"RateLimit\"),\n handler: async (ctx, { identifier, attemptsLeft, lastAttemptTime }) => {\n return await ctx.db.insert(\"RateLimit\", {\n identifier,\n attempts_left: attemptsLeft,\n last_attempt_time: lastAttemptTime,\n });\n },\n});\n\n/**\n * Patch a rate limit entry with partial data.\n *\n * Updates an existing `RateLimit` document with the provided fields.\n * Automatically maps camelCase field names (`attemptsLeft`,\n * `lastAttemptTime`) to the snake_case storage format before writing.\n * Typically called to decrement remaining attempts or to reset the\n * bucket after a cooldown window has elapsed.\n *\n * @param rateLimitId - The `_id` of the `RateLimit` document to update.\n * @param data - An object containing the fields to patch. Supports\n * camelCase names which are transparently converted:\n * - `attemptsLeft` -- Updated number of remaining attempts.\n * - `lastAttemptTime` -- Updated timestamp of the most recent attempt.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Decrement attempts after a failed login\n * await ctx.runMutation(\n * components.auth.security.limits.rateLimitPatch,\n * {\n * rateLimitId: limit._id,\n * data: {\n * attemptsLeft: limit.attemptsLeft - 1,\n * lastAttemptTime: Date.now(),\n * },\n * },\n * );\n * ```\n */\nexport const rateLimitPatch = mutation({\n args: { rateLimitId: v.id(\"RateLimit\"), data: v.any() },\n returns: v.null(),\n handler: async (ctx, { rateLimitId, data }) => {\n const nextData: Record<string, unknown> = { ...data };\n if (nextData.attemptsLeft !== undefined) {\n nextData.attempts_left = nextData.attemptsLeft;\n delete nextData.attemptsLeft;\n }\n if (nextData.lastAttemptTime !== undefined) {\n nextData.last_attempt_time = nextData.lastAttemptTime;\n delete nextData.lastAttemptTime;\n }\n await ctx.db.patch(\"RateLimit\", rateLimitId, nextData);\n return null;\n },\n});\n\n/**\n * Delete a rate limit entry from the `RateLimit` table.\n *\n * Permanently removes the rate limit bucket. This effectively resets\n * rate limiting for the associated identifier, allowing the next\n * action to proceed without throttling. Useful for administrative\n * resets or cleanup of expired buckets.\n *\n * @param rateLimitId - The `_id` of the `RateLimit` document to delete.\n * @returns `null` on success.\n *\n * @example\n * ```ts\n * // Admin resets a user's login rate limit\n * await ctx.runMutation(\n * components.auth.security.limits.rateLimitDelete,\n * { rateLimitId: limit._id },\n * );\n * ```\n */\nexport const rateLimitDelete = mutation({\n args: { rateLimitId: v.id(\"RateLimit\") },\n returns: v.null(),\n handler: async (ctx, { rateLimitId }) => {\n await ctx.db.delete(\"RateLimit\", rateLimitId);\n return null;\n },\n});\n\n// ============================================================================\n// Device Authorization (RFC 8628)\n// ============================================================================\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,eAAe,MAAM;CAChC,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE;CAChC,SAAS,EAAE,MAAM,kBAAkB,EAAE,MAAM,CAAC;CAC5C,SAAS,OAAO,KAAK,EAAE,iBAAiB;EACtC,MAAM,MAAM,MAAM,IAAI,GACnB,MAAM,YAAY,CAClB,UAAU,kBAAkB,MAAM,EAAE,GAAG,cAAc,WAAW,CAAC,CACjE,QAAQ;AACX,MAAI,QAAQ,KACV,QAAO;AAET,SAAO;GACL,GAAG;GACH,cAAc,IAAI;GAClB,iBAAiB,IAAI;GACtB;;CAEJ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BF,MAAa,kBAAkB,SAAS;CACtC,MAAM;EACJ,YAAY,EAAE,QAAQ;EACtB,cAAc,EAAE,QAAQ;EACxB,iBAAiB,EAAE,QAAQ;EAC5B;CACD,SAAS,EAAE,GAAG,YAAY;CAC1B,SAAS,OAAO,KAAK,EAAE,YAAY,cAAc,sBAAsB;AACrE,SAAO,MAAM,IAAI,GAAG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