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

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 (314) hide show
  1. package/README.md +10 -11
  2. package/dist/authorization/index.d.ts +1 -1
  3. package/dist/authorization/index.js +1 -1
  4. package/dist/authorization/index.js.map +1 -1
  5. package/dist/client/index.d.ts +1 -2
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +36 -39
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/component/client/index.d.ts +1 -2
  10. package/dist/component/index.js +2 -2
  11. package/dist/component/model.d.ts +9 -9
  12. package/dist/component/model.d.ts.map +1 -1
  13. package/dist/component/public/enterprise/audit.d.ts.map +1 -1
  14. package/dist/component/public/enterprise/audit.js.map +1 -1
  15. package/dist/component/public/enterprise/core.d.ts.map +1 -1
  16. package/dist/component/public/enterprise/core.js.map +1 -1
  17. package/dist/component/public/enterprise/domains.d.ts.map +1 -1
  18. package/dist/component/public/enterprise/domains.js.map +1 -1
  19. package/dist/component/public/enterprise/scim.d.ts.map +1 -1
  20. package/dist/component/public/enterprise/scim.js.map +1 -1
  21. package/dist/component/public/enterprise/secrets.d.ts.map +1 -1
  22. package/dist/component/public/enterprise/secrets.js.map +1 -1
  23. package/dist/component/public/enterprise/webhooks.d.ts.map +1 -1
  24. package/dist/component/public/enterprise/webhooks.js.map +1 -1
  25. package/dist/component/public/factors/devices.d.ts.map +1 -1
  26. package/dist/component/public/factors/devices.js.map +1 -1
  27. package/dist/component/public/factors/passkeys.d.ts.map +1 -1
  28. package/dist/component/public/factors/passkeys.js.map +1 -1
  29. package/dist/component/public/factors/totp.d.ts.map +1 -1
  30. package/dist/component/public/factors/totp.js.map +1 -1
  31. package/dist/component/public/groups/core.js.map +1 -1
  32. package/dist/component/public/groups/invites.d.ts.map +1 -1
  33. package/dist/component/public/groups/invites.js.map +1 -1
  34. package/dist/component/public/groups/members.d.ts.map +1 -1
  35. package/dist/component/public/groups/members.js.map +1 -1
  36. package/dist/component/public/identity/accounts.d.ts.map +1 -1
  37. package/dist/component/public/identity/accounts.js.map +1 -1
  38. package/dist/component/public/identity/codes.d.ts.map +1 -1
  39. package/dist/component/public/identity/codes.js.map +1 -1
  40. package/dist/component/public/identity/sessions.d.ts.map +1 -1
  41. package/dist/component/public/identity/sessions.js.map +1 -1
  42. package/dist/component/public/identity/tokens.d.ts.map +1 -1
  43. package/dist/component/public/identity/tokens.js.map +1 -1
  44. package/dist/component/public/identity/users.d.ts.map +1 -1
  45. package/dist/component/public/identity/users.js.map +1 -1
  46. package/dist/component/public/identity/verifiers.d.ts.map +1 -1
  47. package/dist/component/public/identity/verifiers.js.map +1 -1
  48. package/dist/component/public/security/keys.d.ts.map +1 -1
  49. package/dist/component/public/security/keys.js.map +1 -1
  50. package/dist/component/public/security/limits.d.ts.map +1 -1
  51. package/dist/component/public/security/limits.js.map +1 -1
  52. package/dist/component/schema.d.ts +41 -41
  53. package/dist/component/server/auth.d.ts +127 -130
  54. package/dist/component/server/auth.d.ts.map +1 -1
  55. package/dist/component/server/auth.js +100 -64
  56. package/dist/component/server/auth.js.map +1 -1
  57. package/dist/component/server/context.js +53 -0
  58. package/dist/component/server/context.js.map +1 -0
  59. package/dist/component/server/core.js +113 -250
  60. package/dist/component/server/core.js.map +1 -1
  61. package/dist/component/server/crypto.js +25 -7
  62. package/dist/component/server/crypto.js.map +1 -1
  63. package/dist/component/server/device.js +59 -16
  64. package/dist/component/server/device.js.map +1 -1
  65. package/dist/component/server/enterprise/domain.js +148 -59
  66. package/dist/component/server/enterprise/domain.js.map +1 -1
  67. package/dist/component/server/enterprise/http.js +36 -15
  68. package/dist/component/server/enterprise/http.js.map +1 -1
  69. package/dist/component/server/enterprise/oidc.js +1 -1
  70. package/dist/component/server/http.d.ts +85 -0
  71. package/dist/component/server/http.d.ts.map +1 -0
  72. package/dist/component/server/http.js +85 -22
  73. package/dist/component/server/http.js.map +1 -1
  74. package/dist/component/server/identity.js +5 -2
  75. package/dist/component/server/identity.js.map +1 -1
  76. package/dist/component/server/limits.js +21 -30
  77. package/dist/component/server/limits.js.map +1 -1
  78. package/dist/component/server/mutations/account.js +12 -10
  79. package/dist/component/server/mutations/account.js.map +1 -1
  80. package/dist/component/server/mutations/code.js +5 -2
  81. package/dist/component/server/mutations/code.js.map +1 -1
  82. package/dist/component/server/mutations/invalidate.js +1 -1
  83. package/dist/component/server/mutations/invalidate.js.map +1 -1
  84. package/dist/component/server/mutations/oauth.js +10 -4
  85. package/dist/component/server/mutations/oauth.js.map +1 -1
  86. package/dist/component/server/mutations/refresh.js +2 -2
  87. package/dist/component/server/mutations/refresh.js.map +1 -1
  88. package/dist/component/server/mutations/register.js +46 -42
  89. package/dist/component/server/mutations/register.js.map +1 -1
  90. package/dist/component/server/mutations/retrieve.js +21 -25
  91. package/dist/component/server/mutations/retrieve.js.map +1 -1
  92. package/dist/component/server/mutations/signature.js +10 -4
  93. package/dist/component/server/mutations/signature.js.map +1 -1
  94. package/dist/component/server/mutations/signout.js.map +1 -1
  95. package/dist/component/server/mutations/store.js +9 -24
  96. package/dist/component/server/mutations/store.js.map +1 -1
  97. package/dist/component/server/mutations/verifier.js.map +1 -1
  98. package/dist/component/server/mutations/verify.js +1 -1
  99. package/dist/component/server/mutations/verify.js.map +1 -1
  100. package/dist/component/server/oauth.js +53 -16
  101. package/dist/component/server/oauth.js.map +1 -1
  102. package/dist/component/server/passkey.js +115 -31
  103. package/dist/component/server/passkey.js.map +1 -1
  104. package/dist/component/server/redirects.js +9 -3
  105. package/dist/component/server/redirects.js.map +1 -1
  106. package/dist/component/server/refresh.js +10 -7
  107. package/dist/component/server/refresh.js.map +1 -1
  108. package/dist/component/server/runtime.d.ts +5 -5
  109. package/dist/component/server/runtime.js +156 -113
  110. package/dist/component/server/runtime.js.map +1 -1
  111. package/dist/component/server/signin.js +34 -10
  112. package/dist/component/server/signin.js.map +1 -1
  113. package/dist/component/server/totp.js +79 -19
  114. package/dist/component/server/totp.js.map +1 -1
  115. package/dist/component/server/types.d.ts +12 -20
  116. package/dist/component/server/types.d.ts.map +1 -1
  117. package/dist/component/server/types.js.map +1 -1
  118. package/dist/component/server/users.js +6 -3
  119. package/dist/component/server/users.js.map +1 -1
  120. package/dist/component/server/utils.js +10 -4
  121. package/dist/component/server/utils.js.map +1 -1
  122. package/dist/core/types.d.ts +14 -22
  123. package/dist/core/types.d.ts.map +1 -1
  124. package/dist/factors/device.js +8 -9
  125. package/dist/factors/device.js.map +1 -1
  126. package/dist/factors/passkey.js +18 -21
  127. package/dist/factors/passkey.js.map +1 -1
  128. package/dist/providers/password.js +66 -81
  129. package/dist/providers/password.js.map +1 -1
  130. package/dist/runtime/invite.js +2 -8
  131. package/dist/runtime/invite.js.map +1 -1
  132. package/dist/server/auth.d.ts +127 -130
  133. package/dist/server/auth.d.ts.map +1 -1
  134. package/dist/server/auth.js +100 -64
  135. package/dist/server/auth.js.map +1 -1
  136. package/dist/server/context.d.ts +1 -0
  137. package/dist/server/context.js +53 -0
  138. package/dist/server/context.js.map +1 -0
  139. package/dist/server/core.d.ts +74 -195
  140. package/dist/server/core.d.ts.map +1 -1
  141. package/dist/server/core.js +113 -250
  142. package/dist/server/core.js.map +1 -1
  143. package/dist/server/crypto.d.ts.map +1 -1
  144. package/dist/server/crypto.js +25 -7
  145. package/dist/server/crypto.js.map +1 -1
  146. package/dist/server/device.js +59 -16
  147. package/dist/server/device.js.map +1 -1
  148. package/dist/server/enterprise/domain.d.ts +0 -8
  149. package/dist/server/enterprise/domain.d.ts.map +1 -1
  150. package/dist/server/enterprise/domain.js +148 -59
  151. package/dist/server/enterprise/domain.js.map +1 -1
  152. package/dist/server/enterprise/http.d.ts.map +1 -1
  153. package/dist/server/enterprise/http.js +35 -14
  154. package/dist/server/enterprise/http.js.map +1 -1
  155. package/dist/server/http.d.ts +81 -3
  156. package/dist/server/http.d.ts.map +1 -1
  157. package/dist/server/http.js +84 -21
  158. package/dist/server/http.js.map +1 -1
  159. package/dist/server/identity.js +5 -2
  160. package/dist/server/identity.js.map +1 -1
  161. package/dist/server/index.d.ts +3 -2
  162. package/dist/server/index.js +2 -2
  163. package/dist/server/limits.js +21 -30
  164. package/dist/server/limits.js.map +1 -1
  165. package/dist/server/mounts.d.ts +25 -63
  166. package/dist/server/mounts.d.ts.map +1 -1
  167. package/dist/server/mounts.js +46 -107
  168. package/dist/server/mounts.js.map +1 -1
  169. package/dist/server/mutations/account.d.ts +8 -9
  170. package/dist/server/mutations/account.d.ts.map +1 -1
  171. package/dist/server/mutations/account.js +11 -9
  172. package/dist/server/mutations/account.js.map +1 -1
  173. package/dist/server/mutations/code.d.ts +12 -12
  174. package/dist/server/mutations/code.d.ts.map +1 -1
  175. package/dist/server/mutations/code.js +5 -2
  176. package/dist/server/mutations/code.js.map +1 -1
  177. package/dist/server/mutations/invalidate.d.ts +4 -4
  178. package/dist/server/mutations/invalidate.d.ts.map +1 -1
  179. package/dist/server/mutations/invalidate.js.map +1 -1
  180. package/dist/server/mutations/oauth.d.ts +14 -12
  181. package/dist/server/mutations/oauth.d.ts.map +1 -1
  182. package/dist/server/mutations/oauth.js +9 -3
  183. package/dist/server/mutations/oauth.js.map +1 -1
  184. package/dist/server/mutations/refresh.d.ts +3 -3
  185. package/dist/server/mutations/refresh.d.ts.map +1 -1
  186. package/dist/server/mutations/refresh.js +1 -1
  187. package/dist/server/mutations/refresh.js.map +1 -1
  188. package/dist/server/mutations/register.d.ts +11 -11
  189. package/dist/server/mutations/register.d.ts.map +1 -1
  190. package/dist/server/mutations/register.js +45 -41
  191. package/dist/server/mutations/register.js.map +1 -1
  192. package/dist/server/mutations/retrieve.d.ts +6 -6
  193. package/dist/server/mutations/retrieve.d.ts.map +1 -1
  194. package/dist/server/mutations/retrieve.js +20 -24
  195. package/dist/server/mutations/retrieve.js.map +1 -1
  196. package/dist/server/mutations/signature.d.ts +6 -7
  197. package/dist/server/mutations/signature.d.ts.map +1 -1
  198. package/dist/server/mutations/signature.js +9 -3
  199. package/dist/server/mutations/signature.js.map +1 -1
  200. package/dist/server/mutations/signin.d.ts +5 -5
  201. package/dist/server/mutations/signout.js.map +1 -1
  202. package/dist/server/mutations/store.d.ts +83 -83
  203. package/dist/server/mutations/store.js +8 -23
  204. package/dist/server/mutations/store.js.map +1 -1
  205. package/dist/server/mutations/verifier.js.map +1 -1
  206. package/dist/server/mutations/verify.d.ts +7 -7
  207. package/dist/server/mutations/verify.d.ts.map +1 -1
  208. package/dist/server/mutations/verify.js.map +1 -1
  209. package/dist/server/oauth.js +53 -16
  210. package/dist/server/oauth.js.map +1 -1
  211. package/dist/server/passkey.d.ts +2 -2
  212. package/dist/server/passkey.d.ts.map +1 -1
  213. package/dist/server/passkey.js +114 -30
  214. package/dist/server/passkey.js.map +1 -1
  215. package/dist/server/redirects.js +9 -3
  216. package/dist/server/redirects.js.map +1 -1
  217. package/dist/server/refresh.js +10 -7
  218. package/dist/server/refresh.js.map +1 -1
  219. package/dist/server/runtime.d.ts +11 -11
  220. package/dist/server/runtime.js +155 -112
  221. package/dist/server/runtime.js.map +1 -1
  222. package/dist/server/signin.js +34 -10
  223. package/dist/server/signin.js.map +1 -1
  224. package/dist/server/ssr.d.ts.map +1 -1
  225. package/dist/server/ssr.js +175 -184
  226. package/dist/server/ssr.js.map +1 -1
  227. package/dist/server/totp.js +78 -18
  228. package/dist/server/totp.js.map +1 -1
  229. package/dist/server/types.d.ts +13 -21
  230. package/dist/server/types.d.ts.map +1 -1
  231. package/dist/server/types.js.map +1 -1
  232. package/dist/server/users.js +6 -3
  233. package/dist/server/users.js.map +1 -1
  234. package/dist/server/utils.js +10 -4
  235. package/dist/server/utils.js.map +1 -1
  236. package/package.json +1 -5
  237. package/src/authorization/index.ts +1 -1
  238. package/src/client/core/types.ts +14 -14
  239. package/src/client/factors/device.ts +10 -12
  240. package/src/client/factors/passkey.ts +23 -26
  241. package/src/client/index.ts +54 -64
  242. package/src/client/runtime/invite.ts +5 -7
  243. package/src/component/index.ts +9 -3
  244. package/src/component/public/enterprise/audit.ts +6 -1
  245. package/src/component/public/enterprise/core.ts +1 -0
  246. package/src/component/public/enterprise/domains.ts +5 -1
  247. package/src/component/public/enterprise/scim.ts +1 -0
  248. package/src/component/public/enterprise/secrets.ts +1 -0
  249. package/src/component/public/enterprise/webhooks.ts +1 -0
  250. package/src/component/public/factors/devices.ts +1 -0
  251. package/src/component/public/factors/passkeys.ts +1 -0
  252. package/src/component/public/factors/totp.ts +1 -0
  253. package/src/component/public/groups/core.ts +1 -1
  254. package/src/component/public/groups/invites.ts +7 -1
  255. package/src/component/public/groups/members.ts +1 -0
  256. package/src/component/public/identity/accounts.ts +1 -0
  257. package/src/component/public/identity/codes.ts +1 -0
  258. package/src/component/public/identity/sessions.ts +1 -0
  259. package/src/component/public/identity/tokens.ts +1 -0
  260. package/src/component/public/identity/users.ts +1 -0
  261. package/src/component/public/identity/verifiers.ts +1 -0
  262. package/src/component/public/security/keys.ts +1 -0
  263. package/src/component/public/security/limits.ts +1 -0
  264. package/src/providers/password.ts +89 -110
  265. package/src/server/auth.ts +240 -182
  266. package/src/server/context.ts +90 -0
  267. package/src/server/core.ts +195 -286
  268. package/src/server/crypto.ts +31 -29
  269. package/src/server/device.ts +65 -32
  270. package/src/server/enterprise/domain.ts +158 -170
  271. package/src/server/enterprise/http.ts +46 -39
  272. package/src/server/http.ts +289 -30
  273. package/src/server/identity.ts +5 -5
  274. package/src/server/index.ts +9 -3
  275. package/src/server/limits.ts +53 -80
  276. package/src/server/mounts.ts +56 -80
  277. package/src/server/mutations/account.ts +22 -36
  278. package/src/server/mutations/code.ts +6 -6
  279. package/src/server/mutations/invalidate.ts +1 -1
  280. package/src/server/mutations/oauth.ts +14 -8
  281. package/src/server/mutations/refresh.ts +5 -4
  282. package/src/server/mutations/register.ts +87 -132
  283. package/src/server/mutations/retrieve.ts +44 -44
  284. package/src/server/mutations/signature.ts +13 -6
  285. package/src/server/mutations/signout.ts +1 -1
  286. package/src/server/mutations/store.ts +16 -31
  287. package/src/server/mutations/verifier.ts +1 -1
  288. package/src/server/mutations/verify.ts +3 -5
  289. package/src/server/oauth.ts +60 -69
  290. package/src/server/passkey.ts +567 -517
  291. package/src/server/redirects.ts +10 -6
  292. package/src/server/refresh.ts +14 -18
  293. package/src/server/runtime.ts +340 -302
  294. package/src/server/signin.ts +44 -37
  295. package/src/server/ssr.ts +390 -407
  296. package/src/server/totp.ts +85 -35
  297. package/src/server/types.ts +19 -22
  298. package/src/server/users.ts +7 -6
  299. package/src/server/utils.ts +10 -12
  300. package/dist/component/server/authError.js +0 -34
  301. package/dist/component/server/authError.js.map +0 -1
  302. package/dist/component/server/errors.d.ts +0 -1
  303. package/dist/component/server/errors.js +0 -137
  304. package/dist/component/server/errors.js.map +0 -1
  305. package/dist/server/authError.d.ts +0 -46
  306. package/dist/server/authError.d.ts.map +0 -1
  307. package/dist/server/authError.js +0 -34
  308. package/dist/server/authError.js.map +0 -1
  309. package/dist/server/errors.d.ts +0 -177
  310. package/dist/server/errors.d.ts.map +0 -1
  311. package/dist/server/errors.js +0 -212
  312. package/dist/server/errors.js.map +0 -1
  313. package/src/server/authError.ts +0 -44
  314. package/src/server/errors.ts +0 -290
@@ -13,7 +13,7 @@ import { GenericId } from "convex/values";
13
13
  type AuthConfig = Omit<ConvexAuthConfig, "component">;
14
14
  /** Canonical user document type exposed by Convex Auth. */
15
15
  type UserDoc = Doc<"User">;
16
- type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig | undefined> = Omit<ReturnType<typeof Auth$1>["auth"]["member"], "create" | "list" | "update" | "resolve"> & {
16
+ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig | undefined> = Omit<ReturnType<typeof Auth$1>["auth"]["member"], "create" | "list" | "update" | "inspect" | "require"> & {
17
17
  create: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["create"]>[0], data: {
18
18
  groupId: string;
19
19
  userId: string;
@@ -21,7 +21,6 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
21
21
  status?: string;
22
22
  extend?: Record<string, unknown>;
23
23
  }) => Promise<{
24
- ok: true;
25
24
  memberId: string;
26
25
  }>;
27
26
  list: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["list"]>[0], opts?: {
@@ -39,17 +38,22 @@ type MemberApiWithAuthorization<TAuthorization extends AuthAuthorizationConfig |
39
38
  update: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["update"]>[0], memberId: string, data: Record<string, unknown> & {
40
39
  roleIds?: AuthRoleId<TAuthorization>[];
41
40
  }) => Promise<{
42
- ok: true;
43
41
  memberId: string;
44
42
  }>;
45
- resolve: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["resolve"]>[0], opts: {
43
+ inspect: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["inspect"]>[0], opts: {
44
+ userId: string;
45
+ groupId: string;
46
+ ancestry?: boolean;
47
+ maxDepth?: number;
48
+ }) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["inspect"]>;
49
+ require: (ctx: Parameters<ReturnType<typeof Auth$1>["auth"]["member"]["require"]>[0], opts: {
46
50
  userId: string;
47
51
  groupId: string;
48
52
  ancestry?: boolean;
49
53
  roleIds?: AuthRoleId<TAuthorization>[];
50
54
  grants?: AuthGrant<TAuthorization>[];
51
55
  maxDepth?: number;
52
- }) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["resolve"]>;
56
+ }) => ReturnType<ReturnType<typeof Auth$1>["auth"]["member"]["require"]>;
53
57
  };
54
58
  /**
55
59
  * The base auth API surface returned by {@link createAuth}.
@@ -80,30 +84,40 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
80
84
  key: ReturnType<typeof Auth$1>["auth"]["key"];
81
85
  http: ReturnType<typeof Auth$1>["auth"]["http"];
82
86
  /**
83
- * Resolve the current user's auth context. Framework-agnostic — use
87
+ * Resolve the current request's auth context. Framework-agnostic — use
84
88
  * this in fluent-convex middleware, custom wrappers, or anywhere you
85
- * need the resolved `{ userId, user, groupId, role, grants }` object.
89
+ * need the current `{ userId, user, groupId, role, grants }` object.
86
90
  *
87
- * Returns `null` when unauthenticated. Does not throw.
91
+ * Throws a structured `ConvexError` when unauthenticated by default.
92
+ * Pass `{ optional: true }` to get a null-shaped auth object instead.
88
93
  *
89
94
  * @param ctx - Convex query, mutation, or action context.
90
- * @returns The resolved auth context, or `null`.
95
+ * @param config - Optional auth resolution config. Supports `optional`,
96
+ * `resolve`, and `authResolve`.
97
+ * @returns The current auth context.
91
98
  *
92
99
  * @example fluent-convex middleware
93
100
  * ```ts
94
101
  * const withAuth = convex.createMiddleware(async (ctx, next) => {
95
- * return next({ ...ctx, auth: await auth.resolve(ctx) });
102
+ * return next({ ...ctx, auth: await auth.context(ctx) });
96
103
  * });
97
104
  * ```
98
105
  *
99
106
  * @example Direct usage in a handler
100
- * ```ts
101
- * const resolved = await auth.resolve(ctx);
102
- * if (!resolved) return { ok: false, code: "NOT_SIGNED_IN" };
103
- * const { userId, grants } = resolved;
104
- * ```
105
- */
106
- resolve: (ctx: any) => Promise<AuthResolvedContext | null>;
107
+ * ```ts
108
+ * const authContext = await auth.context(ctx);
109
+ * const { userId, grants } = authContext;
110
+ * ```
111
+ *
112
+ * @example Optional usage
113
+ * ```ts
114
+ * const authContext = await auth.context(ctx, { optional: true });
115
+ * if (authContext.userId === null) {
116
+ * return null;
117
+ * }
118
+ * ```
119
+ */
120
+ context: AuthContextResolver;
107
121
  /**
108
122
  * Context enrichment for convex-helpers `customQuery` / `customMutation` /
109
123
  * `customAction`.
@@ -112,9 +126,9 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
112
126
  * and grants, then attaches them to `ctx.auth`. Returns a `Customization`
113
127
  * object compatible with convex-helpers' custom function builders.
114
128
  *
115
- * `ctx.auth` is `{ userId, user, groupId, role, grants }` when
116
- * authenticated, `null` when unauthenticated. No throwing your
117
- * handler decides how to respond.
129
+ * `ctx.auth` is the current request auth context.
130
+ * By default this throws when unauthenticated so handlers can assume
131
+ * `ctx.auth.userId` and `ctx.auth.user` exist.
118
132
  *
119
133
  * @returns A convex-helpers `Customization` object.
120
134
  *
@@ -136,37 +150,29 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
136
150
  * export const list = authQuery({
137
151
  * args: { workspaceId: v.string() },
138
152
  * handler: async (ctx, args) => {
139
- * if (!ctx.auth) return [];
140
153
  * const { userId, groupId, grants } = ctx.auth;
141
154
  * // business logic
142
155
  * },
143
156
  * });
144
157
  * ```
145
158
  */
146
- ctx: () => {
147
- args: Record<string, never>;
148
- input: (ctx: any) => Promise<{
149
- ctx: {
150
- auth: AuthResolvedContext | null;
151
- };
152
- args: Record<string, never>;
153
- }>;
154
- };
159
+ ctx: AuthContextFactory;
155
160
  };
156
161
  /**
157
- * Resolved auth context injected into `ctx.auth` by `auth.ctx()` and
158
- * {@link AuthCtx}. Also the expected return shape for custom
159
- * {@link AuthCtxConfig.authResolve | authResolve} hooks.
162
+ * Current request auth context injected into `ctx.auth` by `auth.ctx()`. This
163
+ * is the authenticated auth shape returned by {@link createAuth().context}.
164
+ * Optional context builders may still surface nullable fields when
165
+ * `optional: true` is used.
160
166
  *
161
- * - `null` when unauthenticated.
162
167
  * - `groupId` is `null` when the user has no active group set.
163
- * - `role` / `grants` are `null` / `[]` when no active group or no membership.
168
+ * - `role` is `null` when no active group or no membership is resolved.
169
+ * - `grants` is `[]` when no active group or no membership is resolved.
164
170
  *
165
171
  * @example
166
172
  * ```ts
167
- * import type { AuthResolvedContext } from "@robelest/convex-auth/server";
173
+ * import type { AuthContext } from "@robelest/convex-auth/server";
168
174
  *
169
- * const mockAuth: AuthResolvedContext = {
175
+ * const mockAuth: AuthContext = {
170
176
  * userId: "user123" as Id<"User">,
171
177
  * user: { _id: "user123", email: "test@example.com" },
172
178
  * groupId: "group456",
@@ -175,23 +181,66 @@ type AuthApiBase<TAuthorization extends AuthAuthorizationConfig | undefined = un
175
181
  * };
176
182
  * ```
177
183
  */
178
- type AuthResolvedContext = {
184
+ type AuthContext = {
179
185
  /** The authenticated user's document ID. */userId: GenericId<"User">; /** The authenticated user's full document. */
180
186
  user: UserDoc; /** The user's active group ID, or `null` if none set. */
181
187
  groupId: string | null; /** The user's primary role in the active group, or `null`. */
182
188
  role: string | null; /** Resolved grant strings from the user's role definitions. */
183
189
  grants: string[];
184
190
  };
185
- type AuthCtxBase = {
191
+ /**
192
+ * Nullable auth context returned by `auth.context(ctx, { optional: true })`
193
+ * and injected by `auth.ctx({ optional: true })`.
194
+ *
195
+ * Use this when callers may be unauthenticated but you still want a stable
196
+ * auth-shaped object.
197
+ *
198
+ * - `userId` and `user` are `null` when unauthenticated.
199
+ * - `groupId` and `role` are `null` when no active group is resolved.
200
+ * - `grants` is `[]` when no membership is resolved.
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * const authContext = await auth.context(ctx, { optional: true });
205
+ * if (authContext.userId === null) {
206
+ * return null;
207
+ * }
208
+ * ```
209
+ */
210
+ type OptionalAuthContext = {
211
+ /** The authenticated user's document ID, or `null` when unauthenticated. */userId: GenericId<"User"> | null; /** The authenticated user's full document, or `null` when unauthenticated. */
212
+ user: UserDoc | null; /** The user's active group ID, or `null` if none is set. */
213
+ groupId: string | null; /** The user's primary role in the active group, or `null`. */
214
+ role: string | null; /** Resolved grant strings for the active membership, or `[]`. */
215
+ grants: string[];
216
+ };
217
+ type AuthContextBase = {
186
218
  getUserIdentity: () => Promise<UserIdentity | null>;
187
219
  };
188
- type RequiredAuthCtxState = AuthCtxBase & AuthResolvedContext;
189
- type OptionalAuthCtxState = AuthCtxBase & {
190
- userId: GenericId<"User"> | null;
191
- user: UserDoc | null;
192
- groupId: string | null;
193
- role: string | null;
194
- grants: string[];
220
+ type RequiredAuthContextState = AuthContextBase & AuthContext;
221
+ type OptionalAuthContextState = AuthContextBase & OptionalAuthContext;
222
+ type ResolvedAuthContext<TResolve> = AuthContext & TResolve;
223
+ type ResolvedOptionalAuthContext<TResolve> = OptionalAuthContext & TResolve;
224
+ type AuthContextResolver = {
225
+ <TResolve extends Record<string, unknown> = Record<string, never>>(ctx: any, config: AuthContextConfig<TResolve> & {
226
+ optional: true;
227
+ }): Promise<ResolvedOptionalAuthContext<TResolve>>;
228
+ <TResolve extends Record<string, unknown> = Record<string, never>>(ctx: any, config?: AuthContextConfig<TResolve>): Promise<ResolvedAuthContext<TResolve>>;
229
+ };
230
+ type AuthContextCustomization<TAuth> = {
231
+ args: {};
232
+ input: (ctx: any, _args: any, _extra?: any) => Promise<{
233
+ ctx: {
234
+ auth: TAuth;
235
+ };
236
+ args: {};
237
+ }>;
238
+ };
239
+ type AuthContextFactory = {
240
+ <TResolve extends Record<string, unknown> = Record<string, never>>(config: AuthContextConfig<TResolve> & {
241
+ optional: true;
242
+ }): AuthContextCustomization<OptionalAuthContextState & TResolve>;
243
+ <TResolve extends Record<string, unknown> = Record<string, never>>(config?: AuthContextConfig<TResolve>): AuthContextCustomization<RequiredAuthContextState & TResolve>;
195
244
  };
196
245
  type InternalSsoApi = ReturnType<typeof Auth$1>["auth"]["sso"];
197
246
  type PublicSsoAdminApi = {
@@ -203,7 +252,6 @@ type PublicSsoAdminApi = {
203
252
  domain: string;
204
253
  isPrimary?: boolean;
205
254
  }>) => Promise<{
206
- ok: true;
207
255
  enterpriseId: string;
208
256
  domains: Array<{
209
257
  domainId: string;
@@ -218,7 +266,6 @@ type PublicSsoAdminApi = {
218
266
  enterpriseId: string;
219
267
  domain: string;
220
268
  }) => Promise<{
221
- ok: true;
222
269
  enterpriseId: string;
223
270
  domain: string;
224
271
  requestedAt: number;
@@ -233,7 +280,6 @@ type PublicSsoAdminApi = {
233
280
  enterpriseId: string;
234
281
  domain: string;
235
282
  }) => Promise<{
236
- ok: boolean;
237
283
  enterpriseId: string;
238
284
  domain: string;
239
285
  verifiedAt?: number;
@@ -328,35 +374,53 @@ declare function createAuth<P extends AuthProviderConfig[], TAuthorization exten
328
374
  authorization?: TAuthorization;
329
375
  }): ConvexAuthResult<P, TAuthorization>;
330
376
  /**
331
- * Configuration for {@link AuthCtx} context enrichment.
377
+ * Configuration for {@link createAuth().ctx} context enrichment.
378
+ *
379
+ * The same config shape is also used by {@link createAuth().context}.
332
380
  *
333
381
  * @typeParam TResolve - Extra fields returned from `resolve()` and merged into
334
382
  * the resulting `ctx.auth` object.
383
+ *
384
+ * @example
385
+ * ```ts
386
+ * const authContext = await auth.context(ctx, {
387
+ * resolve: async (_ctx, user, authState) => ({
388
+ * email: user.email,
389
+ * canWrite: authState.grants.includes("posts.write"),
390
+ * }),
391
+ * });
392
+ * ```
335
393
  */
336
- type AuthCtxConfig<TResolve extends Record<string, unknown> = Record<string, never>> = {
337
- /** Allow unauthenticated callers and return `userId: null` / `user: null`. */optional?: boolean;
394
+ type AuthContextConfig<TResolve extends Record<string, unknown> = Record<string, never>> = {
395
+ /**
396
+ * Allow unauthenticated callers and return a null-shaped auth object instead
397
+ * of throwing `NOT_SIGNED_IN`.
398
+ */
399
+ optional?: boolean;
338
400
  /**
339
401
  * Attach additional derived fields to the auth context after the base auth
340
402
  * context is resolved.
403
+ *
404
+ * This callback runs only when a user is authenticated.
341
405
  */
342
- resolve?: (ctx: any, user: UserDoc, auth: AuthResolvedContext) => Promise<TResolve> | TResolve;
406
+ resolve?: (ctx: any, user: UserDoc, auth: AuthContext) => Promise<TResolve> | TResolve;
343
407
  /**
344
- * Override or wrap the base auth resolution used by {@link AuthCtx}.
408
+ * Override or wrap the base auth resolution used by {@link createAuth().ctx}.
345
409
  *
346
410
  * Return `undefined` to fall back to the built-in resolver,
347
411
  * `null` for an explicit unauthenticated state, or an
348
- * {@link AuthResolvedContext} object to provide a pre-resolved auth state.
412
+ * {@link AuthContext} object to provide a pre-resolved auth state.
349
413
  * This is useful for tests, proxy auth, impersonation flows, or any
350
414
  * environment that needs to inject auth without depending on the standard
351
415
  * Convex auth tables.
352
416
  *
353
417
  * @param ctx - The Convex function context.
354
- * @param fallback - The built-in auth resolver used by {@link AuthCtx}.
418
+ * @param fallback - The built-in auth resolver used by {@link createAuth().ctx}.
355
419
  * @returns Resolved auth state, `null`, or `undefined` to use the fallback.
356
420
  *
357
421
  * @example
358
422
  * ```ts
359
- * const authCtx = AuthCtx(auth, {
423
+ * const authCtx = auth.ctx({
360
424
  * authResolve: async (ctx, fallback) => {
361
425
  * const injected = getInjectedAuth(ctx);
362
426
  * return injected ?? (await fallback());
@@ -364,91 +428,24 @@ type AuthCtxConfig<TResolve extends Record<string, unknown> = Record<string, nev
364
428
  * });
365
429
  * ```
366
430
  */
367
- authResolve?: (ctx: any, fallback: () => Promise<AuthResolvedContext | null>) => Promise<AuthResolvedContext | null | undefined> | AuthResolvedContext | null | undefined;
368
- };
369
- /**
370
- * Create a context enrichment for `customQuery` / `customMutation` — optional auth.
371
- *
372
- * When `optional: true` is set, unauthenticated requests are allowed.
373
- * The enriched `ctx.auth` will have `userId: null`, `user: null`,
374
- * `groupId: null`, `role: null`, and `grants: []` for unauthenticated callers.
375
- *
376
- * @param auth - The auth API object returned by {@link createAuth}.
377
- * @param config - Configuration with `optional: true` and an optional
378
- * `resolve` callback for attaching extra fields to the auth context.
379
- * @returns An object with `args` and `input` compatible with Convex
380
- * custom function builders.
381
- *
382
- * @example
383
- * ```ts
384
- * const authCtx = AuthCtx(auth, {
385
- * optional: true,
386
- * resolve: async (_ctx, user) => ({ plan: user?.extend?.plan ?? null }),
387
- * });
388
- * ```
389
- *
390
- * @see {@link createAuth}
391
- */
392
- declare function AuthCtx<TResolve extends Record<string, unknown> = Record<string, never>>(auth: AuthLike, config: AuthCtxConfig<TResolve> & {
393
- optional: true;
394
- }): {
395
- args: {};
396
- input: (ctx: any, _args: any, _extra?: any) => Promise<{
397
- ctx: {
398
- auth: OptionalAuthCtxState & TResolve;
399
- };
400
- args: {};
401
- }>;
402
- };
403
- /**
404
- * Create a context enrichment for `customQuery` / `customMutation` — required auth (default).
405
- *
406
- * When `optional` is omitted or `false`, the inferred type is the authenticated
407
- * auth shape. At runtime this helper still resolves instead of throwing, so if
408
- * no user is signed in the returned `ctx.auth.userId` / `ctx.auth.user` are
409
- * `null`, `ctx.auth.groupId` / `ctx.auth.role` are `null`, and
410
- * `ctx.auth.grants` is `[]`.
411
- *
412
- * @param auth - The auth API object returned by {@link createAuth}.
413
- * @param config - Optional configuration with a `resolve` callback
414
- * for attaching extra fields to the auth context.
415
- * @returns An object with `args` and `input` compatible with Convex
416
- * custom function builders.
417
- *
418
- * @example
419
- * ```ts
420
- * const authCtx = AuthCtx(auth, {
421
- * resolve: async (_ctx, user) => ({ email: user.email }),
422
- * });
423
- * ```
424
- *
425
- * @see {@link createAuth}
426
- */
427
- declare function AuthCtx<TResolve extends Record<string, unknown> = Record<string, never>>(auth: AuthLike, config?: AuthCtxConfig<TResolve>): {
428
- args: {};
429
- input: (ctx: any, _args: any, _extra?: any) => Promise<{
430
- ctx: {
431
- auth: RequiredAuthCtxState & TResolve;
432
- };
433
- args: {};
434
- }>;
431
+ authResolve?: (ctx: any, fallback: () => Promise<AuthContext | null>) => Promise<AuthContext | null | undefined> | AuthContext | null | undefined;
435
432
  };
436
433
  /**
437
- * Extract the resolved `auth` context type from an {@link AuthCtx} instance.
434
+ * Extract the resolved `auth` context type from an `auth.ctx()` customization.
438
435
  *
439
436
  * Use this to type function parameters or variables that receive the
440
- * enriched auth context produced by `AuthCtx`. The inferred type includes
437
+ * enriched auth context produced by `auth.ctx()`. The inferred type includes
441
438
  * `userId`, `user`, `groupId`, `role`, `grants`, `getUserIdentity`, and any
442
439
  * additional fields added by the `resolve` callback. This is the generic
443
440
  * utility for reusing the enriched auth shape without manually duplicating
444
441
  * conditional auth types.
445
442
  *
446
- * @typeParam T - An `AuthCtx` return value (must have an `input` method
443
+ * @typeParam T - An `auth.ctx()` return value (must have an `input` method
447
444
  * that returns `{ ctx: { auth: ... } }`).
448
445
  *
449
446
  * @example
450
447
  * ```ts
451
- * const authCtx = AuthCtx(auth, {
448
+ * const authCtx = auth.ctx({
452
449
  * resolve: async (ctx, user) => ({ orgId: user.orgId }),
453
450
  * });
454
451
  * type Auth = InferAuth<typeof authCtx>;
@@ -465,5 +462,5 @@ type InferAuth<T extends {
465
462
  }>;
466
463
  }> = Awaited<ReturnType<T["input"]>>["ctx"]["auth"];
467
464
  //#endregion
468
- export { AuthApi, AuthApiBase, AuthConfig, AuthCtx, AuthCtxConfig, AuthResolvedContext, ConvexAuthResult, InferAuth, InferClientApi, UserDoc, createAuth };
465
+ export { AuthApi, AuthApiBase, AuthConfig, AuthContext, AuthContextConfig, ConvexAuthResult, InferAuth, InferClientApi, OptionalAuthContext, UserDoc, createAuth };
469
466
  //# sourceMappingURL=auth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","names":[],"sources":["../../src/server/auth.ts"],"mappings":";;;;;;;;;;AAqCA;;KAHY,UAAA,GAAa,IAAA,CAAK,gBAAA;;KAGlB,OAAA,GAAU,GAAA;AAAA,KAEjB,0BAAA,wBACoB,uBAAA,gBACrB,IAAA,CACF,UAAA,QAAkB,MAAA;EAGlB,MAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,MAAA,mCAEpB,IAAA;IACE,OAAA;IACA,MAAA;IACA,OAAA,GAAU,UAAA,CAAW,cAAA;IACrB,MAAA;IACA,MAAA,GAAS,MAAA;EAAA,MAER,OAAA;IAAU,EAAA;IAAU,QAAA;EAAA;EACzB,IAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,MAAA,iCAEpB,IAAA;IACE,KAAA;MACE,OAAA;MACA,MAAA;MACA,MAAA,GAAS,UAAA,CAAW,cAAA;MACpB,MAAA;IAAA;IAEF,KAAA;IACA,MAAA;IACA,OAAA;IACA,KAAA;EAAA,MAEC,UAAA,CAAW,UAAA,QAAkB,MAAA;EAClC,MAAA,GACE,GAAA,EAAK,UAAA,CACH,UAAA,QAAkB,MAAA,mCAEpB,QAAA,UACA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H,OAAA,CAAQ,QAAA,IAAY,QAAA;EAlajB;;;;;;;;;;;;;;;;;;;;;;;;EA2bR,WAAA,IACE,GAAA,OACA,QAAA,QAAgB,OAAA,CAAQ,WAAA,aACrB,OAAA,CAAQ,WAAA,uBAAkC,WAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;KAkHrC,SAAA;EACE,KAAA,MAAW,IAAA,YAAgB,OAAA;IAAU,GAAA;MAAO,IAAA;IAAA;EAAA;AAAA,KACtD,OAAA,CAAQ,UAAA,CAAW,CAAA"}
@@ -1,8 +1,14 @@
1
- import { AuthError } from "./authError.js";
1
+ import { createUnauthenticatedAuthContext, getAuthContext } from "./context.js";
2
2
  import { Auth } from "./runtime.js";
3
+ import { Cv } from "@robelest/fx/convex";
3
4
 
4
5
  //#region src/server/auth.ts
5
6
  /**
7
+ * Auth configuration helpers for Convex Auth.
8
+ *
9
+ * @module
10
+ */
11
+ /**
6
12
  * Create an auth API object.
7
13
  *
8
14
  * When `new SSO()` is included in providers, `auth.sso` and `auth.scim`
@@ -26,41 +32,29 @@ import { Auth } from "./runtime.js";
26
32
  * });
27
33
  * ```
28
34
  *
29
- * @see {@link AuthCtx}
30
- */
31
- /**
32
- * Resolve auth context for the current user. Returns the enriched
33
- * `ctx.auth` object or `null` when unauthenticated.
34
- *
35
- * Resolution flow:
36
- * 1. `user.id(ctx)` → userId or null (exit early)
37
- * 2. `user.get(ctx, userId)` → user doc (cached per-execution)
38
- * 3. `user.getActiveGroup(ctx, { userId })` → groupId or null
39
- * 4. If groupId → `member.resolve(ctx, { userId, groupId })` → role + grants
35
+ * @see {@link AuthContextConfig}
40
36
  */
41
- async function resolveAuthContext(auth, ctx) {
42
- const userId = await auth.user.id(ctx);
43
- if (!userId) return null;
44
- const user = await auth.user.get(ctx, userId);
45
- const groupId = await auth.user.getActiveGroup(ctx, { userId });
46
- let role = null;
47
- let grants = [];
48
- if (groupId) {
49
- const resolved = await auth.member.resolve(ctx, {
50
- userId,
51
- groupId
52
- });
53
- if (resolved.membership) {
54
- role = resolved.roleIds[0] ?? null;
55
- grants = resolved.grants;
56
- }
37
+ async function resolveConfiguredAuthContext(auth, ctx, config) {
38
+ const fallback = () => getAuthContext(auth, ctx);
39
+ const authOverride = config?.authResolve ? await config.authResolve(ctx, fallback) : void 0;
40
+ return authOverride === void 0 ? await fallback() : authOverride;
41
+ }
42
+ function createNotSignedInError() {
43
+ return Cv.error({
44
+ code: "NOT_SIGNED_IN",
45
+ message: "Authentication required."
46
+ });
47
+ }
48
+ async function createPublicAuthContext(auth, ctx, config) {
49
+ const resolved = await resolveConfiguredAuthContext(auth, ctx, config);
50
+ if (resolved === null) {
51
+ if (config?.optional !== true) throw createNotSignedInError();
52
+ return createUnauthenticatedAuthContext();
57
53
  }
54
+ const extra = config?.resolve ? await config.resolve(ctx, resolved.user, resolved) : {};
58
55
  return {
59
- userId,
60
- user,
61
- groupId,
62
- role,
63
- grants
56
+ ...resolved,
57
+ ...extra
64
58
  };
65
59
  }
66
60
  function createAuth(component, config) {
@@ -72,20 +66,32 @@ function createAuth(component, config) {
72
66
  const { domain: domainApi, scim: scimApi, connection: connectionApi, audit: auditApi, webhook: webhookApi, oidc: oidcApi, saml: samlApi, ...restSso } = authResult.auth.sso;
73
67
  const setEnterpriseDomains = async (ctx, enterpriseId, domains) => {
74
68
  const enterprise = await connectionApi.get(ctx, enterpriseId);
75
- if (enterprise === null) throw new AuthError("INVALID_PARAMETERS", "Enterprise not found.").toConvexError();
69
+ if (enterprise === null) throw Cv.error({
70
+ code: "INVALID_PARAMETERS",
71
+ message: "Enterprise not found."
72
+ });
76
73
  const normalized = domains.map((entry) => ({
77
74
  ...entry,
78
75
  domain: entry.domain.trim().toLowerCase()
79
76
  }));
80
77
  const deduped = /* @__PURE__ */ new Map();
81
78
  for (const entry of normalized) {
82
- if (entry.domain.length === 0) throw new AuthError("INVALID_PARAMETERS", "Domain must not be empty.").toConvexError();
83
- if (deduped.has(entry.domain)) throw new AuthError("INVALID_PARAMETERS", `Duplicate domain: ${entry.domain}`).toConvexError();
79
+ if (entry.domain.length === 0) throw Cv.error({
80
+ code: "INVALID_PARAMETERS",
81
+ message: "Domain must not be empty."
82
+ });
83
+ if (deduped.has(entry.domain)) throw Cv.error({
84
+ code: "INVALID_PARAMETERS",
85
+ message: `Duplicate domain: ${entry.domain}`
86
+ });
84
87
  deduped.set(entry.domain, entry);
85
88
  }
86
89
  const nextDomains = [...deduped.values()];
87
90
  const primaryCount = nextDomains.filter((entry) => entry.isPrimary).length;
88
- if (primaryCount > 1) throw new AuthError("INVALID_PARAMETERS", "Only one primary domain may be set.").toConvexError();
91
+ if (primaryCount > 1) throw Cv.error({
92
+ code: "INVALID_PARAMETERS",
93
+ message: "Only one primary domain may be set."
94
+ });
89
95
  if (nextDomains.length > 0 && primaryCount === 0) nextDomains[0] = {
90
96
  ...nextDomains[0],
91
97
  isPrimary: true
@@ -109,7 +115,6 @@ function createAuth(component, config) {
109
115
  });
110
116
  }
111
117
  return {
112
- ok: true,
113
118
  enterpriseId,
114
119
  domains: (await domainApi.list(ctx, enterpriseId)).map((domain) => ({
115
120
  domainId: domain._id,
@@ -168,38 +173,69 @@ function createAuth(component, config) {
168
173
  validate: scimApi.validate
169
174
  } },
170
175
  http: authResult.auth.http,
171
- resolve: (ctx) => resolveAuthContext(authResult.auth, ctx),
172
- ctx: () => ({
173
- args: {},
174
- input: async (ctx) => {
175
- return {
176
- ctx: { auth: await resolveAuthContext(authResult.auth, ctx) },
177
- args: {}
178
- };
179
- }
180
- })
176
+ context: ((ctx, config$1) => createPublicAuthContext(authResult.auth, ctx, config$1)),
177
+ ctx: ((config$1) => createAuthContextCustomization(authResult.auth, config$1))
181
178
  };
182
179
  }
183
- function AuthCtx(auth, config) {
180
+ /**
181
+ * Create a context enrichment for `customQuery` / `customMutation` — optional auth.
182
+ *
183
+ * When `optional: true` is set, unauthenticated requests are allowed.
184
+ * The enriched `ctx.auth` will have `userId: null`, `user: null`,
185
+ * `groupId: null`, `role: null`, and `grants: []` for unauthenticated callers.
186
+ *
187
+ * @param config - Configuration with `optional: true` and an optional
188
+ * `resolve` callback for attaching extra fields to the auth context.
189
+ * @returns An object with `args` and `input` compatible with Convex
190
+ * custom function builders.
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * const authCtx = auth.ctx({
195
+ * optional: true,
196
+ * resolve: async (_ctx, user) => ({ plan: user.extend?.plan ?? null }),
197
+ * });
198
+ * ```
199
+ *
200
+ * @see {@link createAuth}
201
+ */
202
+ /**
203
+ * Create a context enrichment for `customQuery` / `customMutation` — required auth (default).
204
+ *
205
+ * When `optional` is omitted or `false`, unauthenticated requests throw a
206
+ * structured `ConvexError` before your handler runs.
207
+ *
208
+ * @param config - Optional configuration with a `resolve` callback
209
+ * for attaching extra fields to the auth context.
210
+ * @returns An object with `args` and `input` compatible with Convex
211
+ * custom function builders.
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * const authCtx = auth.ctx({
216
+ * resolve: async (_ctx, user) => ({ email: user.email }),
217
+ * });
218
+ * ```
219
+ *
220
+ * @see {@link createAuth}
221
+ */
222
+ function createAuthContextCustomization(auth, config) {
184
223
  return {
185
224
  args: {},
186
225
  input: async (ctx, _args, _extra) => {
187
226
  const nativeAuth = ctx.auth;
188
227
  const getUserIdentity = nativeAuth.getUserIdentity.bind(nativeAuth);
189
- const fallback = () => resolveAuthContext(auth, ctx);
190
- const authOverride = config?.authResolve ? await config.authResolve(ctx, fallback) : void 0;
191
- const resolved = authOverride === void 0 ? await fallback() : authOverride;
192
- if (resolved === null) return {
193
- ctx: { auth: {
194
- getUserIdentity,
195
- userId: null,
196
- user: null,
197
- groupId: null,
198
- role: null,
199
- grants: []
200
- } },
201
- args: {}
202
- };
228
+ const resolved = await resolveConfiguredAuthContext(auth, ctx, config);
229
+ if (resolved === null) {
230
+ if (config?.optional !== true) throw createNotSignedInError();
231
+ return {
232
+ ctx: { auth: {
233
+ getUserIdentity,
234
+ ...createUnauthenticatedAuthContext()
235
+ } },
236
+ args: {}
237
+ };
238
+ }
203
239
  const extra = config?.resolve ? await config.resolve(ctx, resolved.user, resolved) : {};
204
240
  return {
205
241
  ctx: { auth: {
@@ -214,5 +250,5 @@ function AuthCtx(auth, config) {
214
250
  }
215
251
 
216
252
  //#endregion
217
- export { AuthCtx, createAuth };
253
+ export { createAuth };
218
254
  //# sourceMappingURL=auth.js.map