@robelest/convex-auth 0.0.4-preview.25 → 0.0.4-preview.28

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 (666) hide show
  1. package/README.md +43 -36
  2. package/dist/bin.js +5765 -4880
  3. package/dist/browser/index.d.ts +30 -0
  4. package/dist/browser/index.js +93 -0
  5. package/dist/browser/locks.js +11 -0
  6. package/dist/browser/navigation.js +14 -0
  7. package/dist/{factors → browser}/passkey.js +23 -32
  8. package/dist/browser/runtime.js +92 -0
  9. package/dist/client/core/types.d.ts +452 -5
  10. package/dist/client/core/types.js +17 -0
  11. package/dist/client/errors.js +19 -0
  12. package/dist/client/factors/device.js +94 -0
  13. package/dist/{factors → client/factors}/totp.js +12 -4
  14. package/dist/client/index.d.ts +47 -1
  15. package/dist/client/index.js +269 -232
  16. package/dist/client/runtime/mutex.js +24 -0
  17. package/dist/client/runtime/proxy.js +30 -0
  18. package/dist/client/runtime/storage.js +45 -0
  19. package/dist/client/services/adapters.js +7 -0
  20. package/dist/client/services/http.js +6 -0
  21. package/dist/client/services/resolve.js +13 -0
  22. package/dist/client/services/runtime.js +6 -0
  23. package/dist/component/_generated/component.d.ts +1355 -1399
  24. package/dist/component/convex.config.d.ts +2 -2
  25. package/dist/component/index.d.ts +4 -26
  26. package/dist/component/index.js +1 -1
  27. package/dist/component/model.d.ts +26 -112
  28. package/dist/component/model.js +76 -54
  29. package/dist/component/modules.js +38 -0
  30. package/dist/component/public/factors/devices.js +1 -1
  31. package/dist/component/public/factors/passkeys.js +1 -1
  32. package/dist/component/public/factors/totp.js +1 -1
  33. package/dist/component/public/groups/core.js +2 -2
  34. package/dist/component/public/groups/invites.js +1 -1
  35. package/dist/component/public/groups/members.js +1 -1
  36. package/dist/component/public/identity/accounts.js +1 -1
  37. package/dist/component/public/identity/codes.js +1 -1
  38. package/dist/component/public/identity/sessions.js +39 -2
  39. package/dist/component/public/identity/tokens.js +82 -4
  40. package/dist/component/public/identity/users.js +1 -1
  41. package/dist/component/public/identity/verifiers.js +10 -4
  42. package/dist/component/public/security/keys.js +1 -1
  43. package/dist/component/public/security/limits.js +1 -1
  44. package/dist/component/public/{enterprise → sso}/audit.js +26 -26
  45. package/dist/component/public/sso/core.js +263 -0
  46. package/dist/component/public/sso/domains.js +280 -0
  47. package/dist/component/public/{enterprise → sso}/scim.js +87 -87
  48. package/dist/component/public/sso/secrets.js +125 -0
  49. package/dist/component/public/{enterprise → sso}/webhooks.js +59 -59
  50. package/dist/component/public.js +9 -9
  51. package/dist/component/schema.d.ts +472 -393
  52. package/dist/component/schema.js +36 -35
  53. package/dist/core/index.d.ts +380 -0
  54. package/dist/core/index.js +83 -0
  55. package/dist/otel.d.ts +69 -0
  56. package/dist/otel.js +82 -0
  57. package/dist/providers/anonymous.d.ts +15 -34
  58. package/dist/providers/anonymous.js +27 -35
  59. package/dist/providers/apple.d.ts +59 -0
  60. package/dist/providers/apple.js +58 -0
  61. package/dist/providers/credentials.d.ts +18 -34
  62. package/dist/providers/credentials.js +16 -27
  63. package/dist/providers/custom.d.ts +94 -0
  64. package/dist/providers/custom.js +119 -0
  65. package/dist/providers/device.d.ts +15 -49
  66. package/dist/providers/device.js +17 -34
  67. package/dist/providers/email.d.ts +21 -38
  68. package/dist/providers/email.js +36 -55
  69. package/dist/providers/github.d.ts +54 -0
  70. package/dist/providers/github.js +75 -0
  71. package/dist/providers/google.d.ts +54 -0
  72. package/dist/providers/google.js +61 -0
  73. package/dist/providers/index.d.ts +16 -12
  74. package/dist/providers/index.js +15 -11
  75. package/dist/providers/microsoft.d.ts +57 -0
  76. package/dist/providers/microsoft.js +101 -0
  77. package/dist/providers/passkey.d.ts +19 -35
  78. package/dist/providers/passkey.js +20 -30
  79. package/dist/providers/password.d.ts +17 -18
  80. package/dist/providers/password.js +121 -143
  81. package/dist/providers/phone.d.ts +13 -28
  82. package/dist/providers/phone.js +21 -46
  83. package/dist/providers/sso.d.ts +16 -36
  84. package/dist/providers/sso.js +21 -22
  85. package/dist/providers/totp.d.ts +13 -29
  86. package/dist/providers/totp.js +17 -27
  87. package/dist/server/auth-context.d.ts +204 -0
  88. package/dist/server/auth-context.js +76 -0
  89. package/dist/server/auth.d.ts +99 -244
  90. package/dist/server/auth.js +56 -152
  91. package/dist/server/componentContext.d.ts +12 -0
  92. package/dist/server/componentContext.js +1 -0
  93. package/dist/server/config.js +6 -67
  94. package/dist/server/constants.js +6 -0
  95. package/dist/server/contract.d.ts +105 -0
  96. package/dist/server/contract.js +43 -0
  97. package/dist/server/cookies.js +3 -2
  98. package/dist/server/core.js +31 -36
  99. package/dist/server/crypto.js +34 -44
  100. package/dist/server/db.js +6 -1
  101. package/dist/server/device.js +96 -130
  102. package/dist/server/env.js +48 -0
  103. package/dist/server/errors.js +20 -0
  104. package/dist/server/http.d.ts +15 -59
  105. package/dist/server/http.js +136 -120
  106. package/dist/server/identity.js +2 -2
  107. package/dist/server/index.d.ts +5 -4
  108. package/dist/server/index.js +3 -3
  109. package/dist/server/keys.js +10 -1
  110. package/dist/server/limits.js +26 -26
  111. package/dist/server/log.js +28 -0
  112. package/dist/server/mounts.d.ts +1107 -296
  113. package/dist/server/mounts.js +315 -196
  114. package/dist/server/mutations/account.js +11 -14
  115. package/dist/server/mutations/code.js +6 -5
  116. package/dist/server/mutations/invalidate.js +9 -11
  117. package/dist/server/mutations/oauth.js +112 -73
  118. package/dist/server/mutations/refresh.js +47 -97
  119. package/dist/server/mutations/register.js +37 -35
  120. package/dist/server/mutations/retrieve.js +16 -16
  121. package/dist/server/mutations/signature.js +15 -18
  122. package/dist/server/mutations/signin.js +10 -5
  123. package/dist/server/mutations/signout.js +11 -14
  124. package/dist/server/mutations/store.js +25 -18
  125. package/dist/server/mutations/verifier.js +11 -8
  126. package/dist/server/mutations/verify.js +53 -41
  127. package/dist/server/oauth/factory.js +44 -0
  128. package/dist/server/oauth/index.js +12 -0
  129. package/dist/server/oauth/runtime.js +248 -0
  130. package/dist/server/passkey.js +331 -365
  131. package/dist/server/payloads.d.ts +16 -0
  132. package/dist/server/payloads.js +30 -0
  133. package/dist/server/{ssr.d.ts → prefetch.d.ts} +2 -2
  134. package/dist/server/prefetch.js +635 -0
  135. package/dist/server/random.js +19 -0
  136. package/dist/server/redirects.js +10 -5
  137. package/dist/server/refresh.js +14 -86
  138. package/dist/server/runtime.d.ts +531 -31
  139. package/dist/server/runtime.js +106 -267
  140. package/dist/server/secret.js +44 -0
  141. package/dist/server/services/config.js +10 -0
  142. package/dist/server/services/group.js +211 -0
  143. package/dist/server/services/logger.js +8 -0
  144. package/dist/server/services/providers.js +22 -0
  145. package/dist/server/services/refresh.js +8 -0
  146. package/dist/server/services/resolve.js +27 -0
  147. package/dist/server/services/signin.js +8 -0
  148. package/dist/server/sessions.js +35 -34
  149. package/dist/server/signin.js +229 -140
  150. package/dist/server/{enterprise → sso}/config.js +10 -3
  151. package/dist/server/sso/domain.d.ts +614 -0
  152. package/dist/server/sso/domain.js +1175 -0
  153. package/dist/server/sso/http.js +1060 -0
  154. package/dist/server/sso/oidc.js +324 -0
  155. package/dist/server/sso/policies.js +59 -0
  156. package/dist/server/sso/policy.js +139 -0
  157. package/dist/server/sso/profile.js +22 -0
  158. package/dist/server/sso/provision.js +179 -0
  159. package/dist/{component/server/enterprise → server/sso}/saml.js +142 -56
  160. package/dist/{component/server/enterprise → server/sso}/scim.js +13 -7
  161. package/dist/server/sso/shared.js +74 -0
  162. package/dist/server/sso/validators.js +88 -0
  163. package/dist/server/sso/webhook.js +94 -0
  164. package/dist/server/tokens.js +16 -4
  165. package/dist/server/totp.js +155 -164
  166. package/dist/server/types.d.ts +306 -296
  167. package/dist/server/types.js +1 -30
  168. package/dist/server/url.js +32 -0
  169. package/dist/server/users.js +74 -40
  170. package/dist/server/utils/cache.js +51 -0
  171. package/dist/server/utils/dispatch.js +36 -0
  172. package/dist/server/utils/retry.js +24 -0
  173. package/dist/server/utils/span.js +32 -0
  174. package/dist/shared/errors.js +19 -0
  175. package/dist/shared/log.js +45 -0
  176. package/{src/test.ts → dist/test.d.ts} +21 -22
  177. package/dist/test.js +51 -0
  178. package/package.json +70 -42
  179. package/dist/authorization/index.d.ts.map +0 -1
  180. package/dist/authorization/index.js.map +0 -1
  181. package/dist/client/core/types.d.ts.map +0 -1
  182. package/dist/client/index.d.ts.map +0 -1
  183. package/dist/client/index.js.map +0 -1
  184. package/dist/component/_generated/api.d.ts +0 -75
  185. package/dist/component/_generated/api.d.ts.map +0 -1
  186. package/dist/component/_generated/api.js.map +0 -1
  187. package/dist/component/_generated/component.d.ts.map +0 -1
  188. package/dist/component/_generated/dataModel.d.ts +0 -42
  189. package/dist/component/_generated/dataModel.d.ts.map +0 -1
  190. package/dist/component/_generated/server.d.ts +0 -117
  191. package/dist/component/_generated/server.d.ts.map +0 -1
  192. package/dist/component/_generated/server.js.map +0 -1
  193. package/dist/component/_virtual/rolldown_runtime.js +0 -18
  194. package/dist/component/client/core/types.d.ts +0 -2
  195. package/dist/component/client/index.d.ts +0 -1
  196. package/dist/component/convex.config.d.ts.map +0 -1
  197. package/dist/component/convex.config.js.map +0 -1
  198. package/dist/component/functions.d.ts +0 -25
  199. package/dist/component/functions.d.ts.map +0 -1
  200. package/dist/component/functions.js.map +0 -1
  201. package/dist/component/index.d.ts.map +0 -1
  202. package/dist/component/model.d.ts.map +0 -1
  203. package/dist/component/model.js.map +0 -1
  204. package/dist/component/providers/anonymous.d.ts +0 -54
  205. package/dist/component/providers/anonymous.d.ts.map +0 -1
  206. package/dist/component/providers/credentials.d.ts +0 -38
  207. package/dist/component/providers/credentials.d.ts.map +0 -1
  208. package/dist/component/providers/device.d.ts +0 -67
  209. package/dist/component/providers/device.d.ts.map +0 -1
  210. package/dist/component/providers/email.d.ts +0 -62
  211. package/dist/component/providers/email.d.ts.map +0 -1
  212. package/dist/component/providers/oauth.d.ts +0 -25
  213. package/dist/component/providers/oauth.d.ts.map +0 -1
  214. package/dist/component/providers/oauth.js +0 -13
  215. package/dist/component/providers/oauth.js.map +0 -1
  216. package/dist/component/providers/passkey.d.ts +0 -57
  217. package/dist/component/providers/passkey.d.ts.map +0 -1
  218. package/dist/component/providers/password.d.ts +0 -88
  219. package/dist/component/providers/password.d.ts.map +0 -1
  220. package/dist/component/providers/phone.d.ts +0 -48
  221. package/dist/component/providers/phone.d.ts.map +0 -1
  222. package/dist/component/providers/sso.d.ts +0 -50
  223. package/dist/component/providers/sso.d.ts.map +0 -1
  224. package/dist/component/providers/totp.d.ts +0 -45
  225. package/dist/component/providers/totp.d.ts.map +0 -1
  226. package/dist/component/public/enterprise/audit.d.ts +0 -73
  227. package/dist/component/public/enterprise/audit.d.ts.map +0 -1
  228. package/dist/component/public/enterprise/audit.js.map +0 -1
  229. package/dist/component/public/enterprise/core.d.ts +0 -176
  230. package/dist/component/public/enterprise/core.d.ts.map +0 -1
  231. package/dist/component/public/enterprise/core.js +0 -292
  232. package/dist/component/public/enterprise/core.js.map +0 -1
  233. package/dist/component/public/enterprise/domains.d.ts +0 -174
  234. package/dist/component/public/enterprise/domains.d.ts.map +0 -1
  235. package/dist/component/public/enterprise/domains.js +0 -271
  236. package/dist/component/public/enterprise/domains.js.map +0 -1
  237. package/dist/component/public/enterprise/scim.d.ts +0 -245
  238. package/dist/component/public/enterprise/scim.d.ts.map +0 -1
  239. package/dist/component/public/enterprise/scim.js.map +0 -1
  240. package/dist/component/public/enterprise/secrets.d.ts +0 -78
  241. package/dist/component/public/enterprise/secrets.d.ts.map +0 -1
  242. package/dist/component/public/enterprise/secrets.js +0 -118
  243. package/dist/component/public/enterprise/secrets.js.map +0 -1
  244. package/dist/component/public/enterprise/webhooks.d.ts +0 -211
  245. package/dist/component/public/enterprise/webhooks.d.ts.map +0 -1
  246. package/dist/component/public/enterprise/webhooks.js.map +0 -1
  247. package/dist/component/public/factors/devices.d.ts +0 -157
  248. package/dist/component/public/factors/devices.d.ts.map +0 -1
  249. package/dist/component/public/factors/devices.js.map +0 -1
  250. package/dist/component/public/factors/passkeys.d.ts +0 -175
  251. package/dist/component/public/factors/passkeys.d.ts.map +0 -1
  252. package/dist/component/public/factors/passkeys.js.map +0 -1
  253. package/dist/component/public/factors/totp.d.ts +0 -189
  254. package/dist/component/public/factors/totp.d.ts.map +0 -1
  255. package/dist/component/public/factors/totp.js.map +0 -1
  256. package/dist/component/public/groups/core.d.ts +0 -137
  257. package/dist/component/public/groups/core.d.ts.map +0 -1
  258. package/dist/component/public/groups/core.js.map +0 -1
  259. package/dist/component/public/groups/invites.d.ts +0 -217
  260. package/dist/component/public/groups/invites.d.ts.map +0 -1
  261. package/dist/component/public/groups/invites.js.map +0 -1
  262. package/dist/component/public/groups/members.d.ts +0 -204
  263. package/dist/component/public/groups/members.d.ts.map +0 -1
  264. package/dist/component/public/groups/members.js.map +0 -1
  265. package/dist/component/public/identity/accounts.d.ts +0 -147
  266. package/dist/component/public/identity/accounts.d.ts.map +0 -1
  267. package/dist/component/public/identity/accounts.js.map +0 -1
  268. package/dist/component/public/identity/codes.d.ts +0 -104
  269. package/dist/component/public/identity/codes.d.ts.map +0 -1
  270. package/dist/component/public/identity/codes.js.map +0 -1
  271. package/dist/component/public/identity/sessions.d.ts +0 -128
  272. package/dist/component/public/identity/sessions.d.ts.map +0 -1
  273. package/dist/component/public/identity/sessions.js.map +0 -1
  274. package/dist/component/public/identity/tokens.d.ts +0 -169
  275. package/dist/component/public/identity/tokens.d.ts.map +0 -1
  276. package/dist/component/public/identity/tokens.js.map +0 -1
  277. package/dist/component/public/identity/users.d.ts +0 -212
  278. package/dist/component/public/identity/users.d.ts.map +0 -1
  279. package/dist/component/public/identity/users.js.map +0 -1
  280. package/dist/component/public/identity/verifiers.d.ts +0 -116
  281. package/dist/component/public/identity/verifiers.d.ts.map +0 -1
  282. package/dist/component/public/identity/verifiers.js.map +0 -1
  283. package/dist/component/public/security/keys.d.ts +0 -209
  284. package/dist/component/public/security/keys.d.ts.map +0 -1
  285. package/dist/component/public/security/keys.js.map +0 -1
  286. package/dist/component/public/security/limits.d.ts +0 -114
  287. package/dist/component/public/security/limits.d.ts.map +0 -1
  288. package/dist/component/public/security/limits.js.map +0 -1
  289. package/dist/component/public.d.ts +0 -28
  290. package/dist/component/public.d.ts.map +0 -1
  291. package/dist/component/schema.d.ts.map +0 -1
  292. package/dist/component/schema.js.map +0 -1
  293. package/dist/component/server/auth.d.ts +0 -447
  294. package/dist/component/server/auth.d.ts.map +0 -1
  295. package/dist/component/server/auth.js +0 -254
  296. package/dist/component/server/auth.js.map +0 -1
  297. package/dist/component/server/config.js +0 -121
  298. package/dist/component/server/config.js.map +0 -1
  299. package/dist/component/server/context.js +0 -53
  300. package/dist/component/server/context.js.map +0 -1
  301. package/dist/component/server/cookies.js +0 -47
  302. package/dist/component/server/cookies.js.map +0 -1
  303. package/dist/component/server/core.js +0 -576
  304. package/dist/component/server/core.js.map +0 -1
  305. package/dist/component/server/crypto.js +0 -56
  306. package/dist/component/server/crypto.js.map +0 -1
  307. package/dist/component/server/db.js +0 -87
  308. package/dist/component/server/db.js.map +0 -1
  309. package/dist/component/server/device.js +0 -152
  310. package/dist/component/server/device.js.map +0 -1
  311. package/dist/component/server/enterprise/config.js +0 -46
  312. package/dist/component/server/enterprise/config.js.map +0 -1
  313. package/dist/component/server/enterprise/domain.js +0 -974
  314. package/dist/component/server/enterprise/domain.js.map +0 -1
  315. package/dist/component/server/enterprise/http.js +0 -787
  316. package/dist/component/server/enterprise/http.js.map +0 -1
  317. package/dist/component/server/enterprise/oidc.js +0 -248
  318. package/dist/component/server/enterprise/oidc.js.map +0 -1
  319. package/dist/component/server/enterprise/policy.js +0 -85
  320. package/dist/component/server/enterprise/policy.js.map +0 -1
  321. package/dist/component/server/enterprise/saml.js.map +0 -1
  322. package/dist/component/server/enterprise/scim.js.map +0 -1
  323. package/dist/component/server/enterprise/shared.js +0 -51
  324. package/dist/component/server/enterprise/shared.js.map +0 -1
  325. package/dist/component/server/http.d.ts +0 -85
  326. package/dist/component/server/http.d.ts.map +0 -1
  327. package/dist/component/server/http.js +0 -351
  328. package/dist/component/server/http.js.map +0 -1
  329. package/dist/component/server/identity.js +0 -16
  330. package/dist/component/server/identity.js.map +0 -1
  331. package/dist/component/server/keys.js +0 -96
  332. package/dist/component/server/keys.js.map +0 -1
  333. package/dist/component/server/limits.js +0 -52
  334. package/dist/component/server/limits.js.map +0 -1
  335. package/dist/component/server/mutations/account.js +0 -46
  336. package/dist/component/server/mutations/account.js.map +0 -1
  337. package/dist/component/server/mutations/code.js +0 -68
  338. package/dist/component/server/mutations/code.js.map +0 -1
  339. package/dist/component/server/mutations/invalidate.js +0 -32
  340. package/dist/component/server/mutations/invalidate.js.map +0 -1
  341. package/dist/component/server/mutations/oauth.js +0 -116
  342. package/dist/component/server/mutations/oauth.js.map +0 -1
  343. package/dist/component/server/mutations/refresh.js +0 -119
  344. package/dist/component/server/mutations/refresh.js.map +0 -1
  345. package/dist/component/server/mutations/register.js +0 -87
  346. package/dist/component/server/mutations/register.js.map +0 -1
  347. package/dist/component/server/mutations/retrieve.js +0 -61
  348. package/dist/component/server/mutations/retrieve.js.map +0 -1
  349. package/dist/component/server/mutations/signature.js +0 -38
  350. package/dist/component/server/mutations/signature.js.map +0 -1
  351. package/dist/component/server/mutations/signin.js +0 -27
  352. package/dist/component/server/mutations/signin.js.map +0 -1
  353. package/dist/component/server/mutations/signout.js +0 -27
  354. package/dist/component/server/mutations/signout.js.map +0 -1
  355. package/dist/component/server/mutations/store/refs.js +0 -15
  356. package/dist/component/server/mutations/store/refs.js.map +0 -1
  357. package/dist/component/server/mutations/store.js +0 -70
  358. package/dist/component/server/mutations/store.js.map +0 -1
  359. package/dist/component/server/mutations/verifier.js +0 -18
  360. package/dist/component/server/mutations/verifier.js.map +0 -1
  361. package/dist/component/server/mutations/verify.js +0 -98
  362. package/dist/component/server/mutations/verify.js.map +0 -1
  363. package/dist/component/server/oauth.js +0 -242
  364. package/dist/component/server/oauth.js.map +0 -1
  365. package/dist/component/server/passkey.js +0 -415
  366. package/dist/component/server/passkey.js.map +0 -1
  367. package/dist/component/server/redirects.js +0 -40
  368. package/dist/component/server/redirects.js.map +0 -1
  369. package/dist/component/server/refresh.js +0 -99
  370. package/dist/component/server/refresh.js.map +0 -1
  371. package/dist/component/server/runtime.d.ts +0 -136
  372. package/dist/component/server/runtime.d.ts.map +0 -1
  373. package/dist/component/server/runtime.js +0 -456
  374. package/dist/component/server/runtime.js.map +0 -1
  375. package/dist/component/server/sessions.js +0 -71
  376. package/dist/component/server/sessions.js.map +0 -1
  377. package/dist/component/server/signin.js +0 -225
  378. package/dist/component/server/signin.js.map +0 -1
  379. package/dist/component/server/tokens.js +0 -17
  380. package/dist/component/server/tokens.js.map +0 -1
  381. package/dist/component/server/totp.js +0 -208
  382. package/dist/component/server/totp.js.map +0 -1
  383. package/dist/component/server/types.d.ts +0 -949
  384. package/dist/component/server/types.d.ts.map +0 -1
  385. package/dist/component/server/types.js +0 -79
  386. package/dist/component/server/types.js.map +0 -1
  387. package/dist/component/server/users.js +0 -123
  388. package/dist/component/server/users.js.map +0 -1
  389. package/dist/component/server/utils.js +0 -140
  390. package/dist/component/server/utils.js.map +0 -1
  391. package/dist/core/types.d.ts +0 -361
  392. package/dist/core/types.d.ts.map +0 -1
  393. package/dist/factors/device.js +0 -104
  394. package/dist/factors/device.js.map +0 -1
  395. package/dist/factors/passkey.js.map +0 -1
  396. package/dist/factors/totp.js.map +0 -1
  397. package/dist/providers/anonymous.d.ts.map +0 -1
  398. package/dist/providers/anonymous.js.map +0 -1
  399. package/dist/providers/credentials.d.ts.map +0 -1
  400. package/dist/providers/credentials.js.map +0 -1
  401. package/dist/providers/device.d.ts.map +0 -1
  402. package/dist/providers/device.js.map +0 -1
  403. package/dist/providers/email.d.ts.map +0 -1
  404. package/dist/providers/email.js.map +0 -1
  405. package/dist/providers/oauth.d.ts +0 -69
  406. package/dist/providers/oauth.d.ts.map +0 -1
  407. package/dist/providers/oauth.js +0 -43
  408. package/dist/providers/oauth.js.map +0 -1
  409. package/dist/providers/passkey.d.ts.map +0 -1
  410. package/dist/providers/passkey.js.map +0 -1
  411. package/dist/providers/password.d.ts.map +0 -1
  412. package/dist/providers/password.js.map +0 -1
  413. package/dist/providers/phone.d.ts.map +0 -1
  414. package/dist/providers/phone.js.map +0 -1
  415. package/dist/providers/sso.d.ts.map +0 -1
  416. package/dist/providers/sso.js.map +0 -1
  417. package/dist/providers/totp.d.ts.map +0 -1
  418. package/dist/providers/totp.js.map +0 -1
  419. package/dist/runtime/browser.js +0 -68
  420. package/dist/runtime/browser.js.map +0 -1
  421. package/dist/runtime/invite.js.map +0 -1
  422. package/dist/runtime/proxy.js +0 -70
  423. package/dist/runtime/proxy.js.map +0 -1
  424. package/dist/runtime/storage.js +0 -37
  425. package/dist/runtime/storage.js.map +0 -1
  426. package/dist/server/auth.d.ts.map +0 -1
  427. package/dist/server/auth.js.map +0 -1
  428. package/dist/server/config.d.ts +0 -1
  429. package/dist/server/config.js.map +0 -1
  430. package/dist/server/context.d.ts +0 -1
  431. package/dist/server/context.js.map +0 -1
  432. package/dist/server/cookies.d.ts +0 -1
  433. package/dist/server/cookies.js.map +0 -1
  434. package/dist/server/core.d.ts +0 -1315
  435. package/dist/server/core.d.ts.map +0 -1
  436. package/dist/server/core.js.map +0 -1
  437. package/dist/server/crypto.d.ts +0 -8
  438. package/dist/server/crypto.d.ts.map +0 -1
  439. package/dist/server/crypto.js.map +0 -1
  440. package/dist/server/db.d.ts +0 -1
  441. package/dist/server/db.js.map +0 -1
  442. package/dist/server/device.d.ts +0 -1
  443. package/dist/server/device.js.map +0 -1
  444. package/dist/server/enterprise/config.d.ts +0 -1
  445. package/dist/server/enterprise/config.js.map +0 -1
  446. package/dist/server/enterprise/domain.d.ts +0 -401
  447. package/dist/server/enterprise/domain.d.ts.map +0 -1
  448. package/dist/server/enterprise/domain.js +0 -974
  449. package/dist/server/enterprise/domain.js.map +0 -1
  450. package/dist/server/enterprise/http.d.ts +0 -26
  451. package/dist/server/enterprise/http.d.ts.map +0 -1
  452. package/dist/server/enterprise/http.js +0 -787
  453. package/dist/server/enterprise/http.js.map +0 -1
  454. package/dist/server/enterprise/oidc.d.ts +0 -1
  455. package/dist/server/enterprise/oidc.js +0 -248
  456. package/dist/server/enterprise/oidc.js.map +0 -1
  457. package/dist/server/enterprise/policy.d.ts +0 -1
  458. package/dist/server/enterprise/policy.js +0 -85
  459. package/dist/server/enterprise/policy.js.map +0 -1
  460. package/dist/server/enterprise/saml.d.ts +0 -1
  461. package/dist/server/enterprise/saml.js +0 -338
  462. package/dist/server/enterprise/saml.js.map +0 -1
  463. package/dist/server/enterprise/scim.d.ts +0 -1
  464. package/dist/server/enterprise/scim.js +0 -97
  465. package/dist/server/enterprise/scim.js.map +0 -1
  466. package/dist/server/enterprise/shared.d.ts +0 -5
  467. package/dist/server/enterprise/shared.d.ts.map +0 -1
  468. package/dist/server/enterprise/shared.js +0 -51
  469. package/dist/server/enterprise/shared.js.map +0 -1
  470. package/dist/server/enterprise/validators.d.ts +0 -1
  471. package/dist/server/enterprise/validators.js +0 -60
  472. package/dist/server/enterprise/validators.js.map +0 -1
  473. package/dist/server/http.d.ts.map +0 -1
  474. package/dist/server/http.js.map +0 -1
  475. package/dist/server/identity.d.ts +0 -1
  476. package/dist/server/identity.js.map +0 -1
  477. package/dist/server/keys.d.ts +0 -1
  478. package/dist/server/keys.js.map +0 -1
  479. package/dist/server/limits.d.ts +0 -1
  480. package/dist/server/limits.js.map +0 -1
  481. package/dist/server/mounts.d.ts.map +0 -1
  482. package/dist/server/mounts.js.map +0 -1
  483. package/dist/server/mutations/account.d.ts +0 -29
  484. package/dist/server/mutations/account.d.ts.map +0 -1
  485. package/dist/server/mutations/account.js.map +0 -1
  486. package/dist/server/mutations/code.d.ts +0 -30
  487. package/dist/server/mutations/code.d.ts.map +0 -1
  488. package/dist/server/mutations/code.js.map +0 -1
  489. package/dist/server/mutations/index.d.ts +0 -14
  490. package/dist/server/mutations/invalidate.d.ts +0 -20
  491. package/dist/server/mutations/invalidate.d.ts.map +0 -1
  492. package/dist/server/mutations/invalidate.js.map +0 -1
  493. package/dist/server/mutations/oauth.d.ts +0 -30
  494. package/dist/server/mutations/oauth.d.ts.map +0 -1
  495. package/dist/server/mutations/oauth.js.map +0 -1
  496. package/dist/server/mutations/refresh.d.ts +0 -21
  497. package/dist/server/mutations/refresh.d.ts.map +0 -1
  498. package/dist/server/mutations/refresh.js.map +0 -1
  499. package/dist/server/mutations/register.d.ts +0 -38
  500. package/dist/server/mutations/register.d.ts.map +0 -1
  501. package/dist/server/mutations/register.js.map +0 -1
  502. package/dist/server/mutations/retrieve.d.ts +0 -33
  503. package/dist/server/mutations/retrieve.d.ts.map +0 -1
  504. package/dist/server/mutations/retrieve.js.map +0 -1
  505. package/dist/server/mutations/signature.d.ts +0 -21
  506. package/dist/server/mutations/signature.d.ts.map +0 -1
  507. package/dist/server/mutations/signature.js.map +0 -1
  508. package/dist/server/mutations/signin.d.ts +0 -22
  509. package/dist/server/mutations/signin.d.ts.map +0 -1
  510. package/dist/server/mutations/signin.js.map +0 -1
  511. package/dist/server/mutations/signout.d.ts +0 -16
  512. package/dist/server/mutations/signout.d.ts.map +0 -1
  513. package/dist/server/mutations/signout.js.map +0 -1
  514. package/dist/server/mutations/store/refs.d.ts +0 -12
  515. package/dist/server/mutations/store/refs.d.ts.map +0 -1
  516. package/dist/server/mutations/store/refs.js.map +0 -1
  517. package/dist/server/mutations/store.d.ts +0 -306
  518. package/dist/server/mutations/store.d.ts.map +0 -1
  519. package/dist/server/mutations/store.js.map +0 -1
  520. package/dist/server/mutations/verifier.d.ts +0 -13
  521. package/dist/server/mutations/verifier.d.ts.map +0 -1
  522. package/dist/server/mutations/verifier.js.map +0 -1
  523. package/dist/server/mutations/verify.d.ts +0 -26
  524. package/dist/server/mutations/verify.d.ts.map +0 -1
  525. package/dist/server/mutations/verify.js.map +0 -1
  526. package/dist/server/oauth.d.ts +0 -1
  527. package/dist/server/oauth.js +0 -242
  528. package/dist/server/oauth.js.map +0 -1
  529. package/dist/server/passkey.d.ts +0 -27
  530. package/dist/server/passkey.d.ts.map +0 -1
  531. package/dist/server/passkey.js.map +0 -1
  532. package/dist/server/redirects.d.ts +0 -1
  533. package/dist/server/redirects.js.map +0 -1
  534. package/dist/server/refresh.d.ts +0 -1
  535. package/dist/server/refresh.js.map +0 -1
  536. package/dist/server/runtime.d.ts.map +0 -1
  537. package/dist/server/runtime.js.map +0 -1
  538. package/dist/server/sessions.d.ts +0 -1
  539. package/dist/server/sessions.js.map +0 -1
  540. package/dist/server/signin.d.ts +0 -1
  541. package/dist/server/signin.js.map +0 -1
  542. package/dist/server/ssr.d.ts.map +0 -1
  543. package/dist/server/ssr.js +0 -777
  544. package/dist/server/ssr.js.map +0 -1
  545. package/dist/server/templates.d.ts +0 -1
  546. package/dist/server/templates.js.map +0 -1
  547. package/dist/server/tokens.d.ts +0 -1
  548. package/dist/server/tokens.js.map +0 -1
  549. package/dist/server/totp.d.ts +0 -1
  550. package/dist/server/totp.js.map +0 -1
  551. package/dist/server/types.d.ts.map +0 -1
  552. package/dist/server/types.js.map +0 -1
  553. package/dist/server/users.d.ts +0 -1
  554. package/dist/server/users.js.map +0 -1
  555. package/dist/server/utils.d.ts +0 -1
  556. package/dist/server/utils.js +0 -140
  557. package/dist/server/utils.js.map +0 -1
  558. package/src/authorization/index.ts +0 -83
  559. package/src/cli/bin.ts +0 -5
  560. package/src/cli/command.ts +0 -70
  561. package/src/cli/index.ts +0 -1112
  562. package/src/cli/keys.ts +0 -23
  563. package/src/client/core/types.ts +0 -437
  564. package/src/client/factors/device.ts +0 -158
  565. package/src/client/factors/passkey.ts +0 -279
  566. package/src/client/factors/totp.ts +0 -150
  567. package/src/client/index.ts +0 -1124
  568. package/src/client/runtime/browser.ts +0 -112
  569. package/src/client/runtime/invite.ts +0 -63
  570. package/src/client/runtime/proxy.ts +0 -111
  571. package/src/client/runtime/storage.ts +0 -79
  572. package/src/component/_generated/api.ts +0 -96
  573. package/src/component/_generated/component.ts +0 -3774
  574. package/src/component/_generated/dataModel.ts +0 -60
  575. package/src/component/_generated/server.ts +0 -156
  576. package/src/component/convex.config.ts +0 -5
  577. package/src/component/functions.ts +0 -104
  578. package/src/component/index.ts +0 -42
  579. package/src/component/model.ts +0 -449
  580. package/src/component/public/enterprise/audit.ts +0 -125
  581. package/src/component/public/enterprise/core.ts +0 -355
  582. package/src/component/public/enterprise/domains.ts +0 -327
  583. package/src/component/public/enterprise/scim.ts +0 -397
  584. package/src/component/public/enterprise/secrets.ts +0 -133
  585. package/src/component/public/enterprise/webhooks.ts +0 -307
  586. package/src/component/public/factors/devices.ts +0 -224
  587. package/src/component/public/factors/passkeys.ts +0 -243
  588. package/src/component/public/factors/totp.ts +0 -259
  589. package/src/component/public/groups/core.ts +0 -481
  590. package/src/component/public/groups/invites.ts +0 -608
  591. package/src/component/public/groups/members.ts +0 -410
  592. package/src/component/public/identity/accounts.ts +0 -207
  593. package/src/component/public/identity/codes.ts +0 -149
  594. package/src/component/public/identity/sessions.ts +0 -210
  595. package/src/component/public/identity/tokens.ts +0 -251
  596. package/src/component/public/identity/users.ts +0 -355
  597. package/src/component/public/identity/verifiers.ts +0 -158
  598. package/src/component/public/security/keys.ts +0 -366
  599. package/src/component/public/security/limits.ts +0 -174
  600. package/src/component/public.ts +0 -27
  601. package/src/component/schema.ts +0 -505
  602. package/src/providers/anonymous.ts +0 -99
  603. package/src/providers/credentials.ts +0 -102
  604. package/src/providers/device.ts +0 -87
  605. package/src/providers/email.ts +0 -99
  606. package/src/providers/index.ts +0 -31
  607. package/src/providers/oauth.ts +0 -117
  608. package/src/providers/passkey.ts +0 -77
  609. package/src/providers/password.ts +0 -441
  610. package/src/providers/phone.ts +0 -93
  611. package/src/providers/sso.ts +0 -54
  612. package/src/providers/totp.ts +0 -62
  613. package/src/samlify.d.ts +0 -53
  614. package/src/server/auth.ts +0 -949
  615. package/src/server/config.ts +0 -200
  616. package/src/server/context.ts +0 -90
  617. package/src/server/cookies.ts +0 -49
  618. package/src/server/core.ts +0 -2004
  619. package/src/server/crypto.ts +0 -90
  620. package/src/server/db.ts +0 -203
  621. package/src/server/device.ts +0 -254
  622. package/src/server/enterprise/config.ts +0 -51
  623. package/src/server/enterprise/domain.ts +0 -1739
  624. package/src/server/enterprise/http.ts +0 -1331
  625. package/src/server/enterprise/oidc.ts +0 -500
  626. package/src/server/enterprise/policy.ts +0 -128
  627. package/src/server/enterprise/saml.ts +0 -578
  628. package/src/server/enterprise/scim.ts +0 -135
  629. package/src/server/enterprise/shared.ts +0 -134
  630. package/src/server/enterprise/validators.ts +0 -93
  631. package/src/server/http.ts +0 -790
  632. package/src/server/identity.ts +0 -18
  633. package/src/server/index.ts +0 -40
  634. package/src/server/keys.ts +0 -158
  635. package/src/server/limits.ts +0 -107
  636. package/src/server/mounts.ts +0 -924
  637. package/src/server/mutations/account.ts +0 -62
  638. package/src/server/mutations/code.ts +0 -119
  639. package/src/server/mutations/index.ts +0 -13
  640. package/src/server/mutations/invalidate.ts +0 -50
  641. package/src/server/mutations/oauth.ts +0 -243
  642. package/src/server/mutations/refresh.ts +0 -299
  643. package/src/server/mutations/register.ts +0 -155
  644. package/src/server/mutations/retrieve.ts +0 -109
  645. package/src/server/mutations/signature.ts +0 -57
  646. package/src/server/mutations/signin.ts +0 -54
  647. package/src/server/mutations/signout.ts +0 -43
  648. package/src/server/mutations/store/refs.ts +0 -10
  649. package/src/server/mutations/store.ts +0 -123
  650. package/src/server/mutations/verifier.ts +0 -34
  651. package/src/server/mutations/verify.ts +0 -200
  652. package/src/server/oauth.ts +0 -418
  653. package/src/server/passkey.ts +0 -838
  654. package/src/server/redirects.ts +0 -59
  655. package/src/server/refresh.ts +0 -218
  656. package/src/server/runtime.ts +0 -918
  657. package/src/server/sessions.ts +0 -132
  658. package/src/server/signin.ts +0 -445
  659. package/src/server/ssr.ts +0 -1747
  660. package/src/server/templates.ts +0 -82
  661. package/src/server/tokens.ts +0 -35
  662. package/src/server/totp.ts +0 -399
  663. package/src/server/types.ts +0 -1942
  664. package/src/server/users.ts +0 -291
  665. package/src/server/utils.ts +0 -220
  666. /package/dist/{runtime → client/runtime}/invite.js +0 -0
package/src/server/ssr.ts DELETED
@@ -1,1747 +0,0 @@
1
- import { Fx } from "@robelest/fx";
2
- import { ConvexHttpClient } from "convex/browser";
3
- import { makeFunctionReference } from "convex/server";
4
- import { ConvexError } from "convex/values";
5
- import { parse, serialize } from "cookie";
6
- import { jwtDecode } from "jwt-decode";
7
-
8
- import type {
9
- SignInAction,
10
- SignInActionResult,
11
- SignOutAction,
12
- } from "./runtime";
13
- import { isLocalHost } from "./utils";
14
-
15
- const signInActionRef: SignInAction = makeFunctionReference("auth:signIn");
16
- const signOutActionRef: SignOutAction = makeFunctionReference("auth:signOut");
17
-
18
- /** Cookie lifetime configuration for auth tokens. */
19
- export type AuthCookieConfig = {
20
- /** Maximum age in seconds, or `null` for session cookies. */
21
- maxAge: number | null;
22
- };
23
-
24
- /** Raw cookie values extracted from a request. */
25
- export type AuthCookies = {
26
- /** The JWT access token, or `null` when absent. */
27
- token: string | null;
28
- /** The refresh token, or `null` when absent. */
29
- refreshToken: string | null;
30
- /** The OAuth PKCE verifier, or `null` when absent. */
31
- verifier: string | null;
32
- };
33
-
34
- /** A structured cookie ready to be set via any framework's cookie API. */
35
- export type AuthCookie = {
36
- name: string;
37
- value: string;
38
- options: {
39
- path: string;
40
- httpOnly: boolean;
41
- secure: boolean;
42
- sameSite: "lax" | "strict" | "none";
43
- maxAge?: number;
44
- expires?: Date;
45
- };
46
- };
47
-
48
- /**
49
- * Options for the SSR auth helper returned by {@link server}.
50
- */
51
- export type ServerOptions = {
52
- /** Convex deployment API URL (e.g. `https://your-app.convex.cloud`). */
53
- url: string;
54
- /**
55
- * Accepted JWT issuers for `refresh()` and `verify()`.
56
- *
57
- * By default, this is derived from `url`. If `url` ends with
58
- * `.convex.cloud`, the matching `.convex.site` issuer is also accepted.
59
- */
60
- acceptedIssuers?: string[];
61
- /**
62
- * Path the client POSTs auth actions to. Defaults to `"/api/auth"`.
63
- * Must match the `proxyPath` option on the client.
64
- *
65
- * @defaultValue "/api/auth"
66
- */
67
- apiRoute?: string;
68
- /** Cookie `maxAge` in seconds, or `null` for session cookies. */
69
- cookieMaxAge?: number | null;
70
- /** Enable verbose debug logging for token refresh and cookie operations. */
71
- verbose?: boolean;
72
- /**
73
- * Optional namespace for auth cookie names.
74
- *
75
- * Use this to isolate auth cookies between multiple local apps on the same host.
76
- * If omitted, a deterministic deployment-scoped namespace is derived from `url`.
77
- */
78
- cookieNamespace?: string;
79
- /**
80
- * Control whether `refresh()` handles OAuth `?code=` query parameters.
81
- *
82
- * - `true` (default): always exchange the code on GET requests with `text/html` accept.
83
- * - `false`: never exchange — useful when only the client handles codes.
84
- * - A function: called with the `Request` for per-request decisions.
85
- *
86
- * @defaultValue true
87
- */
88
- shouldHandleCode?:
89
- | ((request: Request) => boolean | Promise<boolean>)
90
- | boolean;
91
- };
92
-
93
- /**
94
- * Result returned from `server().refresh()`.
95
- *
96
- * Covers both normal SSR refreshes and OAuth code-exchange redirects.
97
- */
98
- export type RefreshResult =
99
- | {
100
- /** Code exchange occurred — return the pre-built redirect `Response`. */
101
- redirect: true;
102
- /** 302 redirect with Set-Cookie headers already serialized. */
103
- response: Response;
104
- }
105
- | {
106
- /** No redirect — apply cookies and read the token. */
107
- redirect: false;
108
- /** Structured cookies to set on the response. */
109
- cookies: AuthCookie[];
110
- /** JWT for SSR hydration, or `null` if not authenticated. */
111
- token: string | null;
112
- };
113
-
114
- const TOKEN_COOKIE_BASE_NAME = "__convexAuthJWT";
115
- const REFRESH_COOKIE_BASE_NAME = "__convexAuthRefreshToken";
116
- const VERIFIER_COOKIE_BASE_NAME = "__convexAuthOAuthVerifier";
117
- const DERIVED_COOKIE_NAMESPACE_FALLBACK = "convexauth";
118
-
119
- /**
120
- * Derive the cookie names used for auth tokens.
121
- *
122
- * On localhost the names are unprefixed; on production hosts they
123
- * use the `__Host-` prefix for tighter security.
124
- *
125
- * @param host - The `Host` header value. Omit to use unprefixed names.
126
- * @param cookieNamespace - Optional namespace suffix for cookie isolation.
127
- * @returns An object with `token`, `refreshToken`, and `verifier` cookie names.
128
- */
129
- export function authCookieNames(
130
- host?: string,
131
- cookieNamespace?: string | null,
132
- ) {
133
- const prefix = isLocalHost(host) ? "" : "__Host-";
134
- const namespace = normalizeCookieNamespace(cookieNamespace);
135
- const suffix = namespace === null ? "" : `_${namespace}`;
136
- return {
137
- token: `${prefix}${TOKEN_COOKIE_BASE_NAME}${suffix}`,
138
- refreshToken: `${prefix}${REFRESH_COOKIE_BASE_NAME}${suffix}`,
139
- verifier: `${prefix}${VERIFIER_COOKIE_BASE_NAME}${suffix}`,
140
- };
141
- }
142
-
143
- /**
144
- * Parse auth cookie values from a raw `Cookie` header string.
145
- *
146
- * @param cookieHeader - The raw `Cookie` header, or `null`/`undefined`.
147
- * @param host - The `Host` header, used to determine cookie name prefixes.
148
- * @param cookieNamespace - Optional namespace suffix for cookie isolation.
149
- * @returns Parsed {@link AuthCookies} with `token`, `refreshToken`, and `verifier`.
150
- */
151
- export function parseAuthCookies(
152
- cookieHeader: string | null | undefined,
153
- host?: string,
154
- cookieNamespace?: string | null,
155
- ): AuthCookies {
156
- const names = authCookieNames(host, cookieNamespace);
157
- const parsed = parse(cookieHeader ?? "");
158
- return {
159
- token: parsed[names.token] ?? null,
160
- refreshToken: parsed[names.refreshToken] ?? null,
161
- verifier: parsed[names.verifier] ?? null,
162
- };
163
- }
164
-
165
- /**
166
- * Serialize auth cookies into `Set-Cookie` header strings.
167
- *
168
- * Nulled-out values produce deletion cookies (maxAge 0, expired date).
169
- *
170
- * @param cookies - The auth cookie values to serialize.
171
- * @param host - The `Host` header, used for cookie name prefixes and `Secure` flag.
172
- * @param config - Cookie lifetime config. Defaults to session cookies.
173
- * @param cookieNamespace - Optional namespace suffix for cookie isolation.
174
- * @returns An array of three `Set-Cookie` header strings.
175
- */
176
- export function serializeAuthCookies(
177
- cookies: AuthCookies,
178
- host?: string,
179
- config: AuthCookieConfig = { maxAge: null },
180
- cookieNamespace?: string | null,
181
- ) {
182
- const names = authCookieNames(host, cookieNamespace);
183
- const secure = !isLocalHost(host);
184
- const base = {
185
- path: "/",
186
- httpOnly: true,
187
- sameSite: "lax" as const,
188
- secure,
189
- };
190
- const maxAge = config.maxAge ?? undefined;
191
- const serialized = [
192
- serialize(names.token, cookies.token ?? "", {
193
- ...base,
194
- maxAge: cookies.token === null ? 0 : maxAge,
195
- expires: cookies.token === null ? new Date(0) : undefined,
196
- }),
197
- serialize(names.refreshToken, cookies.refreshToken ?? "", {
198
- ...base,
199
- maxAge: cookies.refreshToken === null ? 0 : maxAge,
200
- expires: cookies.refreshToken === null ? new Date(0) : undefined,
201
- }),
202
- serialize(names.verifier, cookies.verifier ?? "", {
203
- ...base,
204
- maxAge: cookies.verifier === null ? 0 : maxAge,
205
- expires: cookies.verifier === null ? new Date(0) : undefined,
206
- }),
207
- ];
208
- return serialized;
209
- }
210
-
211
- /**
212
- * Build structured cookie objects for any SSR framework.
213
- *
214
- * Use with SvelteKit's `event.cookies.set()`, TanStack Start's `setCookie()`,
215
- * Next.js's `cookies().set()`, or any other framework cookie API.
216
- *
217
- * @param cookies - The auth cookie values to convert.
218
- * @param host - The `Host` header, used for cookie name prefixes and `Secure`.
219
- * @param config - Cookie lifetime config. Defaults to session cookies.
220
- * @param cookieNamespace - Optional namespace suffix for cookie isolation.
221
- * @returns Structured cookie descriptors ready for framework cookie APIs.
222
- */
223
- export function structuredAuthCookies(
224
- cookies: AuthCookies,
225
- host?: string,
226
- config: AuthCookieConfig = { maxAge: null },
227
- cookieNamespace?: string | null,
228
- ): AuthCookie[] {
229
- const names = authCookieNames(host, cookieNamespace);
230
- const secure = !isLocalHost(host);
231
- const base = {
232
- path: "/" as const,
233
- httpOnly: true as const,
234
- secure,
235
- sameSite: "lax" as const,
236
- };
237
- const maxAge = config.maxAge ?? undefined;
238
- const structured: AuthCookie[] = [
239
- {
240
- name: names.token,
241
- value: cookies.token ?? "",
242
- options: {
243
- ...base,
244
- maxAge: cookies.token === null ? 0 : maxAge,
245
- expires: cookies.token === null ? new Date(0) : undefined,
246
- },
247
- },
248
- {
249
- name: names.refreshToken,
250
- value: cookies.refreshToken ?? "",
251
- options: {
252
- ...base,
253
- maxAge: cookies.refreshToken === null ? 0 : maxAge,
254
- expires: cookies.refreshToken === null ? new Date(0) : undefined,
255
- },
256
- },
257
- {
258
- name: names.verifier,
259
- value: cookies.verifier ?? "",
260
- options: {
261
- ...base,
262
- maxAge: cookies.verifier === null ? 0 : maxAge,
263
- expires: cookies.verifier === null ? new Date(0) : undefined,
264
- },
265
- },
266
- ];
267
-
268
- return structured;
269
- }
270
-
271
- /**
272
- * Check whether a request pathname matches the auth proxy route.
273
- *
274
- * Handles trailing-slash ambiguity: both `/api/auth` and `/api/auth/`
275
- * match regardless of how `apiRoute` is configured.
276
- *
277
- * @param pathname - The request URL pathname.
278
- * @param apiRoute - The configured proxy route (e.g. `"/api/auth"`).
279
- * @returns `true` when the pathname matches the proxy route.
280
- *
281
- * @see {@link server}
282
- */
283
- export function shouldProxyAuthAction(pathname: string, apiRoute: string) {
284
- if (apiRoute.endsWith("/")) {
285
- return pathname === apiRoute || pathname === apiRoute.slice(0, -1);
286
- }
287
- return pathname === apiRoute || pathname === `${apiRoute}/`;
288
- }
289
-
290
- const REQUIRED_TOKEN_LIFETIME_MS = 60_000;
291
- const MINIMUM_REQUIRED_TOKEN_LIFETIME_MS = 10_000;
292
-
293
- type DecodedToken = { exp?: number; iat?: number; iss?: string };
294
-
295
- function normalizeCookieNamespace(cookieNamespace?: string | null) {
296
- if (cookieNamespace === undefined || cookieNamespace === null) {
297
- return null;
298
- }
299
- const normalized = cookieNamespace
300
- .trim()
301
- .replace(/[^a-zA-Z0-9]+/g, "_")
302
- .replace(/^_+|_+$/g, "")
303
- .toLowerCase();
304
- return normalized.length > 0 ? normalized : null;
305
- }
306
-
307
- /**
308
- * Safely check if a string is a valid URL without throwing.
309
- */
310
- function canParseUrl(value: string): boolean {
311
- try {
312
- new URL(value);
313
- return true;
314
- } catch {
315
- return false;
316
- }
317
- }
318
-
319
- function serializeAuthCookie(cookie: AuthCookie): string {
320
- const parts = [
321
- `${cookie.name}=${cookie.value}`,
322
- `Path=${cookie.options.path}`,
323
- ];
324
- if (cookie.options.httpOnly) parts.push("HttpOnly");
325
- if (cookie.options.secure) parts.push("Secure");
326
- if (cookie.options.sameSite)
327
- parts.push(`SameSite=${cookie.options.sameSite}`);
328
- if (cookie.options.maxAge !== undefined)
329
- parts.push(`Max-Age=${cookie.options.maxAge}`);
330
- if (cookie.options.expires)
331
- parts.push(`Expires=${cookie.options.expires.toUTCString()}`);
332
- return parts.join("; ");
333
- }
334
-
335
- function buildRedirectResponse(
336
- location: string,
337
- cookies: AuthCookie[],
338
- ): Response {
339
- const headers = new Headers({ Location: location });
340
- for (const cookie of cookies) {
341
- headers.append("Set-Cookie", serializeAuthCookie(cookie));
342
- }
343
- return new Response(null, { status: 302, headers });
344
- }
345
-
346
- function deriveCookieNamespaceFromUrl(url: string) {
347
- if (!canParseUrl(url)) return DERIVED_COOKIE_NAMESPACE_FALLBACK;
348
- const parsed = new URL(url);
349
- const raw = `${parsed.hostname}${parsed.pathname}`;
350
- return normalizeCookieNamespace(raw) ?? DERIVED_COOKIE_NAMESPACE_FALLBACK;
351
- }
352
-
353
- function normalizeIssuer(value: string) {
354
- if (!canParseUrl(value)) return value.replace(/\/+$/, "");
355
- const parsed = new URL(value);
356
- const pathname =
357
- parsed.pathname === "/" ? "" : parsed.pathname.replace(/\/+$/, "");
358
- return `${parsed.protocol}//${parsed.host}${pathname}`;
359
- }
360
-
361
- function convexSiteIssuerFromCloudUrl(value: string) {
362
- if (!canParseUrl(value)) return null;
363
- const parsed = new URL(value);
364
- if (!parsed.hostname.endsWith(".convex.cloud")) {
365
- return null;
366
- }
367
- parsed.hostname =
368
- parsed.hostname.slice(0, -".convex.cloud".length) + ".convex.site";
369
- return normalizeIssuer(parsed.toString());
370
- }
371
-
372
- function defaultAcceptedIssuersForUrl(value: string) {
373
- const issuers = [normalizeIssuer(value)];
374
- const siteIssuer = convexSiteIssuerFromCloudUrl(value);
375
- if (siteIssuer !== null) {
376
- issuers.push(siteIssuer);
377
- }
378
- return issuers;
379
- }
380
-
381
- /**
382
- * Create an SSR auth helper for server-side frameworks.
383
- *
384
- * Handles cookie-based token management, OAuth code exchange,
385
- * and automatic JWT refresh on page loads. Works with any
386
- * framework that gives you a `Request` object — SvelteKit,
387
- * TanStack Start, Remix, Next.js, etc.
388
- *
389
- * @param options - SSR configuration (Convex API URL, issuer rules, proxy route, cookie lifetime).
390
- * @returns An object with `token`, `verify`, `proxy`, and `refresh` methods.
391
- *
392
- * @example SvelteKit hooks
393
- * ```ts
394
- * // src/hooks.server.ts
395
- * import { server } from '@robelest/convex-auth/server';
396
- *
397
- * const auth = server({ url: CONVEX_URL });
398
- *
399
- * export const handle = async ({ event, resolve }) => {
400
- * const { cookies, token } = await auth.refresh(event.request);
401
- * for (const c of cookies) event.cookies.set(c.name, c.value, c.options);
402
- * event.locals.token = token;
403
- * return resolve(event);
404
- * };
405
- * ```
406
- *
407
- * @example Generic proxy endpoint
408
- * ```ts
409
- * if (shouldProxyAuthAction(url.pathname, '/api/auth')) {
410
- * return auth.proxy(request);
411
- * }
412
- * ```
413
- *
414
- * @param options - Server-side auth configuration including Convex URL,
415
- * accepted issuers, proxy route, and cookie behavior.
416
- * @returns SSR helpers for reading tokens, refreshing cookies, and proxying
417
- * auth actions through an httpOnly-cookie layer.
418
- *
419
- * @see {@link shouldProxyAuthAction}
420
- */
421
- export function server(options: ServerOptions) {
422
- const convexUrl = options.url;
423
- const apiRoute = options.apiRoute ?? "/api/auth";
424
- const cookieConfig = { maxAge: options.cookieMaxAge ?? null };
425
- const verbose = options.verbose ?? false;
426
- const cookieNamespace =
427
- normalizeCookieNamespace(options.cookieNamespace) ??
428
- deriveCookieNamespaceFromUrl(convexUrl);
429
- const acceptedIssuers = new Set(
430
- (options.acceptedIssuers ?? defaultAcceptedIssuersForUrl(convexUrl))
431
- .map(normalizeIssuer)
432
- .filter((issuer) => issuer.length > 0),
433
- );
434
-
435
- return {
436
- /**
437
- * Read the JWT from the request cookies without any validation.
438
- *
439
- * @param request - The incoming HTTP request.
440
- * @returns The raw JWT string, or `null` when no token cookie exists.
441
- */
442
- token(request: Request): string | null {
443
- return parseAuthCookies(
444
- request.headers.get("cookie"),
445
- request.headers.get("host") ?? new URL(request.url).host,
446
- cookieNamespace,
447
- ).token;
448
- },
449
-
450
- /**
451
- * Check whether the request carries a non-expired JWT.
452
- *
453
- * Performs local expiration checking only (no network call).
454
- * Use for lightweight auth guards in middleware.
455
- *
456
- * @param request - The incoming HTTP request.
457
- * @returns `true` when a valid, non-expired JWT exists in the cookies.
458
- */
459
- async verify(request: Request): Promise<boolean> {
460
- const token = parseAuthCookies(
461
- request.headers.get("cookie"),
462
- request.headers.get("host") ?? new URL(request.url).host,
463
- cookieNamespace,
464
- ).token;
465
- if (token === null) {
466
- return false;
467
- }
468
- const decodedToken = await Fx.run(
469
- Fx.attempt(
470
- async () => jwtDecode<DecodedToken>(token),
471
- (decoded) => decoded,
472
- () => null,
473
- ),
474
- );
475
- if (decodedToken?.exp === undefined || decodedToken.iss === undefined) {
476
- return false;
477
- }
478
- if (!acceptedIssuers.has(normalizeIssuer(decodedToken.iss))) {
479
- return false;
480
- }
481
- return decodedToken.exp * 1000 > Date.now();
482
- },
483
-
484
- /**
485
- * Handle a proxied `signIn` or `signOut` POST from the client.
486
- *
487
- * Validates the route, method, and origin, then forwards the
488
- * action to Convex and returns a `Response` with updated
489
- * `Set-Cookie` headers. The client never sees the real
490
- * refresh token — it stays in httpOnly cookies.
491
- *
492
- * @param request - The incoming POST request from the client.
493
- * @returns A JSON `Response` with auth result and cookie headers.
494
- */
495
- async proxy(request: Request): Promise<Response> {
496
- const requestUrl = new URL(request.url);
497
- const requestDispatch = !shouldProxyAuthAction(
498
- requestUrl.pathname,
499
- apiRoute,
500
- )
501
- ? { kind: "invalidRoute" as const }
502
- : request.method !== "POST"
503
- ? { kind: "invalidMethod" as const }
504
- : (() => {
505
- const originHeader = request.headers.get("origin");
506
- if (originHeader === null) {
507
- return false;
508
- }
509
- const forwardedProtoHeader =
510
- request.headers.get("x-forwarded-proto");
511
- const protocol =
512
- forwardedProtoHeader !== null
513
- ? (() => {
514
- const forwardedProto = forwardedProtoHeader
515
- .split(",")[0]
516
- ?.trim();
517
- if (
518
- forwardedProto !== undefined &&
519
- forwardedProto.length > 0
520
- ) {
521
- return forwardedProto.endsWith(":")
522
- ? forwardedProto
523
- : `${forwardedProto}:`;
524
- }
525
- return new URL(request.url).protocol;
526
- })()
527
- : new URL(request.url).protocol;
528
- const requestHost =
529
- request.headers.get("host") ?? new URL(request.url).host;
530
- const hostCandidate = `${protocol}//${requestHost}`;
531
- const host = canParseUrl(hostCandidate)
532
- ? new URL(hostCandidate).host
533
- : requestHost;
534
- if (!canParseUrl(originHeader)) {
535
- return true;
536
- }
537
- const originUrl = new URL(originHeader);
538
- return (
539
- originUrl.host !== host || originUrl.protocol !== protocol
540
- );
541
- })()
542
- ? { kind: "invalidOrigin" as const }
543
- : { kind: "valid" as const };
544
-
545
- const validationErrorResponse = await Fx.run(
546
- Fx.match(requestDispatch, requestDispatch.kind, {
547
- invalidRoute: () => new Response("Invalid route", { status: 404 }),
548
- invalidMethod: () => new Response("Invalid method", { status: 405 }),
549
- invalidOrigin: () => new Response("Invalid origin", { status: 403 }),
550
- valid: () => null,
551
- }),
552
- );
553
- if (validationErrorResponse !== null) {
554
- return validationErrorResponse;
555
- }
556
-
557
- const body = await Fx.run(
558
- Fx.attempt(
559
- async () => {
560
- const parsed = await request.json();
561
- if (typeof parsed !== "object" || parsed === null) {
562
- return null;
563
- }
564
- return parsed as Record<string, unknown>;
565
- },
566
- (parsed) => parsed,
567
- () => null,
568
- ),
569
- );
570
- if (body === null) {
571
- return new Response("Invalid request body", { status: 400 });
572
- }
573
-
574
- const action = body.action as string;
575
- const args =
576
- typeof body.args === "object" && body.args !== null
577
- ? (body.args as Record<string, any>)
578
- : {};
579
-
580
- const actionDispatch =
581
- action === "auth:signIn"
582
- ? { action: "sessionStart" as const }
583
- : action === "auth:signOut"
584
- ? { action: "sessionStop" as const }
585
- : null;
586
-
587
- if (actionDispatch === null) {
588
- return new Response("Invalid action", { status: 400 });
589
- }
590
-
591
- const host = request.headers.get("host") ?? new URL(request.url).host;
592
- const currentCookies = parseAuthCookies(
593
- request.headers.get("cookie"),
594
- host,
595
- cookieNamespace,
596
- );
597
-
598
- return Fx.run(
599
- Fx.match(actionDispatch, actionDispatch.action, {
600
- sessionStart: (_) =>
601
- Fx.promise(async () => {
602
- const refreshDispatch =
603
- args.refreshToken === undefined
604
- ? { kind: "passthrough" as const }
605
- : currentCookies.refreshToken === null
606
- ? { kind: "refreshRequestedWithoutCookie" as const }
607
- : {
608
- kind: "hydrateRefreshFromCookie" as const,
609
- refreshToken: currentCookies.refreshToken,
610
- };
611
-
612
- const refreshResponse = await Fx.run(
613
- Fx.match(refreshDispatch, refreshDispatch.kind, {
614
- passthrough: async () => null,
615
- hydrateRefreshFromCookie: async ({ refreshToken }) => {
616
- args.refreshToken = refreshToken;
617
- return null;
618
- },
619
- refreshRequestedWithoutCookie: async () => {
620
- const currentToken = currentCookies.token;
621
- const decodedToken =
622
- currentToken === null
623
- ? null
624
- : await Fx.run(
625
- Fx.attempt(
626
- async () => jwtDecode<DecodedToken>(currentToken),
627
- (decoded) => decoded,
628
- () => null,
629
- ),
630
- );
631
- const tokenDispatch =
632
- currentToken !== null &&
633
- decodedToken?.exp !== undefined &&
634
- decodedToken.iss !== undefined &&
635
- acceptedIssuers.has(normalizeIssuer(decodedToken.iss)) &&
636
- decodedToken.exp * 1000 > Date.now()
637
- ? {
638
- kind: "validToken" as const,
639
- token: currentToken,
640
- }
641
- : { kind: "missingToken" as const };
642
- return await Fx.run(
643
- Fx.match(tokenDispatch, tokenDispatch.kind, {
644
- validToken: ({ token }) =>
645
- new Response(
646
- JSON.stringify({
647
- tokens: {
648
- token,
649
- refreshToken: "dummy",
650
- },
651
- }),
652
- {
653
- status: 200,
654
- headers: {
655
- "Content-Type": "application/json",
656
- },
657
- },
658
- ),
659
- missingToken: () =>
660
- new Response(JSON.stringify({ tokens: null }), {
661
- status: 200,
662
- headers: {
663
- "Content-Type": "application/json",
664
- },
665
- }),
666
- }),
667
- );
668
- },
669
- }),
670
- );
671
- const refreshDecision =
672
- refreshResponse !== null
673
- ? {
674
- kind: "shortCircuit" as const,
675
- response: refreshResponse,
676
- }
677
- : { kind: "continue" as const };
678
- const maybeShortCircuitResponse = await Fx.run(
679
- Fx.match(refreshDecision, refreshDecision.kind, {
680
- shortCircuit: ({ response }) => response,
681
- continue: () => null,
682
- }),
683
- );
684
- if (maybeShortCircuitResponse !== null) {
685
- return maybeShortCircuitResponse;
686
- }
687
-
688
- const client = new ConvexHttpClient(convexUrl);
689
- const authDispatch =
690
- args.refreshToken === undefined &&
691
- args.params?.code === undefined &&
692
- currentCookies.token !== null
693
- ? {
694
- kind: "attachAuth" as const,
695
- token: currentCookies.token,
696
- }
697
- : { kind: "skipAuth" as const };
698
- await Fx.run(
699
- Fx.match(authDispatch, authDispatch.kind, {
700
- attachAuth: ({ token }) => {
701
- client.setAuth(token);
702
- },
703
- skipAuth: () => undefined,
704
- }),
705
- );
706
- return Fx.run(
707
- Fx.from({
708
- ok: () => client.action(signInActionRef, args),
709
- err: (error) => error,
710
- }).pipe(
711
- Fx.fold({
712
- ok: (result: SignInActionResult) =>
713
- Fx.run(
714
- Fx.match(result, result.kind, {
715
- redirect: (redirectResult) => {
716
- const response = new Response(
717
- JSON.stringify({
718
- kind: "redirect",
719
- redirect: redirectResult.redirect,
720
- verifier: redirectResult.verifier,
721
- }),
722
- {
723
- status: 200,
724
- headers: {
725
- "Content-Type": "application/json",
726
- },
727
- },
728
- );
729
- for (const value of serializeAuthCookies(
730
- {
731
- ...currentCookies,
732
- verifier: redirectResult.verifier,
733
- },
734
- host,
735
- cookieConfig,
736
- cookieNamespace,
737
- )) {
738
- response.headers.append("Set-Cookie", value);
739
- }
740
- return Fx.succeed(response);
741
- },
742
- signedIn: (signedInResult) => {
743
- const response = new Response(
744
- JSON.stringify({
745
- kind: "signedIn",
746
- tokens:
747
- signedInResult.tokens === null
748
- ? null
749
- : {
750
- token: signedInResult.tokens.token,
751
- refreshToken: "dummy",
752
- },
753
- }),
754
- {
755
- status: 200,
756
- headers: {
757
- "Content-Type": "application/json",
758
- },
759
- },
760
- );
761
- for (const value of serializeAuthCookies(
762
- {
763
- token: signedInResult.tokens?.token ?? null,
764
- refreshToken:
765
- signedInResult.tokens?.refreshToken ?? null,
766
- verifier: null,
767
- },
768
- host,
769
- cookieConfig,
770
- cookieNamespace,
771
- )) {
772
- response.headers.append("Set-Cookie", value);
773
- }
774
- return Fx.succeed(response);
775
- },
776
- started: (startedResult) =>
777
- Fx.succeed(
778
- new Response(JSON.stringify(startedResult), {
779
- status: 200,
780
- headers: {
781
- "Content-Type": "application/json",
782
- },
783
- }),
784
- ),
785
- passkeyOptions: (passkeyOptionsResult) =>
786
- Fx.succeed(
787
- new Response(
788
- JSON.stringify(passkeyOptionsResult),
789
- {
790
- status: 200,
791
- headers: {
792
- "Content-Type": "application/json",
793
- },
794
- },
795
- ),
796
- ),
797
- totpRequired: (totpRequiredResult) =>
798
- Fx.succeed(
799
- new Response(JSON.stringify(totpRequiredResult), {
800
- status: 200,
801
- headers: {
802
- "Content-Type": "application/json",
803
- },
804
- }),
805
- ),
806
- totpSetup: (totpSetupResult) =>
807
- Fx.succeed(
808
- new Response(JSON.stringify(totpSetupResult), {
809
- status: 200,
810
- headers: {
811
- "Content-Type": "application/json",
812
- },
813
- }),
814
- ),
815
- deviceCode: (deviceCodeResult) =>
816
- Fx.succeed(
817
- new Response(JSON.stringify(deviceCodeResult), {
818
- status: 200,
819
- headers: {
820
- "Content-Type": "application/json",
821
- },
822
- }),
823
- ),
824
- }),
825
- ),
826
- err: (error: unknown) => {
827
- const errorBody =
828
- error instanceof ConvexError &&
829
- typeof error.data === "object" &&
830
- error.data !== null &&
831
- "code" in error.data
832
- ? {
833
- error:
834
- (error.data as { message?: string }).message ??
835
- String(error),
836
- authError: error.data,
837
- }
838
- : {
839
- error:
840
- error instanceof Error
841
- ? error.message
842
- : String(error),
843
- };
844
- const response = new Response(JSON.stringify(errorBody), {
845
- status: 400,
846
- headers: {
847
- "Content-Type": "application/json",
848
- },
849
- });
850
- const clearSession =
851
- args.refreshToken !== undefined &&
852
- error instanceof ConvexError &&
853
- typeof error.data === "object" &&
854
- error.data !== null &&
855
- (error.data as Record<string, unknown>).code ===
856
- "INVALID_REFRESH_TOKEN";
857
- for (const value of serializeAuthCookies(
858
- {
859
- token: clearSession ? null : currentCookies.token,
860
- refreshToken: clearSession
861
- ? null
862
- : currentCookies.refreshToken,
863
- verifier: null,
864
- },
865
- host,
866
- cookieConfig,
867
- cookieNamespace,
868
- )) {
869
- response.headers.append("Set-Cookie", value);
870
- }
871
- return response;
872
- },
873
- }),
874
- ),
875
- );
876
- }),
877
- sessionStop: (_) =>
878
- Fx.promise(async () => {
879
- await Fx.run(
880
- Fx.from({
881
- ok: () =>
882
- (() => {
883
- const client = new ConvexHttpClient(convexUrl);
884
- if (currentCookies.token !== null) {
885
- client.setAuth(currentCookies.token);
886
- }
887
- return client.action(signOutActionRef);
888
- })(),
889
- err: (error) => error,
890
- }).pipe(
891
- Fx.recover((error: unknown) => {
892
- console.error(
893
- "[convex-auth/server] proxy sign-out failed",
894
- error,
895
- );
896
- const fallbackDispatch =
897
- currentCookies.refreshToken !== null
898
- ? {
899
- kind: "attemptFallback" as const,
900
- refreshToken: currentCookies.refreshToken,
901
- }
902
- : { kind: "skipFallback" as const };
903
- return Fx.match(fallbackDispatch, fallbackDispatch.kind, {
904
- attemptFallback: ({ refreshToken }) =>
905
- Fx.from({
906
- ok: async () => {
907
- const refreshClient = new ConvexHttpClient(
908
- convexUrl,
909
- );
910
- const refreshed = (await refreshClient.action(
911
- signInActionRef,
912
- {
913
- refreshToken,
914
- },
915
- )) as SignInActionResult;
916
- const refreshedTokens = await Fx.run(
917
- Fx.match(refreshed, refreshed.kind, {
918
- signedIn: (signedInResult) =>
919
- Fx.succeed(signedInResult.tokens),
920
- redirect: () =>
921
- Fx.fatal(
922
- new Error(
923
- "Invalid `auth:signIn` result for sign-out fallback refresh",
924
- ),
925
- ),
926
- started: () =>
927
- Fx.fatal(
928
- new Error(
929
- "Invalid `auth:signIn` result for sign-out fallback refresh",
930
- ),
931
- ),
932
- passkeyOptions: () =>
933
- Fx.fatal(
934
- new Error(
935
- "Invalid `auth:signIn` result for sign-out fallback refresh",
936
- ),
937
- ),
938
- totpRequired: () =>
939
- Fx.fatal(
940
- new Error(
941
- "Invalid `auth:signIn` result for sign-out fallback refresh",
942
- ),
943
- ),
944
- totpSetup: () =>
945
- Fx.fatal(
946
- new Error(
947
- "Invalid `auth:signIn` result for sign-out fallback refresh",
948
- ),
949
- ),
950
- deviceCode: () =>
951
- Fx.fatal(
952
- new Error(
953
- "Invalid `auth:signIn` result for sign-out fallback refresh",
954
- ),
955
- ),
956
- }),
957
- );
958
- const fallbackSignOutDispatch =
959
- refreshedTokens !== null
960
- ? {
961
- kind: "signOutWithRefreshed" as const,
962
- token: refreshedTokens.token,
963
- }
964
- : { kind: "skipRefreshedSignOut" as const };
965
- await Fx.run(
966
- Fx.match(
967
- fallbackSignOutDispatch,
968
- fallbackSignOutDispatch.kind,
969
- {
970
- signOutWithRefreshed: ({ token }) =>
971
- Fx.promise(async () => {
972
- const client = new ConvexHttpClient(
973
- convexUrl,
974
- );
975
- client.setAuth(token);
976
- await client.action(signOutActionRef);
977
- }),
978
- skipRefreshedSignOut: () =>
979
- Fx.succeed(undefined),
980
- },
981
- ),
982
- );
983
- },
984
- err: (error) => error,
985
- }).pipe(
986
- Fx.recover((fallbackError: unknown) => {
987
- console.error(
988
- "[convex-auth/server] proxy sign-out fallback failed",
989
- fallbackError,
990
- );
991
- return Fx.succeed(undefined);
992
- }),
993
- ),
994
- skipFallback: () => Fx.succeed(undefined),
995
- });
996
- }),
997
- Fx.map(() => undefined),
998
- ),
999
- );
1000
- const response = new Response(JSON.stringify(null), {
1001
- status: 200,
1002
- headers: {
1003
- "Content-Type": "application/json",
1004
- },
1005
- });
1006
- for (const value of serializeAuthCookies(
1007
- {
1008
- token: null,
1009
- refreshToken: null,
1010
- verifier: null,
1011
- },
1012
- host,
1013
- cookieConfig,
1014
- cookieNamespace,
1015
- )) {
1016
- response.headers.append("Set-Cookie", value);
1017
- }
1018
- return response;
1019
- }),
1020
- }),
1021
- );
1022
- },
1023
-
1024
- /**
1025
- * Refresh auth tokens on page load.
1026
- *
1027
- * Call this in your server hooks/middleware on every request.
1028
- * It handles three scenarios:
1029
- *
1030
- * 1. **OAuth code exchange** — exchanges a `?code=` query param for tokens and returns a redirect URL.
1031
- * 2. **Token refresh** — refreshes the JWT if it's close to expiry.
1032
- * 3. **No-op** — returns the existing token when no refresh is needed.
1033
- *
1034
- * @param request - The incoming HTTP request.
1035
- * @returns Structured cookies to set on the response, an optional redirect URL, and the current JWT.
1036
- */
1037
- async refresh(request: Request): Promise<RefreshResult> {
1038
- const host = request.headers.get("host") ?? new URL(request.url).host;
1039
- const currentCookies = parseAuthCookies(
1040
- request.headers.get("cookie"),
1041
- host,
1042
- cookieNamespace,
1043
- );
1044
- const currentToken = currentCookies.token;
1045
-
1046
- // CORS request — do not mutate auth cookies from cross-origin requests.
1047
- const originHeader = request.headers.get("origin");
1048
- const forwardedProtoHeader = request.headers.get("x-forwarded-proto");
1049
- const protocol =
1050
- forwardedProtoHeader !== null
1051
- ? (() => {
1052
- const forwardedProto = forwardedProtoHeader.split(",")[0]?.trim();
1053
- if (forwardedProto !== undefined && forwardedProto.length > 0) {
1054
- return forwardedProto.endsWith(":")
1055
- ? forwardedProto
1056
- : `${forwardedProto}:`;
1057
- }
1058
- return new URL(request.url).protocol;
1059
- })()
1060
- : new URL(request.url).protocol;
1061
- const requestHost =
1062
- request.headers.get("host") ?? new URL(request.url).host;
1063
- const hostCandidate = `${protocol}//${requestHost}`;
1064
- const normalizedHost = canParseUrl(hostCandidate)
1065
- ? new URL(hostCandidate).host
1066
- : requestHost;
1067
- const originUrl =
1068
- originHeader !== null && canParseUrl(originHeader)
1069
- ? new URL(originHeader)
1070
- : null;
1071
- const corsRequest =
1072
- originHeader !== null &&
1073
- (originUrl === null ||
1074
- originUrl.host !== normalizedHost ||
1075
- originUrl.protocol !== protocol);
1076
- const corsDispatch = corsRequest
1077
- ? { kind: "crossOrigin" as const }
1078
- : { kind: "sameOrigin" as const };
1079
- const corsRefreshResult = await Fx.run(
1080
- Fx.match(corsDispatch, corsDispatch.kind, {
1081
- crossOrigin: () =>
1082
- ({
1083
- redirect: false,
1084
- cookies: [],
1085
- token: null,
1086
- }) satisfies RefreshResult,
1087
- sameOrigin: () => null,
1088
- }),
1089
- );
1090
- if (corsRefreshResult !== null) {
1091
- return corsRefreshResult;
1092
- }
1093
-
1094
- // OAuth code exchange — exchange code for tokens and redirect.
1095
- const requestUrl = new URL(request.url);
1096
- const code = requestUrl.searchParams.get("code");
1097
- const shouldHandleCode =
1098
- options.shouldHandleCode === undefined
1099
- ? true
1100
- : typeof options.shouldHandleCode === "function"
1101
- ? await options.shouldHandleCode(request)
1102
- : options.shouldHandleCode;
1103
-
1104
- const codeExchangeDispatch =
1105
- code !== null &&
1106
- request.method === "GET" &&
1107
- request.headers.get("accept")?.includes("text/html") &&
1108
- shouldHandleCode
1109
- ? { kind: "exchange" as const, code }
1110
- : { kind: "skip" as const };
1111
- const codeExchangeResult = await Fx.run(
1112
- Fx.match(codeExchangeDispatch, codeExchangeDispatch.kind, {
1113
- exchange: async ({
1114
- code: verificationCode,
1115
- }): Promise<RefreshResult> => {
1116
- const redirectUrl = new URL(requestUrl.toString());
1117
- return Fx.run(
1118
- Fx.from({
1119
- ok: async () => {
1120
- const client = new ConvexHttpClient(convexUrl);
1121
- const result = (await client.action(signInActionRef, {
1122
- params: { code: verificationCode },
1123
- verifier: currentCookies.verifier ?? undefined,
1124
- })) as SignInActionResult;
1125
- const tokens = await Fx.run(
1126
- Fx.match(result, result.kind, {
1127
- signedIn: (signedInResult) =>
1128
- Fx.succeed(signedInResult.tokens),
1129
- redirect: () =>
1130
- Fx.fatal(
1131
- new Error(
1132
- "Invalid `auth:signIn` result for code exchange",
1133
- ),
1134
- ),
1135
- started: () =>
1136
- Fx.fatal(
1137
- new Error(
1138
- "Invalid `auth:signIn` result for code exchange",
1139
- ),
1140
- ),
1141
- passkeyOptions: () =>
1142
- Fx.fatal(
1143
- new Error(
1144
- "Invalid `auth:signIn` result for code exchange",
1145
- ),
1146
- ),
1147
- totpRequired: () =>
1148
- Fx.fatal(
1149
- new Error(
1150
- "Invalid `auth:signIn` result for code exchange",
1151
- ),
1152
- ),
1153
- totpSetup: () =>
1154
- Fx.fatal(
1155
- new Error(
1156
- "Invalid `auth:signIn` result for code exchange",
1157
- ),
1158
- ),
1159
- deviceCode: () =>
1160
- Fx.fatal(
1161
- new Error(
1162
- "Invalid `auth:signIn` result for code exchange",
1163
- ),
1164
- ),
1165
- }),
1166
- );
1167
- return { kind: "signedIn" as const, tokens };
1168
- },
1169
- err: (error) => error,
1170
- }).pipe(
1171
- Fx.fold({
1172
- ok: (result): RefreshResult => {
1173
- redirectUrl.searchParams.delete("code");
1174
- const cookies = structuredAuthCookies(
1175
- {
1176
- token: result.tokens?.token ?? null,
1177
- refreshToken: result.tokens?.refreshToken ?? null,
1178
- verifier: null,
1179
- },
1180
- host,
1181
- cookieConfig,
1182
- cookieNamespace,
1183
- );
1184
- return {
1185
- redirect: true,
1186
- response: buildRedirectResponse(
1187
- redirectUrl.toString(),
1188
- cookies,
1189
- ),
1190
- };
1191
- },
1192
- err: (error: unknown): RefreshResult => {
1193
- console.error(
1194
- "[convex-auth/server] code exchange failed",
1195
- error,
1196
- );
1197
- const errorCode =
1198
- error instanceof ConvexError &&
1199
- typeof error.data === "object" &&
1200
- error.data !== null &&
1201
- typeof (error.data as Record<string, unknown>).code ===
1202
- "string"
1203
- ? ((error.data as Record<string, unknown>)
1204
- .code as string)
1205
- : null;
1206
- const terminalCodeExchangeError =
1207
- errorCode === "OAUTH_INVALID_STATE" ||
1208
- errorCode === "OAUTH_PROVIDER_ERROR" ||
1209
- errorCode === "OAUTH_MISSING_ID_TOKEN" ||
1210
- errorCode === "OAUTH_INVALID_PROFILE" ||
1211
- errorCode === "OAUTH_MISSING_VERIFIER" ||
1212
- errorCode === "INVALID_VERIFIER" ||
1213
- errorCode === "INVALID_VERIFICATION_CODE";
1214
- if (!terminalCodeExchangeError) {
1215
- return {
1216
- redirect: false,
1217
- cookies: [],
1218
- token: currentCookies.token,
1219
- };
1220
- }
1221
- redirectUrl.searchParams.delete("code");
1222
- const cookies = structuredAuthCookies(
1223
- {
1224
- token: currentCookies.token,
1225
- refreshToken: currentCookies.refreshToken,
1226
- verifier: null,
1227
- },
1228
- host,
1229
- cookieConfig,
1230
- cookieNamespace,
1231
- );
1232
- return {
1233
- redirect: true,
1234
- response: buildRedirectResponse(
1235
- redirectUrl.toString(),
1236
- cookies,
1237
- ),
1238
- };
1239
- },
1240
- }),
1241
- ),
1242
- );
1243
- },
1244
- skip: async () => null,
1245
- }),
1246
- );
1247
- const codeExchangeDecision =
1248
- codeExchangeResult !== null
1249
- ? { kind: "done" as const, result: codeExchangeResult }
1250
- : { kind: "continue" as const };
1251
- const maybeCodeExchangeResult = await Fx.run(
1252
- Fx.match(codeExchangeDecision, codeExchangeDecision.kind, {
1253
- done: ({ result }) => result,
1254
- continue: () => null,
1255
- }),
1256
- );
1257
- if (maybeCodeExchangeResult !== null) {
1258
- return maybeCodeExchangeResult;
1259
- }
1260
-
1261
- // Normal page load — refresh tokens if needed.
1262
- const tokens = await Fx.run(
1263
- Fx.gen(function* () {
1264
- const { token, refreshToken } = currentCookies;
1265
-
1266
- const isMalformedRefreshToken =
1267
- refreshToken !== null &&
1268
- (refreshToken.trim().length === 0 || refreshToken === "dummy");
1269
- const malformedRefreshTokenDispatch = isMalformedRefreshToken
1270
- ? { kind: "malformed" as const }
1271
- : { kind: "ok" as const };
1272
- const malformedRefreshTokenResult = yield* Fx.match(
1273
- malformedRefreshTokenDispatch,
1274
- malformedRefreshTokenDispatch.kind,
1275
- {
1276
- malformed: () => {
1277
- if (verbose) {
1278
- console.debug(
1279
- `${new Date().toISOString()} [convex-auth/server] Refresh token cookie malformed, clearing auth cookies`,
1280
- );
1281
- }
1282
- return null;
1283
- },
1284
- ok: () => undefined,
1285
- },
1286
- );
1287
- if (malformedRefreshTokenResult !== undefined) {
1288
- return malformedRefreshTokenResult;
1289
- }
1290
-
1291
- const decodedToken =
1292
- token === null
1293
- ? null
1294
- : yield* Fx.attempt(
1295
- async () => jwtDecode<DecodedToken>(token),
1296
- (decoded) => decoded,
1297
- () => null,
1298
- );
1299
- const issuerDispatch =
1300
- decodedToken?.iss !== undefined &&
1301
- !acceptedIssuers.has(normalizeIssuer(decodedToken.iss))
1302
- ? { kind: "issuerMismatch" as const }
1303
- : { kind: "issuerOk" as const };
1304
- const issuerResult = yield* Fx.match(
1305
- issuerDispatch,
1306
- issuerDispatch.kind,
1307
- {
1308
- issuerMismatch: () => {
1309
- if (verbose) {
1310
- console.debug(
1311
- `${new Date().toISOString()} [convex-auth/server] Access token issuer mismatch, clearing auth cookies`,
1312
- );
1313
- }
1314
- return null;
1315
- },
1316
- issuerOk: () => undefined,
1317
- },
1318
- );
1319
- if (issuerResult !== undefined) {
1320
- return issuerResult;
1321
- }
1322
-
1323
- const tokenState =
1324
- token === null
1325
- ? refreshToken === null
1326
- ? { kind: "none" as const }
1327
- : { kind: "refreshOnly" as const, refreshToken }
1328
- : refreshToken === null
1329
- ? { kind: "accessOnly" as const, token }
1330
- : { kind: "both" as const, token, refreshToken };
1331
-
1332
- return yield* Fx.match(tokenState, tokenState.kind, {
1333
- none: () => {
1334
- if (verbose) {
1335
- console.debug(
1336
- `${new Date().toISOString()} [convex-auth/server] No auth cookies found, skipping refresh`,
1337
- );
1338
- }
1339
- return Fx.succeed(undefined);
1340
- },
1341
- refreshOnly: ({ refreshToken: refreshTokenValue }) => {
1342
- if (verbose) {
1343
- console.debug(
1344
- `${new Date().toISOString()} [convex-auth/server] Access token cookie missing, attempting refresh-token recovery`,
1345
- );
1346
- }
1347
- return Fx.from({
1348
- ok: async () => {
1349
- const client = new ConvexHttpClient(convexUrl);
1350
- const result = (await client.action(signInActionRef, {
1351
- refreshToken: refreshTokenValue,
1352
- })) as SignInActionResult;
1353
- const tokens = await Fx.run(
1354
- Fx.match(result, result.kind, {
1355
- signedIn: (signedInResult) =>
1356
- Fx.succeed(signedInResult.tokens),
1357
- redirect: () =>
1358
- Fx.fatal(
1359
- new Error(
1360
- "Invalid `auth:signIn` result for token refresh",
1361
- ),
1362
- ),
1363
- started: () =>
1364
- Fx.fatal(
1365
- new Error(
1366
- "Invalid `auth:signIn` result for token refresh",
1367
- ),
1368
- ),
1369
- passkeyOptions: () =>
1370
- Fx.fatal(
1371
- new Error(
1372
- "Invalid `auth:signIn` result for token refresh",
1373
- ),
1374
- ),
1375
- totpRequired: () =>
1376
- Fx.fatal(
1377
- new Error(
1378
- "Invalid `auth:signIn` result for token refresh",
1379
- ),
1380
- ),
1381
- totpSetup: () =>
1382
- Fx.fatal(
1383
- new Error(
1384
- "Invalid `auth:signIn` result for token refresh",
1385
- ),
1386
- ),
1387
- deviceCode: () =>
1388
- Fx.fatal(
1389
- new Error(
1390
- "Invalid `auth:signIn` result for token refresh",
1391
- ),
1392
- ),
1393
- }),
1394
- );
1395
- if (verbose) {
1396
- console.debug(
1397
- `${new Date().toISOString()} [convex-auth/server] Refreshed tokens, null=${tokens === null}`,
1398
- );
1399
- }
1400
- return tokens;
1401
- },
1402
- err: (error) => error,
1403
- }).pipe(
1404
- Fx.recover((error: unknown) => {
1405
- console.error(
1406
- "[convex-auth/server] refresh-token exchange failed",
1407
- error,
1408
- );
1409
- const errorCode =
1410
- error instanceof ConvexError &&
1411
- typeof error.data === "object" &&
1412
- error.data !== null &&
1413
- typeof (error.data as Record<string, unknown>).code ===
1414
- "string"
1415
- ? ((error.data as Record<string, unknown>).code as string)
1416
- : null;
1417
- if (errorCode === "INVALID_REFRESH_TOKEN") {
1418
- if (verbose) {
1419
- console.debug(
1420
- `${new Date().toISOString()} [convex-auth/server] Refresh token rejected, clearing auth cookies`,
1421
- );
1422
- }
1423
- return Fx.succeed(
1424
- null as
1425
- | { token: string; refreshToken: string }
1426
- | null
1427
- | undefined,
1428
- );
1429
- }
1430
- if (verbose) {
1431
- console.debug(
1432
- `${new Date().toISOString()} [convex-auth/server] Token refresh failed transiently, keeping current cookies`,
1433
- );
1434
- }
1435
- return Fx.succeed(
1436
- undefined as
1437
- | { token: string; refreshToken: string }
1438
- | null
1439
- | undefined,
1440
- );
1441
- }),
1442
- );
1443
- },
1444
- accessOnly: () => {
1445
- const accessOnlyDispatch =
1446
- decodedToken?.exp !== undefined &&
1447
- decodedToken.iss !== undefined &&
1448
- acceptedIssuers.has(normalizeIssuer(decodedToken.iss)) &&
1449
- decodedToken.exp * 1000 > Date.now()
1450
- ? { kind: "accessValid" as const }
1451
- : { kind: "accessInvalid" as const };
1452
- return Fx.match(accessOnlyDispatch, accessOnlyDispatch.kind, {
1453
- accessValid: () => {
1454
- if (verbose) {
1455
- console.debug(
1456
- `${new Date().toISOString()} [convex-auth/server] Refresh token cookie missing but access token still valid`,
1457
- );
1458
- }
1459
- return Fx.succeed(undefined);
1460
- },
1461
- accessInvalid: () => {
1462
- if (verbose) {
1463
- console.debug(
1464
- `${new Date().toISOString()} [convex-auth/server] Refresh token cookie missing and access token invalid, clearing`,
1465
- );
1466
- }
1467
- return Fx.succeed(null);
1468
- },
1469
- });
1470
- },
1471
- both: ({ refreshToken: refreshTokenValue }) => {
1472
- const bothDecodeDispatch:
1473
- | { kind: "undecodable" }
1474
- | {
1475
- kind: "decoded";
1476
- decodedToken: DecodedToken & {
1477
- exp: number;
1478
- iat: number;
1479
- };
1480
- } =
1481
- decodedToken?.exp === undefined ||
1482
- decodedToken.iat === undefined
1483
- ? { kind: "undecodable" as const }
1484
- : {
1485
- kind: "decoded" as const,
1486
- decodedToken: decodedToken as DecodedToken & {
1487
- exp: number;
1488
- iat: number;
1489
- },
1490
- };
1491
- return Fx.match(bothDecodeDispatch, bothDecodeDispatch.kind, {
1492
- undecodable: () => {
1493
- if (verbose) {
1494
- console.debug(
1495
- `${new Date().toISOString()} [convex-auth/server] Failed to decode access token, attempting refresh-token recovery`,
1496
- );
1497
- }
1498
- return Fx.from({
1499
- ok: async () => {
1500
- const client = new ConvexHttpClient(convexUrl);
1501
- const result = (await client.action(signInActionRef, {
1502
- refreshToken: refreshTokenValue,
1503
- })) as SignInActionResult;
1504
- const tokens = await Fx.run(
1505
- Fx.match(result, result.kind, {
1506
- signedIn: (signedInResult) =>
1507
- Fx.succeed(signedInResult.tokens),
1508
- redirect: () =>
1509
- Fx.fatal(
1510
- new Error(
1511
- "Invalid `auth:signIn` result for token refresh",
1512
- ),
1513
- ),
1514
- started: () =>
1515
- Fx.fatal(
1516
- new Error(
1517
- "Invalid `auth:signIn` result for token refresh",
1518
- ),
1519
- ),
1520
- passkeyOptions: () =>
1521
- Fx.fatal(
1522
- new Error(
1523
- "Invalid `auth:signIn` result for token refresh",
1524
- ),
1525
- ),
1526
- totpRequired: () =>
1527
- Fx.fatal(
1528
- new Error(
1529
- "Invalid `auth:signIn` result for token refresh",
1530
- ),
1531
- ),
1532
- totpSetup: () =>
1533
- Fx.fatal(
1534
- new Error(
1535
- "Invalid `auth:signIn` result for token refresh",
1536
- ),
1537
- ),
1538
- deviceCode: () =>
1539
- Fx.fatal(
1540
- new Error(
1541
- "Invalid `auth:signIn` result for token refresh",
1542
- ),
1543
- ),
1544
- }),
1545
- );
1546
- if (verbose) {
1547
- console.debug(
1548
- `${new Date().toISOString()} [convex-auth/server] Refreshed tokens, null=${tokens === null}`,
1549
- );
1550
- }
1551
- return tokens;
1552
- },
1553
- err: (error) => error,
1554
- }).pipe(
1555
- Fx.recover((error: unknown) => {
1556
- console.error(
1557
- "[convex-auth/server] refresh-token exchange failed",
1558
- error,
1559
- );
1560
- const errorCode =
1561
- error instanceof ConvexError &&
1562
- typeof error.data === "object" &&
1563
- error.data !== null &&
1564
- typeof (error.data as Record<string, unknown>).code ===
1565
- "string"
1566
- ? ((error.data as Record<string, unknown>)
1567
- .code as string)
1568
- : null;
1569
- if (errorCode === "INVALID_REFRESH_TOKEN") {
1570
- if (verbose) {
1571
- console.debug(
1572
- `${new Date().toISOString()} [convex-auth/server] Refresh token rejected, clearing auth cookies`,
1573
- );
1574
- }
1575
- return Fx.succeed(
1576
- null as
1577
- | { token: string; refreshToken: string }
1578
- | null
1579
- | undefined,
1580
- );
1581
- }
1582
- if (verbose) {
1583
- console.debug(
1584
- `${new Date().toISOString()} [convex-auth/server] Token refresh failed transiently, keeping current cookies`,
1585
- );
1586
- }
1587
- return Fx.succeed(
1588
- undefined as
1589
- | { token: string; refreshToken: string }
1590
- | null
1591
- | undefined,
1592
- );
1593
- }),
1594
- );
1595
- },
1596
- decoded: ({ decodedToken: decodedAccessToken }) => {
1597
- const totalTokenLifetimeMs =
1598
- decodedAccessToken.exp * 1000 -
1599
- decodedAccessToken.iat * 1000;
1600
- const minimumExpiration =
1601
- Date.now() +
1602
- Math.min(
1603
- REQUIRED_TOKEN_LIFETIME_MS,
1604
- Math.max(
1605
- MINIMUM_REQUIRED_TOKEN_LIFETIME_MS,
1606
- totalTokenLifetimeMs / 10,
1607
- ),
1608
- );
1609
- const expirationDispatch =
1610
- decodedAccessToken.exp * 1000 > minimumExpiration
1611
- ? { kind: "skipRefresh" as const }
1612
- : { kind: "refresh" as const };
1613
- return Fx.match(expirationDispatch, expirationDispatch.kind, {
1614
- skipRefresh: () => {
1615
- if (verbose) {
1616
- console.debug(
1617
- `${new Date().toISOString()} [convex-auth/server] Token valid long enough, skipping refresh`,
1618
- );
1619
- }
1620
- return Fx.succeed(undefined);
1621
- },
1622
- refresh: () =>
1623
- Fx.from({
1624
- ok: async () => {
1625
- const client = new ConvexHttpClient(convexUrl);
1626
- const result = (await client.action(signInActionRef, {
1627
- refreshToken: refreshTokenValue,
1628
- })) as SignInActionResult;
1629
- const tokens = await Fx.run(
1630
- Fx.match(result, result.kind, {
1631
- signedIn: (signedInResult) =>
1632
- Fx.succeed(signedInResult.tokens),
1633
- redirect: () =>
1634
- Fx.fatal(
1635
- new Error(
1636
- "Invalid `auth:signIn` result for token refresh",
1637
- ),
1638
- ),
1639
- started: () =>
1640
- Fx.fatal(
1641
- new Error(
1642
- "Invalid `auth:signIn` result for token refresh",
1643
- ),
1644
- ),
1645
- passkeyOptions: () =>
1646
- Fx.fatal(
1647
- new Error(
1648
- "Invalid `auth:signIn` result for token refresh",
1649
- ),
1650
- ),
1651
- totpRequired: () =>
1652
- Fx.fatal(
1653
- new Error(
1654
- "Invalid `auth:signIn` result for token refresh",
1655
- ),
1656
- ),
1657
- totpSetup: () =>
1658
- Fx.fatal(
1659
- new Error(
1660
- "Invalid `auth:signIn` result for token refresh",
1661
- ),
1662
- ),
1663
- deviceCode: () =>
1664
- Fx.fatal(
1665
- new Error(
1666
- "Invalid `auth:signIn` result for token refresh",
1667
- ),
1668
- ),
1669
- }),
1670
- );
1671
- if (verbose) {
1672
- console.debug(
1673
- `${new Date().toISOString()} [convex-auth/server] Refreshed tokens, null=${tokens === null}`,
1674
- );
1675
- }
1676
- return tokens;
1677
- },
1678
- err: (error) => error,
1679
- }).pipe(
1680
- Fx.recover((error: unknown) => {
1681
- console.error(
1682
- "[convex-auth/server] refresh-token exchange failed",
1683
- error,
1684
- );
1685
- const errorCode =
1686
- error instanceof ConvexError &&
1687
- typeof error.data === "object" &&
1688
- error.data !== null &&
1689
- typeof (error.data as Record<string, unknown>)
1690
- .code === "string"
1691
- ? ((error.data as Record<string, unknown>)
1692
- .code as string)
1693
- : null;
1694
- if (errorCode === "INVALID_REFRESH_TOKEN") {
1695
- if (verbose) {
1696
- console.debug(
1697
- `${new Date().toISOString()} [convex-auth/server] Refresh token rejected, clearing auth cookies`,
1698
- );
1699
- }
1700
- return Fx.succeed(
1701
- null as
1702
- | { token: string; refreshToken: string }
1703
- | null
1704
- | undefined,
1705
- );
1706
- }
1707
- if (verbose) {
1708
- console.debug(
1709
- `${new Date().toISOString()} [convex-auth/server] Token refresh failed transiently, keeping current cookies`,
1710
- );
1711
- }
1712
- return Fx.succeed(
1713
- undefined as
1714
- | { token: string; refreshToken: string }
1715
- | null
1716
- | undefined,
1717
- );
1718
- }),
1719
- ),
1720
- });
1721
- },
1722
- });
1723
- },
1724
- });
1725
- }),
1726
- );
1727
- if (tokens === undefined) {
1728
- return { redirect: false, cookies: [], token: currentToken };
1729
- }
1730
-
1731
- return {
1732
- redirect: false,
1733
- cookies: structuredAuthCookies(
1734
- {
1735
- token: tokens?.token ?? null,
1736
- refreshToken: tokens?.refreshToken ?? null,
1737
- verifier: null,
1738
- },
1739
- host,
1740
- cookieConfig,
1741
- cookieNamespace,
1742
- ),
1743
- token: tokens?.token ?? null,
1744
- };
1745
- },
1746
- };
1747
- }