@kayjs/auth 0.0.0-stage → 1.104.2

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 (407) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -2
  3. package/llms/admin-and-organizations.md +70 -0
  4. package/llms/admin-pages.md +30 -0
  5. package/llms/grouped-methods.md +106 -0
  6. package/llms/hardening-and-testing.md +108 -0
  7. package/llms/oauth-passkeys-two-factor.md +111 -0
  8. package/llms/oauth-providers.md +28 -0
  9. package/llms/oauth-server-and-tokens.md +76 -0
  10. package/llms/organization-pages.md +20 -0
  11. package/llms/passwords-and-mails.md +129 -0
  12. package/llms/sessions-and-guards.md +51 -0
  13. package/llms/setup.md +122 -0
  14. package/llms/signed-in-components.md +59 -0
  15. package/llms.txt +28 -0
  16. package/package.json +85 -4
  17. package/src/access.ts +31 -0
  18. package/src/account.ts +198 -0
  19. package/src/add.ts +70 -0
  20. package/src/address.ts +51 -0
  21. package/src/admin.ts +361 -0
  22. package/src/auditWording.ts +153 -0
  23. package/src/auth.ts +219 -0
  24. package/src/authorize/clients.ts +160 -0
  25. package/src/authorize/grants.ts +44 -0
  26. package/src/authorize/metadata.ts +72 -0
  27. package/src/authorize/register.ts +38 -0
  28. package/src/authorize/reply.ts +9 -0
  29. package/src/authorize/request.ts +253 -0
  30. package/src/authorize/resource.ts +34 -0
  31. package/src/authorize/revoke.ts +36 -0
  32. package/src/authorize/routes.ts +87 -0
  33. package/src/authorize/token.ts +213 -0
  34. package/src/authorize/uris.ts +76 -0
  35. package/src/bearer.ts +208 -0
  36. package/src/bearerHeader.ts +11 -0
  37. package/src/breached.ts +47 -0
  38. package/src/browser.ts +59 -0
  39. package/src/captcha.ts +196 -0
  40. package/src/cbor.ts +129 -0
  41. package/src/components/deletion-banner.kay +20 -0
  42. package/src/components/impersonation-banner.kay +21 -0
  43. package/src/components/signed-in.kay +8 -0
  44. package/src/components/signed-out.kay +6 -0
  45. package/src/components/user-menu.kay +21 -0
  46. package/src/context.ts +5 -0
  47. package/src/core.ts +308 -0
  48. package/src/createUserCommand.ts +217 -0
  49. package/src/decide.ts +45 -0
  50. package/src/deletion.ts +80 -0
  51. package/src/devTool.ts +104 -0
  52. package/src/device.ts +37 -0
  53. package/src/email.ts +594 -0
  54. package/src/erase.ts +237 -0
  55. package/src/events.ts +297 -0
  56. package/src/export.ts +101 -0
  57. package/src/failure.ts +63 -0
  58. package/src/forwardAuth.ts +55 -0
  59. package/src/forwardRoutes.ts +68 -0
  60. package/src/gate.ts +104 -0
  61. package/src/guard.ts +22 -0
  62. package/src/guards.ts +72 -0
  63. package/src/guesses.ts +99 -0
  64. package/src/index.ts +46 -0
  65. package/src/keys.ts +51 -0
  66. package/src/limits.ts +182 -0
  67. package/src/load.ts +18 -0
  68. package/src/locale.ts +15 -0
  69. package/src/log.ts +4 -0
  70. package/src/mail.ts +225 -0
  71. package/src/memoryStore.ts +599 -0
  72. package/src/messages/fr.ts +357 -0
  73. package/src/methods/account.ts +80 -0
  74. package/src/methods/admin.ts +63 -0
  75. package/src/methods/audit.ts +15 -0
  76. package/src/methods/email.ts +39 -0
  77. package/src/methods/magicLink.ts +32 -0
  78. package/src/methods/oauth.ts +35 -0
  79. package/src/methods/oauthServer.ts +31 -0
  80. package/src/methods/organizations.ts +96 -0
  81. package/src/methods/passkeys.ts +61 -0
  82. package/src/methods/password.ts +53 -0
  83. package/src/methods/sessions.ts +18 -0
  84. package/src/methods/tokens.ts +24 -0
  85. package/src/methods/twoFactor.ts +62 -0
  86. package/src/oauth/client.ts +324 -0
  87. package/src/oauth/flow.ts +350 -0
  88. package/src/oauth/index.ts +5 -0
  89. package/src/oauth/pkce.ts +20 -0
  90. package/src/oauth/providers.ts +490 -0
  91. package/src/oauth/refresher.ts +177 -0
  92. package/src/oauth/timeout.ts +2 -0
  93. package/src/oauth/types.ts +78 -0
  94. package/src/oauthTest.ts +112 -0
  95. package/src/organizations.ts +675 -0
  96. package/src/organizationsCore.ts +60 -0
  97. package/src/own.ts +313 -0
  98. package/src/pages/components/captcha.kay +16 -0
  99. package/src/pages/components/frame.kay +11 -0
  100. package/src/pages/components/message.kay +8 -0
  101. package/src/pages/components/panel.kay +12 -0
  102. package/src/pages/components/passkey-add.kay +48 -0
  103. package/src/pages/components/passkey-sign-in.kay +34 -0
  104. package/src/pages/components/passkey-two-factor.kay +33 -0
  105. package/src/pages/components/proof.kay +15 -0
  106. package/src/pages/components/provider-buttons.kay +25 -0
  107. package/src/pages/components/recovery-codes.kay +13 -0
  108. package/src/pages/components/separator.kay +4 -0
  109. package/src/pages/english.ts +82 -0
  110. package/src/pages/forms.ts +185 -0
  111. package/src/pages/index.ts +361 -0
  112. package/src/pages/organizationPage.ts +12 -0
  113. package/src/pages/routes/account/_guard.ts +15 -0
  114. package/src/pages/routes/account/_layout.kay +37 -0
  115. package/src/pages/routes/account/clients.kay +33 -0
  116. package/src/pages/routes/account/connections.kay +57 -0
  117. package/src/pages/routes/account/data.kay +106 -0
  118. package/src/pages/routes/account/index.kay +109 -0
  119. package/src/pages/routes/account/organizations/[org].kay +188 -0
  120. package/src/pages/routes/account/organizations/_guard.ts +8 -0
  121. package/src/pages/routes/account/organizations/index.kay +86 -0
  122. package/src/pages/routes/account/security.kay +164 -0
  123. package/src/pages/routes/account/sessions.kay +51 -0
  124. package/src/pages/routes/account/tokens.kay +84 -0
  125. package/src/pages/routes/admin/_layout.kay +43 -0
  126. package/src/pages/routes/admin/audit.kay +43 -0
  127. package/src/pages/routes/admin/index.kay +60 -0
  128. package/src/pages/routes/admin/users/[id].kay +181 -0
  129. package/src/pages/routes/confirm-email.kay +39 -0
  130. package/src/pages/routes/invitation.kay +104 -0
  131. package/src/pages/routes/magic-link.kay +42 -0
  132. package/src/pages/routes/oauth/authorize.kay +80 -0
  133. package/src/pages/routes/register.kay +83 -0
  134. package/src/pages/routes/reset-password.kay +73 -0
  135. package/src/pages/routes/sign-in.kay +128 -0
  136. package/src/pages/routes/two-factor.kay +61 -0
  137. package/src/pages/routes/verify-email.kay +83 -0
  138. package/src/passkeys.ts +373 -0
  139. package/src/password.ts +316 -0
  140. package/src/payloads.ts +22 -0
  141. package/src/pg.ts +705 -0
  142. package/src/profile.ts +43 -0
  143. package/src/proof.ts +110 -0
  144. package/src/prune.ts +123 -0
  145. package/src/rates.ts +82 -0
  146. package/src/request.ts +712 -0
  147. package/src/requiredTwoFactor.ts +71 -0
  148. package/src/requirement.ts +23 -0
  149. package/src/roles.ts +18 -0
  150. package/src/routes.ts +202 -0
  151. package/src/rules-check.ts +37 -0
  152. package/src/rules.ts +108 -0
  153. package/src/securityNotice.ts +45 -0
  154. package/src/sessions.ts +184 -0
  155. package/src/setup.ts +417 -0
  156. package/src/signInOptions.ts +9 -0
  157. package/src/state.ts +34 -0
  158. package/src/store.ts +410 -0
  159. package/src/storeHelpers.ts +23 -0
  160. package/src/test.ts +46 -0
  161. package/src/testCookies.ts +9 -0
  162. package/src/testHelpers.ts +58 -0
  163. package/src/testing.ts +113 -0
  164. package/src/text.ts +6 -0
  165. package/src/totp.ts +120 -0
  166. package/src/twoFactor.ts +200 -0
  167. package/src/types.ts +23 -0
  168. package/src/userData.ts +47 -0
  169. package/src/viewer-layout/_layout.kay +11 -0
  170. package/src/viewer.ts +116 -0
  171. package/src/wayIn.ts +40 -0
  172. package/src/webauthn.ts +375 -0
  173. package/src/wsAuth.ts +53 -0
  174. package/types/access.d.ts +12 -0
  175. package/types/access.d.ts.map +1 -0
  176. package/types/account.d.ts +32 -0
  177. package/types/account.d.ts.map +1 -0
  178. package/types/add.d.ts +29 -0
  179. package/types/add.d.ts.map +1 -0
  180. package/types/address.d.ts +2 -0
  181. package/types/address.d.ts.map +1 -0
  182. package/types/admin.d.ts +84 -0
  183. package/types/admin.d.ts.map +1 -0
  184. package/types/auditWording.d.ts +9 -0
  185. package/types/auditWording.d.ts.map +1 -0
  186. package/types/auth.d.ts +3475 -0
  187. package/types/auth.d.ts.map +1 -0
  188. package/types/authorize/clients.d.ts +15 -0
  189. package/types/authorize/clients.d.ts.map +1 -0
  190. package/types/authorize/grants.d.ts +15 -0
  191. package/types/authorize/grants.d.ts.map +1 -0
  192. package/types/authorize/metadata.d.ts +20 -0
  193. package/types/authorize/metadata.d.ts.map +1 -0
  194. package/types/authorize/register.d.ts +4 -0
  195. package/types/authorize/register.d.ts.map +1 -0
  196. package/types/authorize/reply.d.ts +6 -0
  197. package/types/authorize/reply.d.ts.map +1 -0
  198. package/types/authorize/request.d.ts +48 -0
  199. package/types/authorize/request.d.ts.map +1 -0
  200. package/types/authorize/resource.d.ts +4 -0
  201. package/types/authorize/resource.d.ts.map +1 -0
  202. package/types/authorize/revoke.d.ts +4 -0
  203. package/types/authorize/revoke.d.ts.map +1 -0
  204. package/types/authorize/routes.d.ts +5 -0
  205. package/types/authorize/routes.d.ts.map +1 -0
  206. package/types/authorize/token.d.ts +7 -0
  207. package/types/authorize/token.d.ts.map +1 -0
  208. package/types/authorize/uris.d.ts +5 -0
  209. package/types/authorize/uris.d.ts.map +1 -0
  210. package/types/bearer.d.ts +45 -0
  211. package/types/bearer.d.ts.map +1 -0
  212. package/types/bearerHeader.d.ts +3 -0
  213. package/types/bearerHeader.d.ts.map +1 -0
  214. package/types/breached.d.ts +5 -0
  215. package/types/breached.d.ts.map +1 -0
  216. package/types/browser.d.ts +11 -0
  217. package/types/browser.d.ts.map +1 -0
  218. package/types/captcha.d.ts +42 -0
  219. package/types/captcha.d.ts.map +1 -0
  220. package/types/cbor.d.ts +4 -0
  221. package/types/cbor.d.ts.map +1 -0
  222. package/types/context.d.ts +3 -0
  223. package/types/context.d.ts.map +1 -0
  224. package/types/core.d.ts +180 -0
  225. package/types/core.d.ts.map +1 -0
  226. package/types/createUserCommand.d.ts +11 -0
  227. package/types/createUserCommand.d.ts.map +1 -0
  228. package/types/decide.d.ts +12 -0
  229. package/types/decide.d.ts.map +1 -0
  230. package/types/deletion.d.ts +17 -0
  231. package/types/deletion.d.ts.map +1 -0
  232. package/types/devTool.d.ts +3 -0
  233. package/types/devTool.d.ts.map +1 -0
  234. package/types/device.d.ts +7 -0
  235. package/types/device.d.ts.map +1 -0
  236. package/types/email.d.ts +73 -0
  237. package/types/email.d.ts.map +1 -0
  238. package/types/erase.d.ts +31 -0
  239. package/types/erase.d.ts.map +1 -0
  240. package/types/events.d.ts +139 -0
  241. package/types/events.d.ts.map +1 -0
  242. package/types/export.d.ts +54 -0
  243. package/types/export.d.ts.map +1 -0
  244. package/types/failure.d.ts +19 -0
  245. package/types/failure.d.ts.map +1 -0
  246. package/types/forwardAuth.d.ts +12 -0
  247. package/types/forwardAuth.d.ts.map +1 -0
  248. package/types/forwardRoutes.d.ts +11 -0
  249. package/types/forwardRoutes.d.ts.map +1 -0
  250. package/types/gate.d.ts +31 -0
  251. package/types/gate.d.ts.map +1 -0
  252. package/types/guard.d.ts +10 -0
  253. package/types/guard.d.ts.map +1 -0
  254. package/types/guards.d.ts +37 -0
  255. package/types/guards.d.ts.map +1 -0
  256. package/types/guesses.d.ts +18 -0
  257. package/types/guesses.d.ts.map +1 -0
  258. package/types/index.d.ts +47 -0
  259. package/types/index.d.ts.map +1 -0
  260. package/types/keys.d.ts +11 -0
  261. package/types/keys.d.ts.map +1 -0
  262. package/types/limits.d.ts +133 -0
  263. package/types/limits.d.ts.map +1 -0
  264. package/types/load.d.ts +18 -0
  265. package/types/load.d.ts.map +1 -0
  266. package/types/locale.d.ts +4 -0
  267. package/types/locale.d.ts.map +1 -0
  268. package/types/log.d.ts +2 -0
  269. package/types/log.d.ts.map +1 -0
  270. package/types/mail.d.ts +51 -0
  271. package/types/mail.d.ts.map +1 -0
  272. package/types/memoryStore.d.ts +3 -0
  273. package/types/memoryStore.d.ts.map +1 -0
  274. package/types/messages/fr.d.ts +409 -0
  275. package/types/messages/fr.d.ts.map +1 -0
  276. package/types/methods/account.d.ts +24 -0
  277. package/types/methods/account.d.ts.map +1 -0
  278. package/types/methods/admin.d.ts +30 -0
  279. package/types/methods/admin.d.ts.map +1 -0
  280. package/types/methods/audit.d.ts +11 -0
  281. package/types/methods/audit.d.ts.map +1 -0
  282. package/types/methods/email.d.ts +14 -0
  283. package/types/methods/email.d.ts.map +1 -0
  284. package/types/methods/magicLink.d.ts +12 -0
  285. package/types/methods/magicLink.d.ts.map +1 -0
  286. package/types/methods/oauth.d.ts +16 -0
  287. package/types/methods/oauth.d.ts.map +1 -0
  288. package/types/methods/oauthServer.d.ts +13 -0
  289. package/types/methods/oauthServer.d.ts.map +1 -0
  290. package/types/methods/organizations.d.ts +30 -0
  291. package/types/methods/organizations.d.ts.map +1 -0
  292. package/types/methods/passkeys.d.ts +19 -0
  293. package/types/methods/passkeys.d.ts.map +1 -0
  294. package/types/methods/password.d.ts +17 -0
  295. package/types/methods/password.d.ts.map +1 -0
  296. package/types/methods/sessions.d.ts +9 -0
  297. package/types/methods/sessions.d.ts.map +1 -0
  298. package/types/methods/tokens.d.ts +11 -0
  299. package/types/methods/tokens.d.ts.map +1 -0
  300. package/types/methods/twoFactor.d.ts +20 -0
  301. package/types/methods/twoFactor.d.ts.map +1 -0
  302. package/types/oauth/client.d.ts +39 -0
  303. package/types/oauth/client.d.ts.map +1 -0
  304. package/types/oauth/flow.d.ts +38 -0
  305. package/types/oauth/flow.d.ts.map +1 -0
  306. package/types/oauth/index.d.ts +6 -0
  307. package/types/oauth/index.d.ts.map +1 -0
  308. package/types/oauth/pkce.d.ts +10 -0
  309. package/types/oauth/pkce.d.ts.map +1 -0
  310. package/types/oauth/providers.d.ts +100 -0
  311. package/types/oauth/providers.d.ts.map +1 -0
  312. package/types/oauth/refresher.d.ts +55 -0
  313. package/types/oauth/refresher.d.ts.map +1 -0
  314. package/types/oauth/timeout.d.ts +2 -0
  315. package/types/oauth/timeout.d.ts.map +1 -0
  316. package/types/oauth/types.d.ts +75 -0
  317. package/types/oauth/types.d.ts.map +1 -0
  318. package/types/oauthTest.d.ts +23 -0
  319. package/types/oauthTest.d.ts.map +1 -0
  320. package/types/organizations.d.ts +111 -0
  321. package/types/organizations.d.ts.map +1 -0
  322. package/types/organizationsCore.d.ts +29 -0
  323. package/types/organizationsCore.d.ts.map +1 -0
  324. package/types/own.d.ts +55 -0
  325. package/types/own.d.ts.map +1 -0
  326. package/types/pages/english.d.ts +80 -0
  327. package/types/pages/english.d.ts.map +1 -0
  328. package/types/pages/forms.d.ts +204 -0
  329. package/types/pages/forms.d.ts.map +1 -0
  330. package/types/pages/index.d.ts +166 -0
  331. package/types/pages/index.d.ts.map +1 -0
  332. package/types/pages/organizationPage.d.ts +7 -0
  333. package/types/pages/organizationPage.d.ts.map +1 -0
  334. package/types/pages/routes/account/_guard.d.ts +3 -0
  335. package/types/pages/routes/account/_guard.d.ts.map +1 -0
  336. package/types/pages/routes/account/organizations/_guard.d.ts +3 -0
  337. package/types/pages/routes/account/organizations/_guard.d.ts.map +1 -0
  338. package/types/passkeys.d.ts +61 -0
  339. package/types/passkeys.d.ts.map +1 -0
  340. package/types/password.d.ts +49 -0
  341. package/types/password.d.ts.map +1 -0
  342. package/types/payloads.d.ts +4 -0
  343. package/types/payloads.d.ts.map +1 -0
  344. package/types/pg.d.ts +303 -0
  345. package/types/pg.d.ts.map +1 -0
  346. package/types/profile.d.ts +11 -0
  347. package/types/profile.d.ts.map +1 -0
  348. package/types/proof.d.ts +33 -0
  349. package/types/proof.d.ts.map +1 -0
  350. package/types/prune.d.ts +5 -0
  351. package/types/prune.d.ts.map +1 -0
  352. package/types/rates.d.ts +15 -0
  353. package/types/rates.d.ts.map +1 -0
  354. package/types/request.d.ts +119 -0
  355. package/types/request.d.ts.map +1 -0
  356. package/types/requiredTwoFactor.d.ts +16 -0
  357. package/types/requiredTwoFactor.d.ts.map +1 -0
  358. package/types/requirement.d.ts +4 -0
  359. package/types/requirement.d.ts.map +1 -0
  360. package/types/roles.d.ts +9 -0
  361. package/types/roles.d.ts.map +1 -0
  362. package/types/routes.d.ts +31 -0
  363. package/types/routes.d.ts.map +1 -0
  364. package/types/rules-check.d.ts +3 -0
  365. package/types/rules-check.d.ts.map +1 -0
  366. package/types/rules.d.ts +38 -0
  367. package/types/rules.d.ts.map +1 -0
  368. package/types/securityNotice.d.ts +7 -0
  369. package/types/securityNotice.d.ts.map +1 -0
  370. package/types/sessions.d.ts +32 -0
  371. package/types/sessions.d.ts.map +1 -0
  372. package/types/setup.d.ts +18 -0
  373. package/types/setup.d.ts.map +1 -0
  374. package/types/signInOptions.d.ts +6 -0
  375. package/types/signInOptions.d.ts.map +1 -0
  376. package/types/state.d.ts +25 -0
  377. package/types/state.d.ts.map +1 -0
  378. package/types/store.d.ts +295 -0
  379. package/types/store.d.ts.map +1 -0
  380. package/types/storeHelpers.d.ts +4 -0
  381. package/types/storeHelpers.d.ts.map +1 -0
  382. package/types/test.d.ts +21 -0
  383. package/types/test.d.ts.map +1 -0
  384. package/types/testCookies.d.ts +3 -0
  385. package/types/testCookies.d.ts.map +1 -0
  386. package/types/testHelpers.d.ts +34 -0
  387. package/types/testHelpers.d.ts.map +1 -0
  388. package/types/testing.d.ts +12 -0
  389. package/types/testing.d.ts.map +1 -0
  390. package/types/text.d.ts +2 -0
  391. package/types/text.d.ts.map +1 -0
  392. package/types/totp.d.ts +12 -0
  393. package/types/totp.d.ts.map +1 -0
  394. package/types/twoFactor.d.ts +41 -0
  395. package/types/twoFactor.d.ts.map +1 -0
  396. package/types/types.d.ts +17 -0
  397. package/types/types.d.ts.map +1 -0
  398. package/types/userData.d.ts +9 -0
  399. package/types/userData.d.ts.map +1 -0
  400. package/types/viewer.d.ts +23 -0
  401. package/types/viewer.d.ts.map +1 -0
  402. package/types/wayIn.d.ts +12 -0
  403. package/types/wayIn.d.ts.map +1 -0
  404. package/types/webauthn.d.ts +111 -0
  405. package/types/webauthn.d.ts.map +1 -0
  406. package/types/wsAuth.d.ts +14 -0
  407. package/types/wsAuth.d.ts.map +1 -0
@@ -0,0 +1,39 @@
1
+ --- server
2
+ import { action } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Message from '../components/message.kay'
6
+ import Panel from '../components/panel.kay'
7
+ import { failure, schemas } from '../forms'
8
+ import Button from '@kayjs/ui/button.kay'
9
+
10
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
11
+ // The link's token sits in the address, which no Referer header may carry to another site; `same-origin` still lets a form post carry its Origin.
12
+ export const page = { guards: false, security: { headers: { 'referrer-policy': 'same-origin' } } }
13
+
14
+ // The link only shows this button, so a mail scanner opening it spends nothing.
15
+ export const confirm = action(schemas.token, async ({ token }, ctx) => {
16
+ const result = await ctx.locals.auth.email.confirmChange(token)
17
+
18
+ if (!result.ok)
19
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'link' : result.error.code })
20
+
21
+ return ctx.redirect(localize(result.redirect))
22
+ })
23
+
24
+ const token = ctx.url.searchParams.get('token') ?? confirm.failed()?.values.token
25
+ const error = confirm.failed()?.data?.error
26
+ const landmark = ctx.locals.auth.features().pages?.main !== false
27
+ ---
28
+ head
29
+ title {wording('auth.confirmEmail.title', 'Confirm your new address')}
30
+ Panel(landmark={landmark} title={wording('auth.confirmEmail.title', 'Confirm your new address')})
31
+ if token
32
+ form.flex.flex-col.gap-3(action={confirm})
33
+ input(type="hidden" name="token" value={token})
34
+ p {wording('auth.confirmEmail.intro', 'Press the button to move your account to this address.')}
35
+ if error
36
+ Message(text={failure(error)})
37
+ Button(type="submit" tone="accent" label={wording('auth.confirmEmail.submit', 'Confirm the address')})
38
+ else
39
+ Message(text={failure('link')})
@@ -0,0 +1,104 @@
1
+ --- server
2
+ import { action } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Message from '../components/message.kay'
6
+ import Panel from '../components/panel.kay'
7
+ import { failure, ID_MAX, schemas } from '../forms'
8
+ import { organizationPage, organizationsPage } from '../organizationPage'
9
+ import Button from '@kayjs/ui/button.kay'
10
+
11
+ // Whoever holds the link opens this page, signed in or not, so none of the app's guards run here. The id sits in the address, which no Referer header may carry to another site.
12
+ export const page = { guards: false, security: { headers: { 'referrer-policy': 'same-origin' } } }
13
+
14
+ // The id the form posts is the one in the address, read again by the method, which answers for the signed-in user's address only.
15
+ export const accept = action(schemas.id, async ({ id }, ctx) => {
16
+ const auth = ctx.locals.auth
17
+
18
+ if (!auth.features().organizations)
19
+ return ctx.fail(404, { error: 'not-found' })
20
+
21
+ const result = await auth.organizations.acceptInvitation(id)
22
+
23
+ if (!result.ok)
24
+ return ctx.fail(400, { error: result.error.code })
25
+
26
+ const { organization } = result.membership
27
+
28
+ return ctx.redirect(organizationPage(auth.features(), organization))
29
+ })
30
+
31
+ export const reject = action(schemas.id, async ({ id }, ctx) => {
32
+ const auth = ctx.locals.auth
33
+
34
+ if (!auth.features().organizations)
35
+ return ctx.fail(404, { error: 'not-found' })
36
+
37
+ const result = await auth.organizations.rejectInvitation(id)
38
+
39
+ return result.ok ? ctx.redirect(organizationsPage(auth.features())) : ctx.fail(400, { error: result.error.code })
40
+ })
41
+
42
+ // Signs the wrong account out and comes back to this link to sign in with the right one.
43
+ export const switchAccount = action(schemas.id, async ({ id }, ctx) => {
44
+ const auth = ctx.locals.auth
45
+
46
+ if (!auth.features().organizations)
47
+ return ctx.fail(404, { error: 'not-found' })
48
+
49
+ const { signInPage, pages, prefix } = auth.features()
50
+ const signIn = localize(signInPage)
51
+
52
+ await auth.signOut()
53
+
54
+ return ctx.redirect(`${signIn}${signIn.includes('?') ? '&' : '?'}redirect=${encodeURIComponent(`${pages?.prefix ?? prefix}/invitation?invitation=${encodeURIComponent(id)}`)}`)
55
+ })
56
+
57
+ const auth = ctx.locals.auth
58
+ const features = auth.features()
59
+
60
+ if (!features.organizations)
61
+ ctx.notFound()
62
+
63
+ const prefix = features.pages?.prefix ?? features.prefix
64
+ const requested = ctx.url.searchParams.get('invitation') ?? accept.failed()?.values.id ?? reject.failed()?.values.id ?? ''
65
+ const id = requested.length <= ID_MAX ? requested : ''
66
+ const invitation = id !== '' ? await auth.organizations.invitation(id) : null
67
+ const user = invitation && await auth.via() === 'session' ? await auth.user() : null
68
+ const signedIn = user !== null && !user.guest
69
+ const mine = signedIn && user.email === invitation?.email
70
+ const unverified = mine && !user.emailVerified
71
+ const back = encodeURIComponent(`${prefix}/invitation?invitation=${encodeURIComponent(id)}`)
72
+ const refused = [accept, reject].map(form => form.failed()?.data?.error).find(error => error !== undefined)
73
+ const landmark = ctx.locals.auth.features().pages?.main !== false
74
+ ---
75
+ head
76
+ title {wording('auth.invitation.title', 'Invitation')}
77
+ Panel(landmark={landmark} title={wording('auth.invitation.title', 'Invitation')})
78
+ if invitation === null
79
+ Message(text={wording('auth.invitation.invalid', 'This invitation is invalid, expired or already used.')})
80
+ else
81
+ p {wording('auth.invitation.intro', 'You are invited to join {organization}.', { organization: invitation.organization.name })}
82
+ p.text-muted {wording('auth.invitation.sentTo', 'Sent to {email}.', { email: invitation.email })}
83
+ if refused
84
+ Message(text={failure(refused)})
85
+ if mine
86
+ if unverified
87
+ Message(text={failure('unverified')})
88
+ else
89
+ form.flex.flex-wrap.gap-3(action={accept})
90
+ input(type="hidden" name="id" value={invitation.id})
91
+ Button(type="submit" tone="accent" label={wording('auth.invitation.accept', 'Join')})
92
+ form(action={reject})
93
+ input(type="hidden" name="id" value={invitation.id})
94
+ Button(type="submit" label={wording('auth.invitation.reject', 'Decline')})
95
+ else if signedIn
96
+ Message(text={failure('wrong-account')})
97
+ form(action={switchAccount})
98
+ input(type="hidden" name="id" value={invitation.id})
99
+ Button(type="submit" label={wording('auth.invitation.switch', 'Sign in with another account')})
100
+ else
101
+ p {wording('auth.invitation.signInFirst', 'Sign in or create an account with this address to join.')}
102
+ .flex.flex-wrap.gap-3
103
+ a(href={localize(`${prefix}/sign-in?redirect=${back}`)}) {wording('auth.invitation.signIn', 'Sign in')}
104
+ a(href={localize(`${prefix}/register?redirect=${back}`)}) {wording('auth.invitation.register', 'Create an account')}
@@ -0,0 +1,42 @@
1
+ --- server
2
+ import { action } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Message from '../components/message.kay'
6
+ import Panel from '../components/panel.kay'
7
+ import { failure, schemas } from '../forms'
8
+ import Button from '@kayjs/ui/button.kay'
9
+
10
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
11
+ // The link's token sits in the address, which no Referer header may carry to another site; `same-origin` still lets a form post carry its Origin.
12
+ export const page = { guards: false, security: { headers: { 'referrer-policy': 'same-origin' } } }
13
+
14
+ // The link only shows this button, so a mail scanner opening it spends nothing.
15
+ export const spend = action(schemas.token, async ({ token }, ctx) => {
16
+ const result = await ctx.locals.auth.magicLink.signIn(token)
17
+
18
+ if (!result.ok)
19
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'link' : result.error.code })
20
+
21
+ return ctx.redirect(localize(result.redirect))
22
+ })
23
+
24
+ if (!ctx.locals.auth.features().magicLink)
25
+ ctx.notFound()
26
+
27
+ const token = ctx.url.searchParams.get('token') ?? spend.failed()?.values.token
28
+ const error = spend.failed()?.data?.error
29
+ const landmark = ctx.locals.auth.features().pages?.main !== false
30
+ ---
31
+ head
32
+ title {wording('auth.magic.title', 'Sign in')}
33
+ Panel(landmark={landmark} title={wording('auth.magic.title', 'Sign in')})
34
+ if token
35
+ form.flex.flex-col.gap-3(action={spend})
36
+ input(type="hidden" name="token" value={token})
37
+ p {wording('auth.magic.intro', 'Press the button to finish signing in.')}
38
+ if error
39
+ Message(text={failure(error)})
40
+ Button(type="submit" tone="accent" label={wording('auth.magic.submit', 'Sign in')})
41
+ else
42
+ Message(text={failure('link')})
@@ -0,0 +1,80 @@
1
+ --- server
2
+ import { action } from 'kay'
3
+ import { wording } from 'kay/words'
4
+ import Message from '../../components/message.kay'
5
+ import Panel from '../../components/panel.kay'
6
+ import { schemas, scopeLabel } from '../../forms'
7
+ import Button from '@kayjs/ui/button.kay'
8
+
9
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep an MCP client out.
10
+ export const page = { guards: false }
11
+
12
+ const FIELDS = ['client_id', 'redirect_uri', 'response_type', 'code_challenge', 'code_challenge_method', 'scope', 'state', 'resource', 'prompt'] as const
13
+
14
+ // The answer is checked again as the request is, so a form edited by hand gets no more than the request could.
15
+ export const decide = action(schemas.oauthConsent, async ({ decision, ...fields }, ctx) => {
16
+ const result = await ctx.locals.auth.oauthServer.decide({ ...fields, scope: fields.scope === '' ? undefined : fields.scope, state: fields.state === '' ? undefined : fields.state, resource: fields.resource === '' ? undefined : fields.resource }, decision === 'allow')
17
+
18
+ return result.ok ? ctx.redirect(result.redirect) : ctx.fail(400, { error: result.error.code })
19
+ })
20
+
21
+ const auth = ctx.locals.auth
22
+ const refused = decide.failed()?.data?.error
23
+ const request: Parameters<typeof auth.oauthServer.authorize>[0] = {}
24
+
25
+ for (const field of FIELDS) {
26
+ const value = ctx.url.searchParams.get(field)
27
+
28
+ if (value !== null)
29
+ request[field] = value
30
+ }
31
+
32
+ const resources = ctx.url.searchParams.getAll('resource')
33
+
34
+ if (resources.length > 1)
35
+ request.resource = resources
36
+
37
+ const outcome = refused ? undefined : await auth.oauthServer.authorize(request)
38
+
39
+ if (outcome?.kind === 'sign-in')
40
+ ctx.redirect(auth.signInUrl())
41
+
42
+ if (outcome?.kind === 'redirect')
43
+ // kay-allow external-redirect: the client's registered redirect URI, matched exactly
44
+ ctx.redirect(outcome.url)
45
+
46
+ const problem = refused ?? (outcome?.kind === 'error' ? outcome.error : undefined)
47
+ const words = {
48
+ 'client': ['client', 'This app is not known to this site, so nothing was shared with it.'],
49
+ 'redirect_uri': ['redirectUri', 'This app asked to come back to an address it did not register, so nothing was shared with it.'],
50
+ 'signed-out': ['signedOut', 'You are signed out: start again from the app.'],
51
+ } as const
52
+ const landmark = ctx.locals.auth.features().pages?.main !== false
53
+ ---
54
+ head
55
+ title {wording('auth.consent.title', 'Allow access')}
56
+ meta(name="robots" content="noindex")
57
+ Panel(landmark={landmark} title={wording('auth.consent.title', 'Allow access')})
58
+ if problem
59
+ Message(text={wording(`auth.consent.${words[problem][0]}`, words[problem][1])})
60
+ else if outcome?.kind === 'consent'
61
+ p {wording('auth.consent.intro', '{client} wants to use your account.', { client: outcome.client.name })}
62
+ if outcome.client.host
63
+ p.text-muted {wording('auth.consent.from', 'This app lives at {host}. Only continue if you started this sign-in.', { host: outcome.client.host })}
64
+ else
65
+ p.text-muted {wording('auth.consent.unverified', 'The app named itself; nothing checked that name. Only continue if you started this sign-in.')}
66
+ p.text-muted {wording('auth.consent.returnsTo', 'After you decide, you go back to {host}.', { host: outcome.redirectHost })}
67
+ p {wording('auth.consent.scopes', 'It will be allowed to')}
68
+ ul.list-disc.pl-5
69
+ if outcome.scopes === null
70
+ li {wording('auth.consent.everything', 'Do everything you can do')}
71
+ else if outcome.scopes.length === 0
72
+ li {wording('auth.consent.noScopes', 'Identify you, with no access to the features that need a permission')}
73
+ else
74
+ for scope of outcome.scopes key={scope}
75
+ li {scopeLabel(scope)}
76
+ form.flex.flex-wrap.gap-3(action={decide})
77
+ for field of Object.entries(outcome.fields) key={field[0]}
78
+ input(type="hidden" name={field[0]} value={field[1]})
79
+ Button(type="submit" name="decision" value="allow" tone="accent" label={wording('auth.consent.allow', 'Allow')})
80
+ Button(type="submit" name="decision" value="deny" label={wording('auth.consent.deny', 'Deny')})
@@ -0,0 +1,83 @@
1
+ --- server
2
+ import { action, localPath } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Captcha from '../components/captcha.kay'
6
+ import Message from '../components/message.kay'
7
+ import Panel from '../components/panel.kay'
8
+ import ProviderButtons from '../components/provider-buttons.kay'
9
+ import Separator from '../components/separator.kay'
10
+ import { captchaFor, captchaOf, guarded, issue, refusal, refusedFor, remembered, rememberOf, schemas } from '../forms'
11
+ import Button from '@kayjs/ui/button.kay'
12
+ import FormField from '@kayjs/ui/form-field.kay'
13
+
14
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
15
+ export const page = { guards: false }
16
+
17
+ export const register = action(schemas.register, async (input, ctx) => {
18
+ const result = await ctx.locals.auth.password.register({ email: input.email, password: input.password, ...(input.username === undefined ? {} : { username: input.username }), locale: ctx.locale }, { remember: remembered(ctx.locals.auth.features(), input.remember), redirect: input.redirect, captcha: captchaOf(input) })
19
+
20
+ if (!result.ok)
21
+ return ctx.fail(400, 'errors' in result ? { fields: result.errors } : { error: refusedFor(result.error.code, input) })
22
+
23
+ if (!result.signedIn)
24
+ return 'verify'
25
+
26
+ return ctx.redirect(localPath(input.redirect, localize(ctx.locals.auth.features().pages?.home ?? '/')))
27
+ }, guarded)
28
+
29
+ const auth = ctx.locals.auth
30
+ const features = auth.features()
31
+
32
+ if (!features.signUp || !features.password)
33
+ ctx.notFound()
34
+
35
+ const prefix = features.pages?.prefix ?? features.prefix
36
+ const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
37
+ const user = await auth.user()
38
+
39
+ if (user && !user.guest)
40
+ ctx.redirect(redirect)
41
+
42
+ const { askRemember, providerLinks } = rememberOf(features, redirect, provider => wording('auth.signIn.provider', 'Continue with {provider}', { provider }), features.providers)
43
+ const captcha = await captchaFor(auth, features.captcha, 'register')
44
+ const refused = register.failed()?.data
45
+ const fields = refused && 'fields' in refused ? refused.fields : {}
46
+ const error = refusal(register.failed(), features.captcha)
47
+ const verify = register.succeeded() !== undefined
48
+ const landmark = ctx.locals.auth.features().pages?.main !== false
49
+ const privacy = features.pages?.privacy
50
+ const purpose = features.pages?.purpose
51
+ ---
52
+ head
53
+ title {wording('auth.register.title', 'Create an account')}
54
+ Panel(landmark={landmark} title={wording('auth.register.title', 'Create an account')})
55
+ if verify
56
+ Message(tone="success" text={wording('auth.register.checkMail', 'Check your mail to continue.')})
57
+ else
58
+ if features.providers.length > 0
59
+ .flex.flex-col.gap-2
60
+ ProviderButtons(providers={providerLinks})
61
+ Separator
62
+ form.flex.flex-col.gap-3(action={register})
63
+ input(type="hidden" name="redirect" value={redirect})
64
+ FormField(label={wording('auth.email', 'Email')} issue={fields.email && issue('email', fields.email)} required)
65
+ input(name="email" type="email" autocomplete="email" required)
66
+ FormField(label={wording('auth.password', 'Password')} hint={wording('auth.passwordHint', 'At least {min} characters', { min: features.rules.password.min })} issue={fields.password && issue('password', fields.password, features.rules)} required)
67
+ input(name="password" type="password" autocomplete="new-password" minlength={features.rules.password.min} required)
68
+ if askRemember
69
+ label.flex.items-center.gap-2
70
+ input(name="remember" type="checkbox")
71
+ span {wording('auth.remember', 'Keep me signed in')}
72
+ if features.captcha && captcha.shown
73
+ Captcha(widget={features.captcha} challenge={captcha.challenge} action="register")
74
+ if error
75
+ Message(text={error})
76
+ Button(type="submit" tone="accent" label={wording('auth.register.submit', 'Create the account')})
77
+ if privacy || purpose
78
+ p.text-xs.text-muted
79
+ | {purpose ?? wording('auth.register.purpose', 'We use your email address to create and secure your account.')}{' '}
80
+ if privacy
81
+ a(href={privacy} target="_blank" rel="noopener noreferrer" data-new-tab="") {wording('auth.register.privacy', 'Privacy policy')}
82
+ p.text-center
83
+ | {wording('auth.register.haveAccount', 'Already have an account?')} #[a(href={localize(`${prefix}/sign-in?redirect=${encodeURIComponent(redirect)}`)}) {wording('auth.register.signIn', 'Sign in')}]
@@ -0,0 +1,73 @@
1
+ --- server
2
+ import { action, localPath } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Captcha from '../components/captcha.kay'
6
+ import Message from '../components/message.kay'
7
+ import Panel from '../components/panel.kay'
8
+ import { captchaFor, captchaOf, failure, guarded, issue, refusal, refusedFor, schemas } from '../forms'
9
+ import Button from '@kayjs/ui/button.kay'
10
+ import FormField from '@kayjs/ui/form-field.kay'
11
+
12
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
13
+ // The link's token sits in the address, which no Referer header may carry to another site; `same-origin` still lets a form post carry its Origin.
14
+ export const page = { guards: false, security: { headers: { 'referrer-policy': 'same-origin' } } }
15
+
16
+ export const request = action(schemas.requestReset, async (input, ctx) => {
17
+ const result = await ctx.locals.auth.password.requestReset(input.email, { redirect: input.redirect, captcha: captchaOf(input) })
18
+
19
+ return result.ok ? 'sent' : ctx.fail(400, { error: refusedFor(result.error.code, input) })
20
+ }, guarded)
21
+
22
+ export const reset = action(schemas.reset, async ({ token, password }, ctx) => {
23
+ const result = await ctx.locals.auth.password.reset(token, password)
24
+
25
+ if (!result.ok)
26
+ return ctx.fail(400, 'errors' in result ? { password: result.errors.password } : { error: result.error.code === 'invalid' ? 'link' : result.error.code })
27
+
28
+ return ctx.redirect(localize(result.redirect))
29
+ })
30
+
31
+ const auth = ctx.locals.auth
32
+ const features = auth.features()
33
+
34
+ if (!features.password)
35
+ ctx.notFound()
36
+
37
+ const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
38
+ const token = ctx.url.searchParams.get('token') ?? reset.failed()?.values.token
39
+ // Only the request form asks for a captcha, so a visit with a token never reads the client's attempts.
40
+ const captcha = token ? { shown: false, challenge: null } : await captchaFor(auth, features.captcha, 'reset')
41
+ const requestError = refusal(request.failed(), features.captcha)
42
+ const refused = reset.failed()?.data
43
+ const sent = request.succeeded() !== undefined
44
+ const landmark = ctx.locals.auth.features().pages?.main !== false
45
+ ---
46
+ head
47
+ title {wording('auth.reset.title', 'Reset your password')}
48
+ if token
49
+ Panel(landmark={landmark} title={wording('auth.reset.choose', 'Choose a new password')})
50
+ form.flex.flex-col.gap-3(action={reset})
51
+ input(type="hidden" name="token" value={token})
52
+ FormField(label={wording('auth.newPassword', 'New password')} hint={wording('auth.passwordHint', 'At least {min} characters', { min: features.rules.password.min })} issue={refused && 'password' in refused ? issue('password', refused.password, features.rules) : undefined} required)
53
+ input(name="password" type="password" autocomplete="new-password" minlength={features.rules.password.min} required)
54
+ if refused && 'error' in refused
55
+ Message(text={failure(refused.error)})
56
+ Button(type="submit" tone="accent" label={wording('auth.reset.save', 'Save the password')})
57
+ else
58
+ Panel(landmark={landmark} title={wording('auth.reset.title', 'Reset your password')})
59
+ if sent
60
+ Message(tone="success" text={wording('auth.reset.sent', 'If an account has this address, a link to reset its password is on its way.')})
61
+ else if !features.email
62
+ Message(text={failure('unavailable')})
63
+ else
64
+ form.flex.flex-col.gap-3(action={request})
65
+ input(type="hidden" name="redirect" value={redirect})
66
+ p {wording('auth.reset.intro', 'We will mail you a link to choose a new password.')}
67
+ FormField(label={wording('auth.email', 'Email')} required)
68
+ input(name="email" type="email" autocomplete="email" required)
69
+ if features.captcha && captcha.shown
70
+ Captcha(widget={features.captcha} challenge={captcha.challenge} action="reset")
71
+ if requestError
72
+ Message(text={requestError})
73
+ Button(type="submit" tone="accent" label={wording('auth.reset.submit', 'Mail me a link')})
@@ -0,0 +1,128 @@
1
+ --- server
2
+ import { action, localPath } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Captcha from '../components/captcha.kay'
6
+ import Message from '../components/message.kay'
7
+ import Panel from '../components/panel.kay'
8
+ import PasskeySignIn from '../components/passkey-sign-in.kay'
9
+ import ProviderButtons from '../components/provider-buttons.kay'
10
+ import Separator from '../components/separator.kay'
11
+ import { captchaFor, captchaOf, failure, guarded, oauthFailure, refusal, refusedFor, remembered, rememberOf, schemas } from '../forms'
12
+ import Button from '@kayjs/ui/button.kay'
13
+ import FormField from '@kayjs/ui/form-field.kay'
14
+
15
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
16
+ export const page = { guards: false }
17
+
18
+ export const password = action(schemas.signIn, async (input, ctx) => {
19
+ const result = await ctx.locals.auth.password.signIn({ identifier: input.identifier, password: input.password }, { remember: remembered(ctx.locals.auth.features(), input.remember), redirect: input.redirect, captcha: captchaOf(input) })
20
+
21
+ if (!result.ok)
22
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'credentials' : refusedFor(result.error.code, input) })
23
+
24
+ return ctx.redirect(localize(result.redirect))
25
+ }, guarded)
26
+
27
+ export const magicLink = action(schemas.magicLink, async (input, ctx) => {
28
+ const result = await ctx.locals.auth.magicLink.request(input.email, { redirect: input.redirect, captcha: captchaOf(input), remember: remembered(ctx.locals.auth.features(), input.remember) })
29
+
30
+ return result.ok ? input.email : ctx.fail(400, { error: refusedFor(result.error.code, input) })
31
+ }, guarded)
32
+
33
+ export const emailCode = action(schemas.emailCode, async ({ code }, ctx) => {
34
+ const result = await ctx.locals.auth.magicLink.signInWithCode(code)
35
+
36
+ if (!result.ok)
37
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'code' : result.error.code })
38
+
39
+ return ctx.redirect(localize(result.redirect))
40
+ })
41
+
42
+ export const passkeyOptions = action(async ctx => ctx.locals.auth.features().passkeys ? ctx.locals.auth.passkeys.signInOptions() : ctx.fail(404, { error: 'not-found' }))
43
+
44
+ export const passkey = action(schemas.passkey, async (input, ctx) => {
45
+ const result = await ctx.locals.auth.passkeys.signIn(input.response, { redirect: input.redirect, remember: remembered(ctx.locals.auth.features(), input.remember) })
46
+
47
+ return !result.ok ? ctx.fail(400, { error: result.error.code }) : ctx.redirect(localize(result.redirect))
48
+ })
49
+
50
+ const auth = ctx.locals.auth
51
+ const features = auth.features()
52
+ const prefix = features.pages?.prefix ?? features.prefix
53
+ const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
54
+ const user = await auth.user()
55
+
56
+ // A guest signs in to keep what they did; anyone else is signed in already.
57
+ if (user && !user.guest)
58
+ ctx.redirect(redirect)
59
+
60
+ const [captchaSignIn, captchaMagic] = await Promise.all([captchaFor(auth, features.captcha, 'sign-in'), captchaFor(auth, features.captcha, 'magic-link')])
61
+ const pending = await auth.email.pendingCode()
62
+ const oauthError = oauthFailure(ctx.url.searchParams.get('error'))
63
+ const signInError = refusal(password.failed(), features.captcha)
64
+ const mailError = refusal(magicLink.failed(), features.captcha)
65
+ const codeError = emailCode.failed()?.data?.error
66
+ const mailedTo = magicLink.succeeded()?.data
67
+ const { askRemember, providerLinks } = rememberOf(features, redirect, provider => wording('auth.signIn.provider', 'Continue with {provider}', { provider }), features.providers)
68
+ const codes = features.codes?.['magic-link'] ?? 'link'
69
+ const landmark = ctx.locals.auth.features().pages?.main !== false
70
+ ---
71
+ head
72
+ title {wording('auth.signIn.title', 'Sign in')}
73
+ Panel(landmark={landmark} title={wording('auth.signIn.title', 'Sign in')})
74
+ if oauthError
75
+ Message(text={oauthError})
76
+ if features.providers.length > 0
77
+ .flex.flex-col.gap-2
78
+ ProviderButtons(providers={providerLinks})
79
+ Separator
80
+ if pending?.type === 'sign-in'
81
+ form.flex.flex-col.gap-3(action={emailCode})
82
+ p {wording('auth.signIn.codeSent', 'Type the code mailed to {email}.', { email: pending.email })}
83
+ FormField(label={wording('auth.code', 'Code')} required)
84
+ input(name="code" inputmode="numeric" autocomplete="one-time-code" required)
85
+ if codeError
86
+ Message(text={failure(codeError)})
87
+ Button(type="submit" tone="accent" label={wording('auth.signIn.codeSubmit', 'Sign in')})
88
+ if features.password
89
+ form.flex.flex-col.gap-3(action={password})
90
+ input(type="hidden" name="redirect" value={redirect})
91
+ FormField(label={wording('auth.signIn.identifier', 'Email or username')} required)
92
+ input(name="identifier" autocomplete="username webauthn" required)
93
+ FormField(label={wording('auth.password', 'Password')} required)
94
+ input(name="password" type="password" autocomplete="current-password" required)
95
+ if askRemember
96
+ label.flex.items-center.gap-2
97
+ input(name="remember" type="checkbox")
98
+ span {wording('auth.remember', 'Keep me signed in')}
99
+ if features.captcha && captchaSignIn.shown
100
+ Captcha(widget={features.captcha} challenge={captchaSignIn.challenge} action="sign-in")
101
+ if signInError
102
+ Message(text={signInError})
103
+ Button(type="submit" tone="accent" label={wording('auth.signIn.submit', 'Sign in')})
104
+ if features.email
105
+ a(href={localize(`${prefix}/reset-password?redirect=${encodeURIComponent(redirect)}`)}) {wording('auth.signIn.forgot', 'Forgot your password?')}
106
+ if features.passkeys
107
+ PasskeySignIn(redirect={redirect} label={wording('auth.signIn.passkey', 'Sign in with a passkey')} failed={failure('invalid')})
108
+ if features.magicLink
109
+ if features.password || features.passkeys
110
+ Separator
111
+ if mailedTo && codes === 'link'
112
+ Message(tone="success" text={wording('auth.signIn.sent', 'If an account has this address, a mail is on its way.')})
113
+ form.flex.flex-col.gap-3(action={magicLink})
114
+ input(type="hidden" name="redirect" value={redirect})
115
+ FormField(label={wording('auth.email', 'Email')} required)
116
+ input(name="email" type="email" autocomplete="email" required)
117
+ if askRemember
118
+ label.flex.items-center.gap-2
119
+ input(name="remember" type="checkbox")
120
+ span {wording('auth.remember', 'Keep me signed in')}
121
+ if features.captcha && captchaMagic.shown
122
+ Captcha(widget={features.captcha} challenge={captchaMagic.challenge} action="magic-link")
123
+ if mailError
124
+ Message(text={mailError})
125
+ Button(type="submit" label={codes === 'link' ? wording('auth.signIn.magic', 'Mail me a sign-in link') : wording('auth.signIn.magicCode', 'Mail me a sign-in code')})
126
+ if features.signUp && features.password
127
+ p.text-center
128
+ | {wording('auth.signIn.noAccount', 'No account yet?')} #[a(href={localize(`${prefix}/register?redirect=${encodeURIComponent(redirect)}`)}) {wording('auth.signIn.register', 'Create one')}]
@@ -0,0 +1,61 @@
1
+ --- server
2
+ import { action, localPath } from 'kay'
3
+ import { localize } from 'kay/i18n'
4
+ import { wording } from 'kay/words'
5
+ import Message from '../components/message.kay'
6
+ import Panel from '../components/panel.kay'
7
+ import PasskeyTwoFactor from '../components/passkey-two-factor.kay'
8
+ import { failure, schemas } from '../forms'
9
+ import Button from '@kayjs/ui/button.kay'
10
+ import FormField from '@kayjs/ui/form-field.kay'
11
+
12
+ // Visitors reach this page before signing in, so none of the app's guards run here, whatever they check: one asking for a session would keep everyone out.
13
+ export const page = { guards: false }
14
+
15
+ export const verify = action(schemas.twoFactor, async ({ code }, ctx) => {
16
+ const result = await ctx.locals.auth.twoFactor.verify(code)
17
+
18
+ if (!result.ok)
19
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'code' : result.error.code })
20
+
21
+ return ctx.redirect(localize(result.redirect))
22
+ })
23
+
24
+ export const passkeyOptions = action(async (ctx) => {
25
+ const result = await ctx.locals.auth.twoFactor.passkeyOptions()
26
+
27
+ return result.ok ? result.options : ctx.fail(400, { error: result.error.code })
28
+ })
29
+
30
+ export const passkey = action(schemas.passkey, async (input, ctx) => {
31
+ const result = await ctx.locals.auth.twoFactor.verifyPasskey(input.response)
32
+
33
+ if (!result.ok)
34
+ return ctx.fail(400, { error: result.error.code === 'invalid' ? 'code' : result.error.code })
35
+
36
+ return ctx.redirect(localize(result.redirect))
37
+ })
38
+
39
+ const auth = ctx.locals.auth
40
+ const features = auth.features()
41
+ const pending = await auth.twoFactor.pending()
42
+ const redirect = localPath(ctx.url.searchParams.get('redirect'), localize(features.pages?.home ?? '/'))
43
+ const error = verify.failed()?.data?.error
44
+ const landmark = ctx.locals.auth.features().pages?.main !== false
45
+ ---
46
+ head
47
+ title {wording('auth.twoFactor.title', 'Two-step verification')}
48
+ Panel(landmark={landmark} title={wording('auth.twoFactor.title', 'Two-step verification')})
49
+ if pending
50
+ form.flex.flex-col.gap-3(action={verify})
51
+ p {wording('auth.twoFactor.intro', 'Type the code your authenticator app shows, or one of your recovery codes.')}
52
+ FormField(label={wording('auth.code', 'Code')} required)
53
+ input(name="code" autocomplete="one-time-code" autocapitalize="off" spellcheck="false" required)
54
+ if error
55
+ Message(text={failure(error)})
56
+ Button(type="submit" tone="accent" label={wording('auth.twoFactor.submit', 'Verify')})
57
+ if features.passkeys
58
+ PasskeyTwoFactor(label={wording('auth.twoFactor.passkey', 'Use a passkey instead')} failed={failure('invalid')})
59
+ else
60
+ p {wording('auth.twoFactor.nothing', 'No sign-in is waiting for a code.')}
61
+ a(href={localize(`${features.signInPage}?redirect=${encodeURIComponent(redirect)}`)}) {wording('auth.signIn.title', 'Sign in')}