@humanonlyweb/create-nuxtflare 0.0.0-stage → 0.1.0

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 (138) hide show
  1. package/README.md +55 -2
  2. package/dist/cli.js +322 -0
  3. package/dist/template/.env.example +22 -0
  4. package/dist/template/.github/workflows/ci.yml +96 -0
  5. package/dist/template/.oxfmtrc.json +18 -0
  6. package/dist/template/.oxlintrc.json +69 -0
  7. package/dist/template/CLAUDE.md +42 -0
  8. package/dist/template/_gitignore +14 -0
  9. package/dist/template/app/app.vue +3 -0
  10. package/dist/template/app/components/qr-code.vue +36 -0
  11. package/dist/template/app/components/totp-setup.vue +151 -0
  12. package/dist/template/app/composables/use-features.ts +1 -0
  13. package/dist/template/app/composables/use-me.ts +10 -0
  14. package/dist/template/app/middleware/admin.global.ts +12 -0
  15. package/dist/template/app/middleware/auth.ts +6 -0
  16. package/dist/template/app/middleware/guest.ts +4 -0
  17. package/dist/template/app/pages/account.vue +22 -0
  18. package/dist/template/app/pages/admin/index.vue +16 -0
  19. package/dist/template/app/pages/admin/login.vue +39 -0
  20. package/dist/template/app/pages/index.vue +18 -0
  21. package/dist/template/app/pages/login.vue +110 -0
  22. package/dist/template/app/pages/register.vue +51 -0
  23. package/dist/template/app/utils/api-error.ts +50 -0
  24. package/dist/template/nuxt.config.ts +115 -0
  25. package/dist/template/package.json +59 -0
  26. package/dist/template/scripts/admin-password.ts +123 -0
  27. package/dist/template/server/api/account/email/index.delete.ts +5 -0
  28. package/dist/template/server/api/account/email/index.post.ts +8 -0
  29. package/dist/template/server/api/account/index.delete.ts +18 -0
  30. package/dist/template/server/api/account/index.patch.ts +16 -0
  31. package/dist/template/server/api/account/sessions.get.ts +11 -0
  32. package/dist/template/server/api/account/totp/cancel.post.ts +5 -0
  33. package/dist/template/server/api/account/totp/confirm.post.ts +10 -0
  34. package/dist/template/server/api/account/totp/disable.post.ts +8 -0
  35. package/dist/template/server/api/account/totp/index.get.ts +6 -0
  36. package/dist/template/server/api/account/totp/start.post.ts +8 -0
  37. package/dist/template/server/api/admin/me.get.ts +7 -0
  38. package/dist/template/server/api/admin/session/index.delete.ts +12 -0
  39. package/dist/template/server/api/admin/session/index.post.ts +18 -0
  40. package/dist/template/server/api/auth/email/index.post.ts +13 -0
  41. package/dist/template/server/api/auth/login/totp.post.ts +15 -0
  42. package/dist/template/server/api/auth/login.post.ts +17 -0
  43. package/dist/template/server/api/auth/logout-everywhere.post.ts +17 -0
  44. package/dist/template/server/api/auth/logout.post.ts +14 -0
  45. package/dist/template/server/api/auth/register.post.ts +18 -0
  46. package/dist/template/server/api/auth/reset/check.post.ts +5 -0
  47. package/dist/template/server/api/auth/reset/index.post.ts +5 -0
  48. package/dist/template/server/api/auth/reset/request.post.ts +5 -0
  49. package/dist/template/server/api/auth/verify/index.post.ts +17 -0
  50. package/dist/template/server/api/auth/verify/send.post.ts +7 -0
  51. package/dist/template/server/api/me.get.ts +8 -0
  52. package/dist/template/server/app/admin/auth.test.ts +41 -0
  53. package/dist/template/server/app/admin/auth.ts +133 -0
  54. package/dist/template/server/app/features.ts +18 -0
  55. package/dist/template/server/app/identity/email-change.ts +147 -0
  56. package/dist/template/server/app/identity/identity.test.ts +119 -0
  57. package/dist/template/server/app/identity/lockout.ts +93 -0
  58. package/dist/template/server/app/identity/password-reset.ts +119 -0
  59. package/dist/template/server/app/identity/service.ts +379 -0
  60. package/dist/template/server/app/identity/sessions.ts +141 -0
  61. package/dist/template/server/app/identity/state.ts +41 -0
  62. package/dist/template/server/app/identity/totp-crypto.ts +89 -0
  63. package/dist/template/server/app/identity/totp.test.ts +103 -0
  64. package/dist/template/server/app/identity/totp.ts +361 -0
  65. package/dist/template/server/app/identity/verification.ts +120 -0
  66. package/dist/template/server/app/notifications/email.ts +12 -0
  67. package/dist/template/server/app/notifications/render.test.ts +26 -0
  68. package/dist/template/server/app/notifications/render.ts +82 -0
  69. package/dist/template/server/app/notifications/samples.ts +11 -0
  70. package/dist/template/server/app/notifications/templates.ts +37 -0
  71. package/dist/template/server/assets/email/blocks/button.html +1 -0
  72. package/dist/template/server/assets/email/blocks/lead.html +1 -0
  73. package/dist/template/server/assets/email/blocks/note.html +1 -0
  74. package/dist/template/server/assets/email/blocks/text.html +1 -0
  75. package/dist/template/server/assets/email/layout.html +22 -0
  76. package/dist/template/server/cloudflare/exports/rate-limit.do.ts +38 -0
  77. package/dist/template/server/core/app-layer.ts +33 -0
  78. package/dist/template/server/core/db/atomic-d1.ts +21 -0
  79. package/dist/template/server/core/db/atomic.ts +86 -0
  80. package/dist/template/server/core/db/chunked.ts +13 -0
  81. package/dist/template/server/core/db/migrations.ts +149 -0
  82. package/dist/template/server/core/db/migrator.ts +39 -0
  83. package/dist/template/server/core/http/body-limit.ts +35 -0
  84. package/dist/template/server/core/http/csrf.ts +16 -0
  85. package/dist/template/server/core/http/device-label.ts +27 -0
  86. package/dist/template/server/core/http/handle.ts +271 -0
  87. package/dist/template/server/core/http/limit-key.ts +17 -0
  88. package/dist/template/server/core/http/page-csp.ts +16 -0
  89. package/dist/template/server/core/http/public-failure.ts +9 -0
  90. package/dist/template/server/core/http/request-user.ts +13 -0
  91. package/dist/template/server/core/http/runtime.ts +30 -0
  92. package/dist/template/server/core/http/session-cookie.ts +29 -0
  93. package/dist/template/server/core/platform/app-config.ts +55 -0
  94. package/dist/template/server/core/platform/clock.ts +4 -0
  95. package/dist/template/server/core/platform/database.ts +78 -0
  96. package/dist/template/server/core/platform/email-assets.ts +32 -0
  97. package/dist/template/server/core/platform/id-generator.ts +29 -0
  98. package/dist/template/server/core/platform/kv.ts +54 -0
  99. package/dist/template/server/core/platform/mailer-cloudflare.ts +51 -0
  100. package/dist/template/server/core/platform/mailer.ts +21 -0
  101. package/dist/template/server/core/platform/password-hasher.ts +27 -0
  102. package/dist/template/server/core/platform/pii.ts +80 -0
  103. package/dist/template/server/core/platform/provider-http.ts +21 -0
  104. package/dist/template/server/core/platform/rate-limiter-strict.ts +42 -0
  105. package/dist/template/server/core/platform/rate-limiter.ts +35 -0
  106. package/dist/template/server/core/platform/runtime-config-schema.ts +15 -0
  107. package/dist/template/server/core/platform/seal.ts +68 -0
  108. package/dist/template/server/core/platform/sliding-window.ts +39 -0
  109. package/dist/template/server/core/platform/tokens.ts +36 -0
  110. package/dist/template/server/core/platform/turnstile.ts +61 -0
  111. package/dist/template/server/middleware/00-body-limit.ts +10 -0
  112. package/dist/template/server/middleware/00-security-headers.ts +19 -0
  113. package/dist/template/server/middleware/01-user-session.ts +25 -0
  114. package/dist/template/server/plugins/page-csp.ts +15 -0
  115. package/dist/template/server/plugins/sessions.ts +21 -0
  116. package/dist/template/server/routes/(dev)/emails.get.dev.ts +37 -0
  117. package/dist/template/server/routes/auth/github.get.ts +96 -0
  118. package/dist/template/server/routes/auth/google.get.ts +61 -0
  119. package/dist/template/server/test/email-assets.ts +8 -0
  120. package/dist/template/server/test/invariants.test.ts +83 -0
  121. package/dist/template/server/test/kit.ts +83 -0
  122. package/dist/template/server/utils/define-validated-handler.ts +78 -0
  123. package/dist/template/server/utils/errors.ts +16 -0
  124. package/dist/template/server/utils/read-body.ts +34 -0
  125. package/dist/template/server/utils/user-actor.ts +10 -0
  126. package/dist/template/server.ts +5 -0
  127. package/dist/template/shared/domain/errors.ts +33 -0
  128. package/dist/template/shared/types/auth.d.ts +13 -0
  129. package/dist/template/shared/utils/admin.ts +13 -0
  130. package/dist/template/shared/utils/auth.ts +132 -0
  131. package/dist/template/shared/utils/ids.ts +21 -0
  132. package/dist/template/shared/utils/local-path.ts +2 -0
  133. package/dist/template/shared/utils/schema-validation.ts +59 -0
  134. package/dist/template/shared/utils/unknown.ts +9 -0
  135. package/dist/template/tsconfig.json +17 -0
  136. package/dist/template/vitest.config.ts +10 -0
  137. package/dist/template/wrangler.json +46 -0
  138. package/package.json +28 -4
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "nuxtflare-app",
3
+ "private": true,
4
+ "type": "module",
5
+ "scripts": {
6
+ "build": "nuxt build",
7
+ "dev": "nuxt dev",
8
+ "generate": "nuxt generate",
9
+ "preview": "nuxt preview",
10
+ "postinstall": "wrangler types && nuxt prepare",
11
+ "prepare": "effect-tsgo patch --no-typescript --oxlint && simple-git-hooks",
12
+ "typecheck": "wrangler types && vue-tsc --build",
13
+ "lint": "oxlint --deny-warnings",
14
+ "lint:fix": "oxlint --fix",
15
+ "fmt": "oxfmt",
16
+ "fmt:check": "oxfmt --check",
17
+ "test": "vitest run",
18
+ "test:affected": "vitest run --since-last-run",
19
+ "check": "bun run typecheck && bun run lint && bun run fmt:check && bun run test",
20
+ "deploy": "nuxt build && wrangler deploy"
21
+ },
22
+ "dependencies": {
23
+ "@humanonlyweb/cf-access": "0.1.2",
24
+ "db0": "^0.4.2",
25
+ "effect": "^4.1.7",
26
+ "nuxt": "npm:nuxt-nightly@5x",
27
+ "nuxt-auth-utils": "0.6.2",
28
+ "qrcode": "^1.5.4",
29
+ "vue": "^3.5.46",
30
+ "vue-router": "^5.5.3"
31
+ },
32
+ "devDependencies": {
33
+ "@adonisjs/hash": "9.1.1",
34
+ "@effect/tsgo": "^0.74.1",
35
+ "@types/node": "^26.6.11",
36
+ "@types/qrcode": "^1.5.6",
37
+ "miniflare": "5.20261113.0-beta",
38
+ "oxfmt": "^0.80.0",
39
+ "oxlint": "1.94.0",
40
+ "oxlint-tsgolint": "7.0.2009",
41
+ "simple-git-hooks": "2.14.0",
42
+ "typescript": "npm:typescript-native-bridge@6.0.3-bridge.18.tsgo.7.0.2",
43
+ "vitest": "^5.1.2",
44
+ "vue-tsc": "^3.3.14",
45
+ "wrangler": "^4.169.1"
46
+ },
47
+ "overrides": {
48
+ "@nuxt/kit": "npm:@nuxt/kit-nightly@5.0.0-2611241632-f471666",
49
+ "@nuxt/schema": "npm:@nuxt/schema-nightly@5.0.0-2611241632-f471666",
50
+ "typescript": "$typescript"
51
+ },
52
+ "simple-git-hooks": {
53
+ "pre-commit": "git diff --cached --name-only --diff-filter=ACMR -z | xargs -0 bunx oxfmt --check --no-error-on-unmatched-pattern || { echo 'Not formatted: run bun run fmt, then commit again.'; exit 1; }"
54
+ },
55
+ "engines": {
56
+ "node": ">=26"
57
+ },
58
+ "packageManager": "bun@1.4.5"
59
+ }
@@ -0,0 +1,123 @@
1
+ import { Hash } from '@adonisjs/hash'
2
+ import { Scrypt } from '@adonisjs/hash/drivers/scrypt'
3
+ import { Schema } from 'effect'
4
+ import { spawnSync } from 'node:child_process'
5
+ /**
6
+ * Sets an admin's password in D1 (the admin's second lock, after Cloudflare Access).
7
+ * Reads the D1 database name from root `wrangler.json` automatically.
8
+ *
9
+ * bun scripts/admin-password.ts --email you@example.com local dev database
10
+ * bun scripts/admin-password.ts --email you@example.com --remote deployed database
11
+ */
12
+ import { readFileSync } from 'node:fs'
13
+ import { resolve } from 'node:path'
14
+ import { createInterface } from 'node:readline/promises'
15
+
16
+ const args = process.argv.slice(2)
17
+ const flag = (name: string) => args.includes(`--${name}`)
18
+ const value = (name: string) => {
19
+ const at = args.indexOf(`--${name}`)
20
+ return at === -1 ? undefined : args[at + 1]
21
+ }
22
+ const email = value('email')?.trim().toLowerCase()
23
+ const MIN = 8
24
+
25
+ if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
26
+ console.error('Usage: bun scripts/admin-password.ts --email <address> [--remote]')
27
+ process.exit(1)
28
+ }
29
+
30
+ const WranglerConfig = Schema.fromJsonString(
31
+ Schema.Struct({
32
+ d1_databases: Schema.optionalKey(Schema.Array(Schema.Struct({ database_name: Schema.optionalKey(Schema.String) }))),
33
+ }),
34
+ )
35
+ const wranglerConfig = Schema.decodeSync(WranglerConfig)(readFileSync(resolve(import.meta.dirname, '..', 'wrangler.json'), 'utf8'))
36
+ const dbName = wranglerConfig.d1_databases?.[0]?.database_name
37
+ if (!dbName) {
38
+ console.error('Could not find d1_databases[0].database_name in wrangler.json.')
39
+ process.exit(1)
40
+ }
41
+
42
+ const askHidden = (question: string) =>
43
+ new Promise<string>((resolveAnswer) => {
44
+ const input = process.stdin
45
+ process.stdout.write(question)
46
+ let typed = ''
47
+ const done = (answer: string) => {
48
+ input.setRawMode(false)
49
+ input.pause()
50
+ input.off('data', onData)
51
+ process.stdout.write('\n')
52
+ resolveAnswer(answer)
53
+ }
54
+ let escaping = false
55
+ const onData = (chunk: Buffer) => {
56
+ for (const char of chunk.toString('utf8')) {
57
+ if (escaping) {
58
+ if (char === '[' || char === 'O') continue
59
+ if (char >= '@' && char <= '~') escaping = false
60
+ continue
61
+ }
62
+ if (char === '\u001b') {
63
+ escaping = true
64
+ continue
65
+ }
66
+ if (char === '\r' || char === '\n') return done(typed)
67
+ if (char === '\u0003') {
68
+ process.stdout.write('\n')
69
+ process.exit(130)
70
+ }
71
+ if (char === '\u007f' || char === '\b') typed = typed.slice(0, -1)
72
+ else if (char >= ' ') typed += char
73
+ }
74
+ }
75
+ input.setRawMode(true)
76
+ input.resume()
77
+ input.on('data', onData)
78
+ })
79
+
80
+ const ask = async (question: string) => {
81
+ const prompt = createInterface({ input: process.stdin, output: process.stdout })
82
+ const answer = await prompt.question(question)
83
+ prompt.close()
84
+ return answer
85
+ }
86
+
87
+ let password = process.env.ADMIN_PASSWORD
88
+ if (password === undefined) {
89
+ if (!process.stdin.isTTY) {
90
+ console.error('Run this in a terminal (it asks for the password), or set ADMIN_PASSWORD.')
91
+ process.exit(1)
92
+ }
93
+ password = await askHidden(`New admin password for ${email} (at least ${MIN} characters): `)
94
+ if (password.length >= MIN && (await askHidden('Type it again: ')) !== password) {
95
+ console.error('Those do not match. Nothing was changed.')
96
+ process.exit(1)
97
+ }
98
+ }
99
+ if (password.length < MIN) {
100
+ console.error(`Use at least ${MIN} characters. Nothing was changed.`)
101
+ process.exit(1)
102
+ }
103
+
104
+ if (flag('remote') && process.env.ADMIN_PASSWORD === undefined) {
105
+ const answer = await ask(`Set the admin password for ${email} on the deployed database (${dbName})? This signs them out everywhere. (y/N) `)
106
+ if (!/^y(es)?$/i.test(answer.trim())) {
107
+ console.error('Nothing was changed.')
108
+ process.exit(1)
109
+ }
110
+ }
111
+
112
+ const hash = await new Hash(new Scrypt({})).make(password)
113
+ const quote = (text: string) => `'${text.replaceAll("'", "''")}'`
114
+ const now = new Date().toISOString()
115
+ const statements = [
116
+ `INSERT INTO admins (email, password_hash, created_at) VALUES (${quote(email)}, ${quote(hash)}, ${quote(now)}) ON CONFLICT(email) DO UPDATE SET password_hash = excluded.password_hash`,
117
+ `DELETE FROM admin_sessions WHERE email = ${quote(email)}`,
118
+ ]
119
+
120
+ const result = spawnSync('bunx', ['wrangler', 'd1', 'execute', dbName, flag('remote') ? '--remote' : '--local', '--command', statements.join('; ')], {
121
+ stdio: 'inherit',
122
+ })
123
+ process.exit(result.status ?? 1)
@@ -0,0 +1,5 @@
1
+ import { EmailChange } from '#server/app/identity/email-change'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+
5
+ export default handleAuthed({}, () => UserActor.use((actor) => EmailChange.use((change) => change.cancel(actor.id))))
@@ -0,0 +1,8 @@
1
+ import { EmailChange } from '#server/app/identity/email-change'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+ import { EmailChangeRequestBody } from '#shared/utils/auth'
5
+
6
+ export default handleAuthed({ body: EmailChangeRequestBody, limit: 'email-change' }, ({ body }) =>
7
+ UserActor.use((actor) => EmailChange.use((change) => change.request(actor.id, body.email, body.password))),
8
+ )
@@ -0,0 +1,18 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Identity } from '#server/app/identity/service'
4
+ import { handleAuthed } from '#server/core/http/handle'
5
+ import { endUserSession } from '#server/core/http/session-cookie'
6
+ import { UserActor } from '#server/utils/user-actor'
7
+ import { DeleteAccountBody } from '#shared/utils/auth'
8
+
9
+ export default handleAuthed(
10
+ { body: DeleteAccountBody },
11
+ ({ body }, event) =>
12
+ Effect.gen(function* () {
13
+ const actor = yield* UserActor
14
+ yield* Identity.use((identity) => identity.deleteAccount(actor.id, body.confirm))
15
+ yield* endUserSession(event)
16
+ }),
17
+ { verification: 'allow-unverified' },
18
+ )
@@ -0,0 +1,16 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Identity } from '#server/app/identity/service'
4
+ import { handleAuthed } from '#server/core/http/handle'
5
+ import { updateUserSessionMetadata } from '#server/core/http/session-cookie'
6
+ import { UserActor } from '#server/utils/user-actor'
7
+ import { AuthUserView, UpdateProfileBody } from '#shared/utils/auth'
8
+
9
+ export default handleAuthed({ body: UpdateProfileBody, response: AuthUserView }, ({ body }, event) =>
10
+ Effect.gen(function* () {
11
+ const actor = yield* UserActor
12
+ const user = yield* Identity.use((identity) => identity.updateProfile(actor.id, body))
13
+ yield* updateUserSessionMetadata(event, user)
14
+ return user
15
+ }),
16
+ )
@@ -0,0 +1,11 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Sessions } from '#server/app/identity/sessions'
4
+ import { handleAuthed } from '#server/core/http/handle'
5
+ import { requestSessionId } from '#server/core/http/request-user'
6
+ import { UserActor } from '#server/utils/user-actor'
7
+ import { SessionsResponse } from '#shared/utils/auth'
8
+
9
+ export default handleAuthed({ response: SessionsResponse }, (_input, event) =>
10
+ UserActor.use((actor) => Sessions.use((sessions) => sessions.recent(actor.id, requestSessionId(event)))).pipe(Effect.map((sessions) => ({ sessions }))),
11
+ )
@@ -0,0 +1,5 @@
1
+ import { Totp } from '#server/app/identity/totp'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+
5
+ export default handleAuthed({}, () => UserActor.use((actor) => Totp.use((totp) => totp.cancelSetup(actor.id))))
@@ -0,0 +1,10 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Totp } from '#server/app/identity/totp'
4
+ import { handleAuthed } from '#server/core/http/handle'
5
+ import { UserActor } from '#server/utils/user-actor'
6
+ import { TotpConfirmBody, TotpConfirmResponse } from '#shared/utils/auth'
7
+
8
+ export default handleAuthed({ body: TotpConfirmBody, response: TotpConfirmResponse, limit: 'totp' }, ({ body }) =>
9
+ UserActor.use((actor) => Totp.use((totp) => totp.confirmSetup(actor.id, body.code))).pipe(Effect.map((recoveryCodes) => ({ recoveryCodes }))),
10
+ )
@@ -0,0 +1,8 @@
1
+ import { Totp } from '#server/app/identity/totp'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+ import { TotpDisableBody } from '#shared/utils/auth'
5
+
6
+ export default handleAuthed({ body: TotpDisableBody, limit: 'totp' }, ({ body }) =>
7
+ UserActor.use((actor) => Totp.use((totp) => totp.disable(actor.id, body.password, body.code))),
8
+ )
@@ -0,0 +1,6 @@
1
+ import { Totp } from '#server/app/identity/totp'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+ import { TotpSecurityView } from '#shared/utils/auth'
5
+
6
+ export default handleAuthed({ response: TotpSecurityView }, () => UserActor.use((actor) => Totp.use((totp) => totp.security(actor.id))))
@@ -0,0 +1,8 @@
1
+ import { Totp } from '#server/app/identity/totp'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+ import { TotpStartBody, TotpStartResponse } from '#shared/utils/auth'
5
+
6
+ export default handleAuthed({ body: TotpStartBody, response: TotpStartResponse, limit: 'totp' }, ({ body }) =>
7
+ UserActor.use((actor) => Totp.use((totp) => totp.startSetup(actor.id, body.password))),
8
+ )
@@ -0,0 +1,7 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { AdminActor } from '#server/app/admin/auth'
4
+ import { handleAdmin } from '#server/core/http/handle'
5
+ import { AdminMeView } from '#shared/utils/admin'
6
+
7
+ export default handleAdmin({ response: AdminMeView }, () => AdminActor.use((actor) => Effect.succeed({ email: actor.email })))
@@ -0,0 +1,12 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { AdminAuth } from '#server/app/admin/auth'
4
+ import { adminCookieOf, clearAdminCookie, handleAdmin } from '#server/core/http/handle'
5
+
6
+ export default handleAdmin({}, (_input, event) =>
7
+ Effect.gen(function* () {
8
+ const token = adminCookieOf(event)
9
+ if (token) yield* AdminAuth.use((auth) => auth.signOut(token))
10
+ clearAdminCookie(event)
11
+ }),
12
+ )
@@ -0,0 +1,18 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { AdminActor, AdminAuth } from '#server/app/admin/auth'
4
+ import { handleAdmin, setAdminCookie } from '#server/core/http/handle'
5
+ import { AdminLoginBody, AdminMeView } from '#shared/utils/admin'
6
+
7
+ export default handleAdmin(
8
+ { body: AdminLoginBody, response: AdminMeView, limit: 'sign-in' },
9
+ ({ body }, event) =>
10
+ Effect.gen(function* () {
11
+ const actor = yield* AdminActor
12
+ const auth = yield* AdminAuth
13
+ const { token } = yield* auth.signIn(actor.email, body.email, body.password)
14
+ setAdminCookie(event, token)
15
+ return { email: actor.email }
16
+ }),
17
+ { gate: 'access-only' },
18
+ )
@@ -0,0 +1,13 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { EmailChange } from '#server/app/identity/email-change'
4
+ import { handlePublic } from '#server/core/http/handle'
5
+ import { endUserSession } from '#server/core/http/session-cookie'
6
+ import { TokenBody } from '#shared/utils/auth'
7
+
8
+ export default handlePublic({ body: TokenBody }, ({ body }, event) =>
9
+ Effect.gen(function* () {
10
+ yield* EmailChange.use((change) => change.confirm(body.token))
11
+ yield* endUserSession(event)
12
+ }),
13
+ )
@@ -0,0 +1,15 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Totp } from '#server/app/identity/totp'
4
+ import { handlePublic } from '#server/core/http/handle'
5
+ import { establishUserSession } from '#server/core/http/session-cookie'
6
+ import { AuthUserView, LoginTotpBody } from '#shared/utils/auth'
7
+
8
+ export default handlePublic({ body: LoginTotpBody, response: AuthUserView, limit: 'totp' }, ({ body }, event) =>
9
+ Effect.gen(function* () {
10
+ const totp = yield* Totp
11
+ const user = yield* totp.verifyChallenge(body.challenge, body.code)
12
+ yield* establishUserSession(event, user)
13
+ return user
14
+ }),
15
+ )
@@ -0,0 +1,17 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Identity } from '#server/app/identity/service'
4
+ import { handlePublic } from '#server/core/http/handle'
5
+ import { establishUserSession } from '#server/core/http/session-cookie'
6
+ import { LoginBody, LoginResponse } from '#shared/utils/auth'
7
+
8
+ export default handlePublic({ body: LoginBody, response: LoginResponse, limit: 'sign-in' }, ({ body }, event) =>
9
+ Effect.gen(function* () {
10
+ const identity = yield* Identity
11
+ const outcome = yield* identity.login(body)
12
+ if (outcome.status === 'authenticated') {
13
+ yield* establishUserSession(event, outcome.user)
14
+ }
15
+ return outcome
16
+ }),
17
+ )
@@ -0,0 +1,17 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Sessions } from '#server/app/identity/sessions'
4
+ import { handleAuthed } from '#server/core/http/handle'
5
+ import { endUserSession } from '#server/core/http/session-cookie'
6
+ import { UserActor } from '#server/utils/user-actor'
7
+
8
+ export default handleAuthed(
9
+ {},
10
+ (_input, event) =>
11
+ Effect.gen(function* () {
12
+ const actor = yield* UserActor
13
+ yield* Sessions.use((sessions) => sessions.endAll(actor.id))
14
+ yield* endUserSession(event)
15
+ }),
16
+ { verification: 'allow-unverified' },
17
+ )
@@ -0,0 +1,14 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Sessions } from '#server/app/identity/sessions'
4
+ import { handlePublic } from '#server/core/http/handle'
5
+ import { requestSessionId } from '#server/core/http/request-user'
6
+ import { endUserSession } from '#server/core/http/session-cookie'
7
+
8
+ export default handlePublic({}, (_input, event) =>
9
+ Effect.gen(function* () {
10
+ const sessionId = requestSessionId(event)
11
+ if (sessionId) yield* Sessions.use((sessions) => sessions.end(sessionId))
12
+ yield* endUserSession(event)
13
+ }),
14
+ )
@@ -0,0 +1,18 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { Identity } from '#server/app/identity/service'
4
+ import { EmailVerification } from '#server/app/identity/verification'
5
+ import { handlePublic } from '#server/core/http/handle'
6
+ import { establishUserSession } from '#server/core/http/session-cookie'
7
+ import { AuthUserView, RegisterBody } from '#shared/utils/auth'
8
+
9
+ export default handlePublic({ body: RegisterBody, response: AuthUserView, limit: 'register', captcha: true }, ({ body }, event) =>
10
+ Effect.gen(function* () {
11
+ const identity = yield* Identity
12
+ const verification = yield* EmailVerification
13
+ const user = yield* identity.register(body)
14
+ yield* establishUserSession(event, user)
15
+ yield* verification.send(user.id).pipe(Effect.ignore)
16
+ return user
17
+ }),
18
+ )
@@ -0,0 +1,5 @@
1
+ import { PasswordReset } from '#server/app/identity/password-reset'
2
+ import { handlePublic } from '#server/core/http/handle'
3
+ import { TokenBody } from '#shared/utils/auth'
4
+
5
+ export default handlePublic({ body: TokenBody, limit: 'reset' }, ({ body }) => PasswordReset.use((reset) => reset.check(body.token)))
@@ -0,0 +1,5 @@
1
+ import { PasswordReset } from '#server/app/identity/password-reset'
2
+ import { handlePublic } from '#server/core/http/handle'
3
+ import { ResetConfirmBody } from '#shared/utils/auth'
4
+
5
+ export default handlePublic({ body: ResetConfirmBody, limit: 'reset' }, ({ body }) => PasswordReset.use((reset) => reset.complete(body.token, body.password)))
@@ -0,0 +1,5 @@
1
+ import { PasswordReset } from '#server/app/identity/password-reset'
2
+ import { handlePublic } from '#server/core/http/handle'
3
+ import { ResetRequestBody } from '#shared/utils/auth'
4
+
5
+ export default handlePublic({ body: ResetRequestBody, limit: 'reset', captcha: true }, ({ body }) => PasswordReset.use((reset) => reset.request(body.email)))
@@ -0,0 +1,17 @@
1
+ import { Effect } from 'effect'
2
+
3
+ import { EmailVerification } from '#server/app/identity/verification'
4
+ import { handlePublic } from '#server/core/http/handle'
5
+ import { requestUser } from '#server/core/http/request-user'
6
+ import { updateUserSessionMetadata } from '#server/core/http/session-cookie'
7
+ import { TokenBody } from '#shared/utils/auth'
8
+
9
+ export default handlePublic({ body: TokenBody }, ({ body }, event) =>
10
+ Effect.gen(function* () {
11
+ const { userId } = yield* EmailVerification.use((verification) => verification.confirm(body.token))
12
+ const current = requestUser(event)
13
+ if (current && current.id === userId) {
14
+ yield* updateUserSessionMetadata(event, { username: current.username, emailStatus: 'verified' })
15
+ }
16
+ }),
17
+ )
@@ -0,0 +1,7 @@
1
+ import { EmailVerification } from '#server/app/identity/verification'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+
5
+ export default handleAuthed({}, () => UserActor.use((actor) => EmailVerification.use((verification) => verification.send(actor.id))), {
6
+ verification: 'allow-unverified',
7
+ })
@@ -0,0 +1,8 @@
1
+ import { Identity } from '#server/app/identity/service'
2
+ import { handleAuthed } from '#server/core/http/handle'
3
+ import { UserActor } from '#server/utils/user-actor'
4
+ import { MeView } from '#shared/utils/auth'
5
+
6
+ export default handleAuthed({ response: MeView }, () => UserActor.use((actor) => Identity.use((identity) => identity.me(actor.id))), {
7
+ verification: 'allow-unverified',
8
+ })
@@ -0,0 +1,41 @@
1
+ import { Effect } from 'effect'
2
+ import { describe, expect, it } from 'vitest'
3
+
4
+ import { AdminAuth } from '#server/app/admin/auth'
5
+ import { Database } from '#server/core/platform/database'
6
+ import { PasswordHasher } from '#server/core/platform/password-hasher'
7
+ import { makeTestLayer } from '#server/test/kit'
8
+
9
+ describe('admin auth', () => {
10
+ it('enforces both Cloudflare Access allowlist and D1 admin password session', async () => {
11
+ const { run } = await makeTestLayer()
12
+
13
+ const hash = await run(PasswordHasher.use((h) => h.hash('super-secret')))
14
+ await run(
15
+ Database.use((db) =>
16
+ db.run(`INSERT INTO admins (email, password_hash, created_at) VALUES (?, ?, ?)`, ['admin@starter.test', hash, new Date().toISOString()]),
17
+ ),
18
+ )
19
+
20
+ const email = await run(AdminAuth.use((a) => a.gate({ email: 'admin@starter.test', sub: 'sub-1', expiresAt: Math.floor(Date.now() / 1000) + 300 })))
21
+ expect(email).toBe('admin@starter.test')
22
+
23
+ const { token } = await run(AdminAuth.use((a) => a.signIn(email, 'admin@starter.test', 'super-secret')))
24
+ const session = await run(AdminAuth.use((a) => a.session(email, token)))
25
+ expect(session.email).toBe('admin@starter.test')
26
+
27
+ await run(AdminAuth.use((a) => a.signOut(token)))
28
+ const afterOut = await run(AdminAuth.use((a) => a.session(email, token)).pipe(Effect.result))
29
+ expect(afterOut._tag).toBe('Failure')
30
+ })
31
+
32
+ it('refuses admin gate explicitly when features.admin is false', async () => {
33
+ const { run } = await makeTestLayer({ config: { features: { admin: false } } })
34
+
35
+ const gated = await run(
36
+ AdminAuth.use((a) => a.gate({ email: 'admin@starter.test', sub: 'sub-1', expiresAt: Math.floor(Date.now() / 1000) + 300 })).pipe(Effect.result),
37
+ )
38
+ expect(gated._tag).toBe('Failure')
39
+ if (gated._tag === 'Failure') expect(gated.failure._tag).toBe('ServiceNotConfigured')
40
+ })
41
+ })