@robelest/convex-auth 0.0.3-preview → 0.0.3-preview.3

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 (304) hide show
  1. package/dist/bin.cjs +15 -15
  2. package/dist/client/index.d.ts +40 -12
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js +73 -12
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/component/_generated/api.d.ts +2 -2
  7. package/dist/component/_generated/api.d.ts.map +1 -1
  8. package/dist/component/_generated/component.d.ts +1 -1
  9. package/dist/component/_generated/component.d.ts.map +1 -1
  10. package/dist/component/{portalBridge.d.ts → bridge.d.ts} +2 -2
  11. package/dist/component/bridge.d.ts.map +1 -0
  12. package/dist/component/{portalBridge.js → bridge.js} +2 -2
  13. package/dist/component/bridge.js.map +1 -0
  14. package/dist/component/index.d.ts +11 -4
  15. package/dist/component/index.d.ts.map +1 -1
  16. package/dist/component/index.js +8 -2
  17. package/dist/component/index.js.map +1 -1
  18. package/dist/component/public.d.ts +24 -17
  19. package/dist/component/public.d.ts.map +1 -1
  20. package/dist/component/public.js +23 -4
  21. package/dist/component/public.js.map +1 -1
  22. package/dist/component/schema.d.ts +11 -7
  23. package/dist/component/schema.d.ts.map +1 -1
  24. package/dist/component/schema.js +4 -1
  25. package/dist/component/schema.js.map +1 -1
  26. package/dist/providers/anonymous.d.ts +3 -0
  27. package/dist/providers/anonymous.d.ts.map +1 -1
  28. package/dist/providers/anonymous.js +3 -0
  29. package/dist/providers/anonymous.js.map +1 -1
  30. package/dist/providers/credentials.d.ts +3 -0
  31. package/dist/providers/credentials.d.ts.map +1 -1
  32. package/dist/providers/credentials.js +3 -0
  33. package/dist/providers/credentials.js.map +1 -1
  34. package/dist/providers/email.d.ts +3 -0
  35. package/dist/providers/email.d.ts.map +1 -1
  36. package/dist/providers/email.js +3 -0
  37. package/dist/providers/email.js.map +1 -1
  38. package/dist/providers/passkey.d.ts +7 -1
  39. package/dist/providers/passkey.d.ts.map +1 -1
  40. package/dist/providers/passkey.js +7 -1
  41. package/dist/providers/passkey.js.map +1 -1
  42. package/dist/providers/password.d.ts +3 -0
  43. package/dist/providers/password.d.ts.map +1 -1
  44. package/dist/providers/password.js +3 -0
  45. package/dist/providers/password.js.map +1 -1
  46. package/dist/providers/phone.d.ts +3 -0
  47. package/dist/providers/phone.d.ts.map +1 -1
  48. package/dist/providers/phone.js +3 -0
  49. package/dist/providers/phone.js.map +1 -1
  50. package/dist/providers/totp.d.ts +8 -0
  51. package/dist/providers/totp.d.ts.map +1 -1
  52. package/dist/providers/totp.js +8 -0
  53. package/dist/providers/totp.js.map +1 -1
  54. package/dist/server/{convex-auth.d.ts → auth.d.ts} +226 -36
  55. package/dist/server/auth.d.ts.map +1 -0
  56. package/dist/server/{convex-auth.js → auth.js} +287 -111
  57. package/dist/server/auth.js.map +1 -0
  58. package/dist/server/errors.d.ts +148 -0
  59. package/dist/server/errors.d.ts.map +1 -0
  60. package/dist/server/errors.js +179 -0
  61. package/dist/server/errors.js.map +1 -0
  62. package/dist/server/implementation/index.d.ts +170 -48
  63. package/dist/server/implementation/index.d.ts.map +1 -1
  64. package/dist/server/implementation/index.js +383 -167
  65. package/dist/server/implementation/index.js.map +1 -1
  66. package/dist/server/implementation/{apiKey.d.ts → keys.d.ts} +1 -1
  67. package/dist/server/implementation/keys.d.ts.map +1 -0
  68. package/dist/server/implementation/{apiKey.js → keys.js} +4 -5
  69. package/dist/server/implementation/keys.js.map +1 -0
  70. package/dist/server/implementation/mutations/{modifyAccount.d.ts → account.d.ts} +3 -3
  71. package/dist/server/implementation/mutations/account.d.ts.map +1 -0
  72. package/dist/server/implementation/mutations/{modifyAccount.js → account.js} +4 -3
  73. package/dist/server/implementation/mutations/account.js.map +1 -0
  74. package/dist/server/implementation/mutations/{createVerificationCode.d.ts → code.d.ts} +1 -1
  75. package/dist/server/implementation/mutations/code.d.ts.map +1 -0
  76. package/dist/server/implementation/mutations/{createVerificationCode.js → code.js} +2 -2
  77. package/dist/server/implementation/mutations/code.js.map +1 -0
  78. package/dist/server/implementation/mutations/index.d.ts +33 -33
  79. package/dist/server/implementation/mutations/index.d.ts.map +1 -1
  80. package/dist/server/implementation/mutations/index.js +22 -22
  81. package/dist/server/implementation/mutations/index.js.map +1 -1
  82. package/dist/server/implementation/mutations/{invalidateSessions.d.ts → invalidate.d.ts} +1 -1
  83. package/dist/server/implementation/mutations/invalidate.d.ts.map +1 -0
  84. package/dist/server/implementation/mutations/{invalidateSessions.js → invalidate.js} +2 -2
  85. package/dist/server/implementation/mutations/invalidate.js.map +1 -0
  86. package/dist/server/implementation/mutations/{userOAuth.d.ts → oauth.d.ts} +3 -3
  87. package/dist/server/implementation/mutations/oauth.d.ts.map +1 -0
  88. package/dist/server/implementation/mutations/{userOAuth.js → oauth.js} +4 -3
  89. package/dist/server/implementation/mutations/oauth.js.map +1 -0
  90. package/dist/server/implementation/mutations/{refreshSession.d.ts → refresh.d.ts} +1 -1
  91. package/dist/server/implementation/mutations/refresh.d.ts.map +1 -0
  92. package/dist/server/implementation/mutations/{refreshSession.js → refresh.js} +3 -3
  93. package/dist/server/implementation/mutations/refresh.js.map +1 -0
  94. package/dist/server/implementation/mutations/{createAccountFromCredentials.d.ts → register.d.ts} +4 -4
  95. package/dist/server/implementation/mutations/register.d.ts.map +1 -0
  96. package/dist/server/implementation/mutations/{createAccountFromCredentials.js → register.js} +4 -3
  97. package/dist/server/implementation/mutations/register.js.map +1 -0
  98. package/dist/server/implementation/mutations/{retrieveAccountWithCredentials.d.ts → retrieve.d.ts} +3 -3
  99. package/dist/server/implementation/mutations/retrieve.d.ts.map +1 -0
  100. package/dist/server/implementation/mutations/{retrieveAccountWithCredentials.js → retrieve.js} +3 -3
  101. package/dist/server/implementation/mutations/retrieve.js.map +1 -0
  102. package/dist/server/implementation/mutations/{verifierSignature.d.ts → signature.d.ts} +1 -1
  103. package/dist/server/implementation/mutations/signature.d.ts.map +1 -0
  104. package/dist/server/implementation/mutations/{verifierSignature.js → signature.js} +4 -3
  105. package/dist/server/implementation/mutations/signature.js.map +1 -0
  106. package/dist/server/implementation/mutations/{signIn.d.ts → signin.d.ts} +1 -1
  107. package/dist/server/implementation/mutations/{signIn.d.ts.map → signin.d.ts.map} +1 -1
  108. package/dist/server/implementation/mutations/{signIn.js → signin.js} +2 -2
  109. package/dist/server/implementation/mutations/{signIn.js.map → signin.js.map} +1 -1
  110. package/dist/server/implementation/mutations/{signOut.d.ts → signout.d.ts} +1 -1
  111. package/dist/server/implementation/mutations/{signOut.d.ts.map → signout.d.ts.map} +1 -1
  112. package/dist/server/implementation/mutations/{signOut.js → signout.js} +2 -2
  113. package/dist/server/implementation/mutations/{signOut.js.map → signout.js.map} +1 -1
  114. package/dist/server/implementation/mutations/{storeRef.d.ts → store.d.ts} +1 -1
  115. package/dist/server/implementation/mutations/store.d.ts.map +1 -0
  116. package/dist/server/implementation/mutations/{storeRef.js → store.js} +1 -1
  117. package/dist/server/implementation/mutations/store.js.map +1 -0
  118. package/dist/server/implementation/mutations/verifier.js +1 -1
  119. package/dist/server/implementation/mutations/verifier.js.map +1 -1
  120. package/dist/server/implementation/mutations/{verifyCodeAndSignIn.d.ts → verify.d.ts} +1 -1
  121. package/dist/server/implementation/mutations/verify.d.ts.map +1 -0
  122. package/dist/server/implementation/mutations/{verifyCodeAndSignIn.js → verify.js} +3 -3
  123. package/dist/server/implementation/mutations/verify.js.map +1 -0
  124. package/dist/server/implementation/passkey.d.ts.map +1 -1
  125. package/dist/server/implementation/passkey.js +47 -55
  126. package/dist/server/implementation/passkey.js.map +1 -1
  127. package/dist/server/implementation/provider.d.ts.map +1 -1
  128. package/dist/server/implementation/provider.js +5 -4
  129. package/dist/server/implementation/provider.js.map +1 -1
  130. package/dist/server/implementation/{rateLimit.d.ts → ratelimit.d.ts} +1 -1
  131. package/dist/server/implementation/{rateLimit.d.ts.map → ratelimit.d.ts.map} +1 -1
  132. package/dist/server/implementation/{rateLimit.js → ratelimit.js} +1 -1
  133. package/dist/server/implementation/{rateLimit.js.map → ratelimit.js.map} +1 -1
  134. package/dist/server/implementation/redirects.d.ts.map +1 -1
  135. package/dist/server/implementation/redirects.js +2 -1
  136. package/dist/server/implementation/redirects.js.map +1 -1
  137. package/dist/server/implementation/{refreshTokens.d.ts → refresh.d.ts} +1 -1
  138. package/dist/server/implementation/refresh.d.ts.map +1 -0
  139. package/dist/server/implementation/{refreshTokens.js → refresh.js} +3 -2
  140. package/dist/server/implementation/refresh.js.map +1 -0
  141. package/dist/server/implementation/sessions.js +1 -1
  142. package/dist/server/implementation/sessions.js.map +1 -1
  143. package/dist/server/implementation/{signIn.d.ts → signin.d.ts} +1 -1
  144. package/dist/server/implementation/{signIn.d.ts.map → signin.d.ts.map} +1 -1
  145. package/dist/server/implementation/{signIn.js → signin.js} +12 -8
  146. package/dist/server/implementation/signin.js.map +1 -0
  147. package/dist/server/implementation/totp.d.ts.map +1 -1
  148. package/dist/server/implementation/totp.js +29 -29
  149. package/dist/server/implementation/totp.js.map +1 -1
  150. package/dist/server/implementation/types.d.ts +131 -1
  151. package/dist/server/implementation/types.d.ts.map +1 -1
  152. package/dist/server/implementation/types.js +65 -1
  153. package/dist/server/implementation/types.js.map +1 -1
  154. package/dist/server/implementation/users.d.ts.map +1 -1
  155. package/dist/server/implementation/users.js +3 -2
  156. package/dist/server/implementation/users.js.map +1 -1
  157. package/dist/server/index.d.ts +131 -1
  158. package/dist/server/index.d.ts.map +1 -1
  159. package/dist/server/index.js +117 -1
  160. package/dist/server/index.js.map +1 -1
  161. package/dist/server/oauth/{authorizationUrl.d.ts → authorization.d.ts} +1 -1
  162. package/dist/server/oauth/authorization.d.ts.map +1 -0
  163. package/dist/server/oauth/{authorizationUrl.js → authorization.js} +4 -3
  164. package/dist/server/oauth/authorization.js.map +1 -0
  165. package/dist/server/oauth/callback.d.ts.map +1 -1
  166. package/dist/server/oauth/callback.js +7 -6
  167. package/dist/server/oauth/callback.js.map +1 -1
  168. package/dist/server/oauth/checks.d.ts.map +1 -1
  169. package/dist/server/oauth/checks.js +2 -1
  170. package/dist/server/oauth/checks.js.map +1 -1
  171. package/dist/server/oauth/{convexAuth.d.ts → helpers.d.ts} +1 -1
  172. package/dist/server/oauth/helpers.d.ts.map +1 -0
  173. package/dist/server/oauth/{convexAuth.js → helpers.js} +6 -5
  174. package/dist/server/oauth/helpers.js.map +1 -0
  175. package/dist/server/oauth/lib/utils/{customFetch.d.ts → fetch.d.ts} +1 -1
  176. package/dist/server/oauth/lib/utils/fetch.d.ts.map +1 -0
  177. package/dist/server/oauth/lib/utils/{customFetch.js → fetch.js} +1 -1
  178. package/dist/server/oauth/lib/utils/fetch.js.map +1 -0
  179. package/dist/server/{provider_utils.d.ts → providers.d.ts} +1 -1
  180. package/dist/server/providers.d.ts.map +1 -0
  181. package/dist/server/{provider_utils.js → providers.js} +1 -1
  182. package/dist/server/providers.js.map +1 -0
  183. package/dist/server/{email-templates.d.ts → templates.d.ts} +8 -1
  184. package/dist/server/templates.d.ts.map +1 -0
  185. package/dist/server/{portal-email.js → templates.js} +74 -3
  186. package/dist/server/templates.js.map +1 -0
  187. package/dist/server/types.d.ts +88 -5
  188. package/dist/server/types.d.ts.map +1 -1
  189. package/dist/server/utils.d.ts.map +1 -1
  190. package/dist/server/utils.js +2 -1
  191. package/dist/server/utils.js.map +1 -1
  192. package/dist/server/version.d.ts +1 -1
  193. package/dist/server/version.d.ts.map +1 -1
  194. package/dist/server/version.js +1 -1
  195. package/dist/server/version.js.map +1 -1
  196. package/package.json +5 -1
  197. package/src/cli/index.ts +5 -5
  198. package/src/cli/{portal-link.ts → link.ts} +1 -1
  199. package/src/cli/utils.ts +1 -1
  200. package/src/client/index.ts +102 -17
  201. package/src/component/_generated/api.ts +2 -2
  202. package/src/component/_generated/component.ts +1 -1
  203. package/src/component/{portalBridge.ts → bridge.ts} +2 -2
  204. package/src/component/index.ts +10 -2
  205. package/src/component/public.ts +25 -4
  206. package/src/component/schema.ts +4 -1
  207. package/src/providers/anonymous.ts +3 -0
  208. package/src/providers/credentials.ts +3 -0
  209. package/src/providers/email.ts +3 -0
  210. package/src/providers/passkey.ts +8 -1
  211. package/src/providers/password.ts +3 -0
  212. package/src/providers/phone.ts +3 -0
  213. package/src/providers/totp.ts +9 -0
  214. package/src/server/auth.ts +969 -0
  215. package/src/server/errors.ts +275 -0
  216. package/src/server/implementation/index.ts +370 -88
  217. package/src/server/implementation/{apiKey.ts → keys.ts} +7 -6
  218. package/src/server/implementation/mutations/{modifyAccount.ts → account.ts} +3 -4
  219. package/src/server/implementation/mutations/{createVerificationCode.ts → code.ts} +1 -1
  220. package/src/server/implementation/mutations/index.ts +22 -22
  221. package/src/server/implementation/mutations/{invalidateSessions.ts → invalidate.ts} +1 -1
  222. package/src/server/implementation/mutations/{userOAuth.ts → oauth.ts} +3 -2
  223. package/src/server/implementation/mutations/{refreshSession.ts → refresh.ts} +2 -2
  224. package/src/server/implementation/mutations/{createAccountFromCredentials.ts → register.ts} +3 -2
  225. package/src/server/implementation/mutations/{retrieveAccountWithCredentials.ts → retrieve.ts} +2 -2
  226. package/src/server/implementation/mutations/{verifierSignature.ts → signature.ts} +3 -2
  227. package/src/server/implementation/mutations/{signIn.ts → signin.ts} +1 -1
  228. package/src/server/implementation/mutations/{signOut.ts → signout.ts} +1 -1
  229. package/src/server/implementation/mutations/verifier.ts +1 -1
  230. package/src/server/implementation/mutations/{verifyCodeAndSignIn.ts → verify.ts} +2 -2
  231. package/src/server/implementation/passkey.ts +86 -116
  232. package/src/server/implementation/provider.ts +5 -8
  233. package/src/server/implementation/redirects.ts +2 -3
  234. package/src/server/implementation/{refreshTokens.ts → refresh.ts} +2 -1
  235. package/src/server/implementation/sessions.ts +1 -1
  236. package/src/server/implementation/{signIn.ts → signin.ts} +13 -11
  237. package/src/server/implementation/totp.ts +60 -84
  238. package/src/server/implementation/types.ts +316 -1
  239. package/src/server/implementation/users.ts +4 -7
  240. package/src/server/index.ts +142 -3
  241. package/src/server/oauth/{authorizationUrl.ts → authorization.ts} +3 -2
  242. package/src/server/oauth/callback.ts +7 -6
  243. package/src/server/oauth/checks.ts +3 -1
  244. package/src/server/oauth/{convexAuth.ts → helpers.ts} +8 -5
  245. package/src/server/{portal-email.ts → templates.ts} +78 -2
  246. package/src/server/types.ts +133 -4
  247. package/src/server/utils.ts +3 -1
  248. package/src/server/version.ts +1 -1
  249. package/dist/component/portalBridge.d.ts.map +0 -1
  250. package/dist/component/portalBridge.js.map +0 -1
  251. package/dist/server/convex-auth.d.ts.map +0 -1
  252. package/dist/server/convex-auth.js.map +0 -1
  253. package/dist/server/convex_types.d.ts +0 -17
  254. package/dist/server/convex_types.d.ts.map +0 -1
  255. package/dist/server/convex_types.js +0 -2
  256. package/dist/server/convex_types.js.map +0 -1
  257. package/dist/server/email-templates.d.ts.map +0 -1
  258. package/dist/server/email-templates.js +0 -74
  259. package/dist/server/email-templates.js.map +0 -1
  260. package/dist/server/implementation/apiKey.d.ts.map +0 -1
  261. package/dist/server/implementation/apiKey.js.map +0 -1
  262. package/dist/server/implementation/mutations/createAccountFromCredentials.d.ts.map +0 -1
  263. package/dist/server/implementation/mutations/createAccountFromCredentials.js.map +0 -1
  264. package/dist/server/implementation/mutations/createVerificationCode.d.ts.map +0 -1
  265. package/dist/server/implementation/mutations/createVerificationCode.js.map +0 -1
  266. package/dist/server/implementation/mutations/invalidateSessions.d.ts.map +0 -1
  267. package/dist/server/implementation/mutations/invalidateSessions.js.map +0 -1
  268. package/dist/server/implementation/mutations/modifyAccount.d.ts.map +0 -1
  269. package/dist/server/implementation/mutations/modifyAccount.js.map +0 -1
  270. package/dist/server/implementation/mutations/refreshSession.d.ts.map +0 -1
  271. package/dist/server/implementation/mutations/refreshSession.js.map +0 -1
  272. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.d.ts.map +0 -1
  273. package/dist/server/implementation/mutations/retrieveAccountWithCredentials.js.map +0 -1
  274. package/dist/server/implementation/mutations/storeRef.d.ts.map +0 -1
  275. package/dist/server/implementation/mutations/storeRef.js.map +0 -1
  276. package/dist/server/implementation/mutations/userOAuth.d.ts.map +0 -1
  277. package/dist/server/implementation/mutations/userOAuth.js.map +0 -1
  278. package/dist/server/implementation/mutations/verifierSignature.d.ts.map +0 -1
  279. package/dist/server/implementation/mutations/verifierSignature.js.map +0 -1
  280. package/dist/server/implementation/mutations/verifyCodeAndSignIn.d.ts.map +0 -1
  281. package/dist/server/implementation/mutations/verifyCodeAndSignIn.js.map +0 -1
  282. package/dist/server/implementation/refreshTokens.d.ts.map +0 -1
  283. package/dist/server/implementation/refreshTokens.js.map +0 -1
  284. package/dist/server/implementation/signIn.js.map +0 -1
  285. package/dist/server/oauth/authorizationUrl.d.ts.map +0 -1
  286. package/dist/server/oauth/authorizationUrl.js.map +0 -1
  287. package/dist/server/oauth/convexAuth.d.ts.map +0 -1
  288. package/dist/server/oauth/convexAuth.js.map +0 -1
  289. package/dist/server/oauth/lib/utils/customFetch.d.ts.map +0 -1
  290. package/dist/server/oauth/lib/utils/customFetch.js.map +0 -1
  291. package/dist/server/portal-email.d.ts +0 -19
  292. package/dist/server/portal-email.d.ts.map +0 -1
  293. package/dist/server/portal-email.js.map +0 -1
  294. package/dist/server/provider_utils.d.ts.map +0 -1
  295. package/dist/server/provider_utils.js.map +0 -1
  296. package/src/server/convex-auth.ts +0 -602
  297. package/src/server/convex_types.ts +0 -55
  298. package/src/server/email-templates.ts +0 -77
  299. /package/src/cli/{generateKeys.ts → keys.ts} +0 -0
  300. /package/src/cli/{portal-upload.ts → upload.ts} +0 -0
  301. /package/src/server/implementation/mutations/{storeRef.ts → store.ts} +0 -0
  302. /package/src/server/implementation/{rateLimit.ts → ratelimit.ts} +0 -0
  303. /package/src/server/oauth/lib/utils/{customFetch.ts → fetch.ts} +0 -0
  304. /package/src/server/{provider_utils.ts → providers.ts} +0 -0
@@ -1,4 +1,5 @@
1
1
  import { ConvexHttpClient } from "convex/browser";
2
+ import { ConvexError } from "convex/values";
2
3
  import { jwtDecode } from "jwt-decode";
3
4
  import { parse, serialize } from "cookie";
4
5
  import type {
@@ -7,13 +8,19 @@ import type {
7
8
  } from "./implementation/index.js";
8
9
  import { isLocalHost } from "./utils.js";
9
10
 
11
+ /** Cookie lifetime configuration for auth tokens. */
10
12
  export type AuthCookieConfig = {
13
+ /** Maximum age in seconds, or `null` for session cookies. */
11
14
  maxAge: number | null;
12
15
  };
13
16
 
17
+ /** Raw cookie values extracted from a request. */
14
18
  export type AuthCookies = {
19
+ /** The JWT access token, or `null` when absent. */
15
20
  token: string | null;
21
+ /** The refresh token, or `null` when absent. */
16
22
  refreshToken: string | null;
23
+ /** The OAuth PKCE verifier, or `null` when absent. */
17
24
  verifier: string | null;
18
25
  };
19
26
 
@@ -31,12 +38,28 @@ export type AuthCookie = {
31
38
  };
32
39
  };
33
40
 
41
+ /**
42
+ * Options for the SSR auth helper returned by {@link server}.
43
+ */
34
44
  export type ServerOptions = {
35
- /** Convex deployment URL. */
45
+ /** Convex deployment URL (e.g. `https://your-app.convex.cloud`). */
36
46
  url: string;
47
+ /**
48
+ * Path the client POSTs auth actions to. Defaults to `"/api/auth"`.
49
+ * Must match the `proxy` option on the client.
50
+ */
37
51
  apiRoute?: string;
52
+ /** Cookie `maxAge` in seconds, or `null` for session cookies. */
38
53
  cookieMaxAge?: number | null;
54
+ /** Enable verbose debug logging for token refresh and cookie operations. */
39
55
  verbose?: boolean;
56
+ /**
57
+ * Control whether `refresh()` handles OAuth `?code=` query parameters.
58
+ *
59
+ * - `true` (default): always exchange the code on GET requests with `text/html` accept.
60
+ * - `false`: never exchange — useful when only the client handles codes.
61
+ * - A function: called with the `Request` for per-request decisions.
62
+ */
40
63
  shouldHandleCode?: ((request: Request) => boolean | Promise<boolean>) | boolean;
41
64
  };
42
65
 
@@ -49,6 +72,15 @@ export type RefreshResult = {
49
72
  token: string | null;
50
73
  };
51
74
 
75
+ /**
76
+ * Derive the cookie names used for auth tokens.
77
+ *
78
+ * On localhost the names are unprefixed; on production hosts they
79
+ * use the `__Host-` prefix for tighter security.
80
+ *
81
+ * @param host - The `Host` header value. Omit to use unprefixed names.
82
+ * @returns An object with `token`, `refreshToken`, and `verifier` cookie names.
83
+ */
52
84
  export function authCookieNames(host?: string) {
53
85
  const prefix = isLocalHost(host) ? "" : "__Host-";
54
86
  return {
@@ -58,6 +90,13 @@ export function authCookieNames(host?: string) {
58
90
  };
59
91
  }
60
92
 
93
+ /**
94
+ * Parse auth cookie values from a raw `Cookie` header string.
95
+ *
96
+ * @param cookieHeader - The raw `Cookie` header, or `null`/`undefined`.
97
+ * @param host - The `Host` header, used to determine cookie name prefixes.
98
+ * @returns Parsed {@link AuthCookies} with `token`, `refreshToken`, and `verifier`.
99
+ */
61
100
  export function parseAuthCookies(
62
101
  cookieHeader: string | null | undefined,
63
102
  host?: string,
@@ -71,6 +110,16 @@ export function parseAuthCookies(
71
110
  };
72
111
  }
73
112
 
113
+ /**
114
+ * Serialize auth cookies into `Set-Cookie` header strings.
115
+ *
116
+ * Nulled-out values produce deletion cookies (maxAge 0, expired date).
117
+ *
118
+ * @param cookies - The auth cookie values to serialize.
119
+ * @param host - The `Host` header, used for cookie name prefixes and `Secure` flag.
120
+ * @param config - Cookie lifetime config. Defaults to session cookies.
121
+ * @returns An array of three `Set-Cookie` header strings.
122
+ */
74
123
  export function serializeAuthCookies(
75
124
  cookies: AuthCookies,
76
125
  host?: string,
@@ -155,6 +204,16 @@ export function structuredAuthCookies(
155
204
  ];
156
205
  }
157
206
 
207
+ /**
208
+ * Check whether a request pathname matches the auth proxy route.
209
+ *
210
+ * Handles trailing-slash ambiguity: both `/api/auth` and `/api/auth/`
211
+ * match regardless of how `apiRoute` is configured.
212
+ *
213
+ * @param pathname - The request URL pathname.
214
+ * @param apiRoute - The configured proxy route (e.g. `"/api/auth"`).
215
+ * @returns `true` when the pathname matches the proxy route.
216
+ */
158
217
  export function shouldProxyAuthAction(pathname: string, apiRoute: string) {
159
218
  if (apiRoute.endsWith("/")) {
160
219
  return pathname === apiRoute || pathname === apiRoute.slice(0, -1);
@@ -167,6 +226,39 @@ const MINIMUM_REQUIRED_TOKEN_LIFETIME_MS = 10_000;
167
226
 
168
227
  type DecodedToken = { exp?: number; iat?: number };
169
228
 
229
+ /**
230
+ * Create an SSR auth helper for server-side frameworks.
231
+ *
232
+ * Handles cookie-based token management, OAuth code exchange,
233
+ * and automatic JWT refresh on page loads. Works with any
234
+ * framework that gives you a `Request` object — SvelteKit,
235
+ * TanStack Start, Remix, Next.js, etc.
236
+ *
237
+ * @param options - SSR configuration (Convex URL, proxy route, cookie lifetime).
238
+ * @returns An object with `token`, `verify`, `proxy`, and `refresh` methods.
239
+ *
240
+ * @example SvelteKit hooks
241
+ * ```ts
242
+ * // src/hooks.server.ts
243
+ * import { server } from '@robelest/convex-auth/server';
244
+ *
245
+ * const auth = server({ url: CONVEX_URL });
246
+ *
247
+ * export const handle = async ({ event, resolve }) => {
248
+ * const { cookies, token } = await auth.refresh(event.request);
249
+ * for (const c of cookies) event.cookies.set(c.name, c.value, c.options);
250
+ * event.locals.token = token;
251
+ * return resolve(event);
252
+ * };
253
+ * ```
254
+ *
255
+ * @example Generic proxy endpoint
256
+ * ```ts
257
+ * if (shouldProxyAuthAction(url.pathname, '/api/auth')) {
258
+ * return auth.proxy(request);
259
+ * }
260
+ * ```
261
+ */
170
262
  export function server(options: ServerOptions) {
171
263
  const convexUrl = options.url;
172
264
  const apiRoute = options.apiRoute ?? "/api/auth";
@@ -285,10 +377,25 @@ export function server(options: ServerOptions) {
285
377
  };
286
378
 
287
379
  return {
380
+ /**
381
+ * Read the JWT from the request cookies without any validation.
382
+ *
383
+ * @param request - The incoming HTTP request.
384
+ * @returns The raw JWT string, or `null` when no token cookie exists.
385
+ */
288
386
  token(request: Request): string | null {
289
387
  return parseRequestCookies(request).token;
290
388
  },
291
389
 
390
+ /**
391
+ * Check whether the request carries a non-expired JWT.
392
+ *
393
+ * Performs local expiration checking only (no network call).
394
+ * Use for lightweight auth guards in middleware.
395
+ *
396
+ * @param request - The incoming HTTP request.
397
+ * @returns `true` when a valid, non-expired JWT exists in the cookies.
398
+ */
292
399
  async verify(request: Request): Promise<boolean> {
293
400
  const token = parseRequestCookies(request).token;
294
401
  if (token === null) {
@@ -301,6 +408,17 @@ export function server(options: ServerOptions) {
301
408
  return decodedToken.exp * 1000 > Date.now();
302
409
  },
303
410
 
411
+ /**
412
+ * Handle a proxied `signIn` or `signOut` POST from the client.
413
+ *
414
+ * Validates the route, method, and origin, then forwards the
415
+ * action to Convex and returns a `Response` with updated
416
+ * `Set-Cookie` headers. The client never sees the real
417
+ * refresh token — it stays in httpOnly cookies.
418
+ *
419
+ * @param request - The incoming POST request from the client.
420
+ * @returns A JSON `Response` with auth result and cookie headers.
421
+ */
304
422
  async proxy(request: Request): Promise<Response> {
305
423
  const requestUrl = new URL(request.url);
306
424
  if (!shouldProxyAuthAction(requestUrl.pathname, apiRoute)) {
@@ -377,8 +495,16 @@ export function server(options: ServerOptions) {
377
495
  );
378
496
  }
379
497
  return jsonResponse(result);
380
- } catch (error) {
381
- const response = jsonResponse({ error: (error as Error).message }, 400);
498
+ } catch (error: unknown) {
499
+ // Forward structured error data when available (ConvexError with { code, message }).
500
+ const errorBody =
501
+ error instanceof ConvexError &&
502
+ typeof error.data === "object" &&
503
+ error.data !== null &&
504
+ "code" in error.data
505
+ ? { error: (error.data as { message?: string }).message ?? String(error), authError: error.data }
506
+ : { error: error instanceof Error ? error.message : String(error) };
507
+ const response = jsonResponse(errorBody, 400);
382
508
  return attachCookies(
383
509
  response,
384
510
  serializeAuthCookies(
@@ -415,6 +541,19 @@ export function server(options: ServerOptions) {
415
541
  );
416
542
  },
417
543
 
544
+ /**
545
+ * Refresh auth tokens on page load.
546
+ *
547
+ * Call this in your server hooks/middleware on every request.
548
+ * It handles three scenarios:
549
+ *
550
+ * 1. **OAuth code exchange** — exchanges a `?code=` query param for tokens and returns a redirect URL.
551
+ * 2. **Token refresh** — refreshes the JWT if it's close to expiry.
552
+ * 3. **No-op** — returns the existing token when no refresh is needed.
553
+ *
554
+ * @param request - The incoming HTTP request.
555
+ * @returns Structured cookies to set on the response, an optional redirect URL, and the current JWT.
556
+ */
418
557
  async refresh(request: Request): Promise<RefreshResult> {
419
558
  const host = cookieHost(request);
420
559
  const currentToken = parseRequestCookies(request).token;
@@ -4,9 +4,10 @@ import { InternalOptions } from "./types.js";
4
4
  import {
5
5
  callbackUrl,
6
6
  getAuthorizationSignature,
7
- } from "./convexAuth.js";
7
+ } from "./helpers.js";
8
8
  import { Cookie } from "@auth/core/lib/utils/cookie.js";
9
9
  import { logWithLevel } from "../implementation/utils.js";
10
+ import { throwAuthError } from "../errors.js";
10
11
 
11
12
  /**
12
13
  * Generates an authorization/request token URL.
@@ -25,7 +26,7 @@ export async function getAuthorizationUrl(
25
26
  const { as, authorization: authorizationEndpoint, configSource } = provider;
26
27
 
27
28
  if (!authorizationEndpoint) {
28
- throw new TypeError("Could not determine the authorization endpoint.");
29
+ throwAuthError("PROVIDER_NOT_CONFIGURED", "Could not determine the authorization endpoint.");
29
30
  }
30
31
  if (!url) {
31
32
  url = new URL(authorizationEndpoint.url);
@@ -3,7 +3,7 @@
3
3
  import * as checks from "./checks.js";
4
4
  import * as o from "oauth4webapi";
5
5
  import { InternalOptions } from "./types.js";
6
- import { fetchOpt } from "./lib/utils/customFetch.js";
6
+ import { fetchOpt } from "./lib/utils/fetch.js";
7
7
  import { Cookie } from "@auth/core/lib/utils/cookie.js";
8
8
  import { logWithLevel } from "../implementation/utils.js";
9
9
  import { Account, Profile, TokenSet } from "@auth/core/types.js";
@@ -11,7 +11,8 @@ import { isOIDCProvider } from "./lib/utils/providers.js";
11
11
  import {
12
12
  callbackUrl,
13
13
  getAuthorizationSignature,
14
- } from "./convexAuth.js";
14
+ } from "./helpers.js";
15
+ import { throwAuthError } from "../errors.js";
15
16
 
16
17
  function formUrlEncode(token: string) {
17
18
  return encodeURIComponent(token).replace(/%20/g, "+");
@@ -88,7 +89,7 @@ export async function handleOAuth(
88
89
  });
89
90
  break;
90
91
  default:
91
- throw new Error("unsupported client authentication method");
92
+ throwAuthError("OAUTH_UNSUPPORTED_AUTH_METHOD");
92
93
  }
93
94
 
94
95
  const resCookies: Cookie[] = [];
@@ -110,7 +111,7 @@ export async function handleOAuth(
110
111
  ...Object.fromEntries(err.cause.entries()),
111
112
  };
112
113
  logWithLevel("DEBUG", "OAuthCallbackError", cause);
113
- throw new Error("OAuth Provider returned an error", { cause });
114
+ throwAuthError("OAUTH_PROVIDER_ERROR", "OAuth provider returned an error", { cause: JSON.stringify(cause) });
114
115
  }
115
116
  throw err;
116
117
  }
@@ -174,7 +175,7 @@ export async function handleOAuth(
174
175
  processedCodeResponse,
175
176
  );
176
177
  if (idTokenClaimsOrUndefined === undefined) {
177
- throw new Error("ID Token claims are missing");
178
+ throwAuthError("OAUTH_MISSING_ID_TOKEN");
178
179
  }
179
180
  const idTokenClaims = idTokenClaimsOrUndefined;
180
181
  profile = idTokenClaims;
@@ -221,7 +222,7 @@ export async function handleOAuth(
221
222
  );
222
223
  profile = await userinfoResponse.json();
223
224
  } else {
224
- throw new TypeError("No userinfo endpoint configured");
225
+ throwAuthError("OAUTH_NO_USERINFO");
225
226
  }
226
227
  }
227
228
 
@@ -6,6 +6,7 @@ import type { InternalOptions } from "./types.js";
6
6
  import { Cookie } from "@auth/core/lib/utils/cookie.js";
7
7
  import { CookiesOptions } from "@auth/core/types.js";
8
8
  import { logWithLevel } from "../implementation/utils.js";
9
+ import { throwAuthError } from "../errors.js";
9
10
 
10
11
  const COOKIE_TTL = 60 * 15; // 15 minutes
11
12
 
@@ -97,7 +98,8 @@ export const state = {
97
98
  const { provider } = options;
98
99
  if (!provider.checks.includes("state")) {
99
100
  if (origin) {
100
- throw new Error(
101
+ throwAuthError(
102
+ "OAUTH_INVALID_STATE",
101
103
  "State data was provided but the provider is not configured to use state",
102
104
  );
103
105
  }
@@ -2,11 +2,12 @@ import { CookieOption, CookiesOptions } from "@auth/core/types.js";
2
2
  import { requireEnv } from "../utils.js";
3
3
  import { InternalProvider } from "./types.js";
4
4
  import { SHARED_COOKIE_OPTIONS } from "../cookies.js";
5
- import { fetchOpt } from "./lib/utils/customFetch.js";
5
+ import { fetchOpt } from "./lib/utils/fetch.js";
6
6
  import * as o from "oauth4webapi";
7
- import { normalizeEndpoint } from "../provider_utils.js";
7
+ import { normalizeEndpoint } from "../providers.js";
8
8
  import { isLocalHost } from "../utils.js";
9
9
  import { OAuthConfig } from "@auth/core/providers/oauth.js";
10
+ import { throwAuthError } from "../errors.js";
10
11
 
11
12
  // ConvexAuth: The logic for the callback URL is different from Auth.js
12
13
  export function callbackUrl(providerId: string) {
@@ -92,7 +93,8 @@ export async function oAuthConfigToInternalProvider(config: OAuthConfig<any>): P
92
93
  if (!config.authorization || !config.token || !config.userinfo) {
93
94
  // Taken from https://github.com/nextauthjs/next-auth/blob/a7491dcb9355ff2d01fb8e9236636605e2090145/packages/core/src/lib/actions/callback/oauth/callback.ts#L63
94
95
  if (!config.issuer) {
95
- throw new Error(
96
+ throwAuthError(
97
+ "PROVIDER_NOT_CONFIGURED",
96
98
  `Provider \`${config.id}\` is missing an \`issuer\` URL configuration. Consult the provider docs.`,
97
99
  );
98
100
  }
@@ -109,8 +111,9 @@ export async function oAuthConfigToInternalProvider(config: OAuthConfig<any>): P
109
111
  );
110
112
 
111
113
  if (!discoveredAs.token_endpoint)
112
- throw new TypeError(
113
- "TODO: Authorization server did not provide a token endpoint.",
114
+ throwAuthError(
115
+ "PROVIDER_NOT_CONFIGURED",
116
+ "Authorization server did not provide a token endpoint.",
114
117
  );
115
118
 
116
119
  const as: o.AuthorizationServer = discoveredAs;
@@ -1,3 +1,81 @@
1
+ /**
2
+ * Default email templates generated by the Auth library.
3
+ *
4
+ * These are used when the library sends emails on behalf of the developer
5
+ * (magic links, portal admin sign-in). The developer provides the transport
6
+ * via `email.send`; the library provides the content.
7
+ *
8
+ * @module
9
+ */
10
+
11
+ /**
12
+ * Default magic link email template.
13
+ *
14
+ * Clean, minimal design that works across email clients.
15
+ * Used by the auto-registered `email` provider when `email` is
16
+ * configured in the Auth constructor.
17
+ */
18
+ export function defaultMagicLinkEmail(url: string, host: string): string {
19
+ const escapedHost = host.replace(/[&<>"']/g, (c) =>
20
+ ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]!,
21
+ );
22
+
23
+ return `<!DOCTYPE html>
24
+ <html lang="en">
25
+ <head>
26
+ <meta charset="utf-8" />
27
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
28
+ <title>Sign in to ${escapedHost}</title>
29
+ </head>
30
+ <body style="margin:0;padding:0;background-color:#f9fafb;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif;">
31
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background-color:#f9fafb;padding:40px 16px;">
32
+ <tr>
33
+ <td align="center">
34
+ <table role="presentation" width="480" cellpadding="0" cellspacing="0" style="background-color:#ffffff;border:1px solid #e5e7eb;border-radius:8px;overflow:hidden;">
35
+ <tr>
36
+ <td style="padding:32px 32px 0 32px;text-align:center;">
37
+ <h1 style="margin:0 0 8px 0;font-size:20px;font-weight:600;color:#111827;line-height:1.3;">
38
+ Sign in to ${escapedHost}
39
+ </h1>
40
+ </td>
41
+ </tr>
42
+ <tr>
43
+ <td style="padding:24px 32px;">
44
+ <p style="margin:0 0 24px 0;font-size:15px;line-height:1.6;color:#4b5563;text-align:center;">
45
+ Click the button below to sign in. This link will expire shortly.
46
+ </p>
47
+ <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
48
+ <tr>
49
+ <td align="center" style="padding:0 0 24px 0;">
50
+ <a href="${url}" target="_blank" style="display:inline-block;background-color:#111827;color:#ffffff;font-size:15px;font-weight:600;text-decoration:none;padding:12px 32px;border-radius:6px;line-height:1;">
51
+ Sign in
52
+ </a>
53
+ </td>
54
+ </tr>
55
+ </table>
56
+ <p style="margin:0 0 12px 0;font-size:13px;line-height:1.6;color:#9ca3af;">
57
+ If the button doesn't work, copy and paste this URL into your browser:
58
+ </p>
59
+ <p style="margin:0;font-size:13px;line-height:1.5;color:#6b7280;word-break:break-all;">
60
+ ${url}
61
+ </p>
62
+ </td>
63
+ </tr>
64
+ <tr>
65
+ <td style="padding:20px 32px;border-top:1px solid #e5e7eb;">
66
+ <p style="margin:0;font-size:12px;line-height:1.5;color:#9ca3af;text-align:center;">
67
+ If you didn't request this email, you can safely ignore it.
68
+ </p>
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </td>
73
+ </tr>
74
+ </table>
75
+ </body>
76
+ </html>`;
77
+ }
78
+
1
79
  /**
2
80
  * Styled dark-theme magic link email template for the Convex Auth Portal.
3
81
  *
@@ -6,8 +84,6 @@
6
84
  * - Accent: #63a8f8 (Convex blue)
7
85
  * - Text: #ffffff (headings), #b9b1aa (secondary), #8f8780 (muted)
8
86
  * - Border: #4a4743
9
- *
10
- * @module
11
87
  */
12
88
 
13
89
  const SHIELD_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#63a8f8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></svg>`;