@humanonlyweb/create-nuxtflare 0.0.0-stage → 0.1.1

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 +340 -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 +46 -4
@@ -0,0 +1,36 @@
1
+ <script setup lang="ts">
2
+ import QRCode from 'qrcode'
3
+
4
+ const { value, label } = defineProps<{ value: string; label?: string }>()
5
+
6
+ const code = computed(() => {
7
+ const qr = QRCode.create(value, { errorCorrectionLevel: 'M' })
8
+ const size = qr.modules.size
9
+ const path: string[] = []
10
+ for (let y = 0; y < size; y++) {
11
+ let x = 0
12
+ while (x < size) {
13
+ if (qr.modules.get(y, x)) {
14
+ const start = x
15
+ while (x < size && qr.modules.get(y, x)) x++
16
+ path.push(`M${start} ${y}h${x - start}v1h-${x - start}z`)
17
+ } else x++
18
+ }
19
+ }
20
+ return { size, d: path.join('') }
21
+ })
22
+ </script>
23
+
24
+ <template>
25
+ <svg class="qr" :viewBox="`0 0 ${code.size} ${code.size}`" shape-rendering="crispEdges" role="img" :aria-label="label ?? `QR code for ${value}`">
26
+ <path :d="code.d" fill="currentColor" />
27
+ </svg>
28
+ </template>
29
+
30
+ <style scoped>
31
+ .qr {
32
+ display: block;
33
+ width: 100%;
34
+ height: auto;
35
+ }
36
+ </style>
@@ -0,0 +1,151 @@
1
+ <script setup lang="ts">
2
+ import type { TotpSecurityView } from '#shared/utils/auth'
3
+
4
+ import { problemOf } from '~/utils/api-error'
5
+
6
+ type UiStep =
7
+ | { readonly kind: 'idle' }
8
+ | { readonly kind: 'enrolling'; readonly secret: string; readonly uri: string }
9
+ | { readonly kind: 'recovery'; readonly codes: ReadonlyArray<string> }
10
+
11
+ const features = useFeatures()
12
+ const { data: security, refresh } = await useFetch<TotpSecurityView | null>('/api/account/totp', {
13
+ immediate: features.totp,
14
+ default: () => null,
15
+ })
16
+
17
+ const step = ref<UiStep>({ kind: 'idle' })
18
+ const password = ref('')
19
+ const code = ref('')
20
+ const busy = ref(false)
21
+ const errorMessage = ref('')
22
+
23
+ const start = async () => {
24
+ errorMessage.value = ''
25
+ busy.value = true
26
+ try {
27
+ const setup = await $fetch('/api/account/totp/start', { method: 'POST', body: { password: password.value } })
28
+ password.value = ''
29
+ step.value = { kind: 'enrolling', secret: setup.secret, uri: setup.uri }
30
+ await refresh()
31
+ } catch (error) {
32
+ errorMessage.value = problemOf(error).message
33
+ } finally {
34
+ busy.value = false
35
+ }
36
+ }
37
+
38
+ const confirm = async () => {
39
+ errorMessage.value = ''
40
+ busy.value = true
41
+ try {
42
+ const result = await $fetch('/api/account/totp/confirm', { method: 'POST', body: { code: code.value } })
43
+ code.value = ''
44
+ step.value = { kind: 'recovery', codes: result.recoveryCodes }
45
+ await refresh()
46
+ } catch (error) {
47
+ errorMessage.value = problemOf(error).message
48
+ } finally {
49
+ busy.value = false
50
+ }
51
+ }
52
+
53
+ const cancel = async () => {
54
+ errorMessage.value = ''
55
+ busy.value = true
56
+ try {
57
+ await $fetch('/api/account/totp/cancel', { method: 'POST' })
58
+ code.value = ''
59
+ step.value = { kind: 'idle' }
60
+ await refresh()
61
+ } catch (error) {
62
+ errorMessage.value = problemOf(error).message
63
+ } finally {
64
+ busy.value = false
65
+ }
66
+ }
67
+
68
+ const disable = async () => {
69
+ errorMessage.value = ''
70
+ busy.value = true
71
+ try {
72
+ await $fetch('/api/account/totp/disable', { method: 'POST', body: { password: password.value, code: code.value } })
73
+ password.value = ''
74
+ code.value = ''
75
+ step.value = { kind: 'idle' }
76
+ await refresh()
77
+ } catch (error) {
78
+ errorMessage.value = problemOf(error).message
79
+ } finally {
80
+ busy.value = false
81
+ }
82
+ }
83
+ </script>
84
+
85
+ <template>
86
+ <section v-if="features.totp && security" class="totp">
87
+ <h2>Two-factor authentication</h2>
88
+ <p v-if="errorMessage" role="alert">{{ errorMessage }}</p>
89
+
90
+ <div v-if="step.kind === 'recovery'">
91
+ <p>Save these recovery codes somewhere safe. Each code can only be used once and will not be shown again.</p>
92
+ <ul>
93
+ <li v-for="item in step.codes" :key="item">
94
+ <code>{{ item }}</code>
95
+ </li>
96
+ </ul>
97
+ <button type="button" @click="step = { kind: 'idle' }">Done</button>
98
+ </div>
99
+
100
+ <div v-else-if="step.kind === 'enrolling'">
101
+ <p>Scan this QR code with your authenticator app, or enter the key manually:</p>
102
+ <div class="totp__qr">
103
+ <QrCode :value="step.uri" label="Authenticator setup QR code" />
104
+ </div>
105
+ <p>
106
+ <code>{{ step.secret }}</code>
107
+ </p>
108
+ <form @submit.prevent="confirm">
109
+ <label>
110
+ 6-digit code
111
+ <input v-model="code" inputmode="numeric" autocomplete="one-time-code" required />
112
+ </label>
113
+ <button type="submit" :disabled="busy">Confirm 2FA</button>
114
+ <button type="button" :disabled="busy" @click="cancel">Cancel</button>
115
+ </form>
116
+ </div>
117
+
118
+ <div v-else-if="security.status === 'enabled'">
119
+ <p>Two-factor authentication is enabled ({{ security.recoveryCodesLeft }} recovery codes left).</p>
120
+ <form @submit.prevent="disable">
121
+ <label>
122
+ Current password
123
+ <input v-model="password" type="password" autocomplete="current-password" required />
124
+ </label>
125
+ <label>
126
+ Authenticator or recovery code
127
+ <input v-model="code" autocomplete="one-time-code" required />
128
+ </label>
129
+ <button type="submit" :disabled="busy">Disable 2FA</button>
130
+ </form>
131
+ </div>
132
+
133
+ <form v-else @submit.prevent="start">
134
+ <p>Protect your account with an authenticator app.</p>
135
+ <label>
136
+ Current password
137
+ <input v-model="password" type="password" autocomplete="current-password" required />
138
+ </label>
139
+ <button type="submit" :disabled="busy">Set up 2FA</button>
140
+ </form>
141
+ </section>
142
+ </template>
143
+
144
+ <style scoped>
145
+ .totp__qr {
146
+ width: 200px;
147
+ padding: 12px;
148
+ background: #fff;
149
+ color: #000;
150
+ }
151
+ </style>
@@ -0,0 +1 @@
1
+ export const useFeatures = () => useRuntimeConfig().public.features
@@ -0,0 +1,10 @@
1
+ import type { MeView } from '#shared/utils/auth'
2
+
3
+ export const useMe = () => {
4
+ const { loggedIn } = useUserSession()
5
+ return useFetch<MeView | null>('/api/me', {
6
+ key: 'me',
7
+ immediate: loggedIn.value,
8
+ default: () => null,
9
+ })
10
+ }
@@ -0,0 +1,12 @@
1
+ export default defineNuxtRouteMiddleware(async (to) => {
2
+ if (!to.path.startsWith('/admin') || to.path === '/admin/login') return
3
+ const features = useFeatures()
4
+ if (!features.admin) return navigateTo('/')
5
+ if (import.meta.server) return
6
+
7
+ try {
8
+ await $fetch('/api/admin/me')
9
+ } catch {
10
+ return navigateTo('/admin/login')
11
+ }
12
+ })
@@ -0,0 +1,6 @@
1
+ export default defineNuxtRouteMiddleware((to) => {
2
+ const { loggedIn } = useUserSession()
3
+ if (!loggedIn.value) {
4
+ return navigateTo({ path: '/login', query: { next: to.fullPath } })
5
+ }
6
+ })
@@ -0,0 +1,4 @@
1
+ export default defineNuxtRouteMiddleware(() => {
2
+ const { loggedIn } = useUserSession()
3
+ if (loggedIn.value) return navigateTo('/account')
4
+ })
@@ -0,0 +1,22 @@
1
+ <script setup lang="ts">
2
+ definePageMeta({ middleware: 'auth' })
3
+
4
+ const { data: me } = await useMe()
5
+ const features = useFeatures()
6
+ const { clear } = useUserSession()
7
+
8
+ const signOut = async () => {
9
+ await $fetch('/api/auth/logout', { method: 'POST' })
10
+ await clear()
11
+ await navigateTo('/login')
12
+ }
13
+ </script>
14
+
15
+ <template>
16
+ <main v-if="me">
17
+ <h1>Account</h1>
18
+ <p>{{ me.username }} ({{ me.email }}) — email {{ me.emailStatus }}</p>
19
+ <button type="button" @click="signOut">Sign out</button>
20
+ <TotpSetup v-if="features.totp && (!features.emailVerificationRequired || me.emailStatus === 'verified')" />
21
+ </main>
22
+ </template>
@@ -0,0 +1,16 @@
1
+ <script setup lang="ts">
2
+ const { data: admin } = await useFetch('/api/admin/me')
3
+
4
+ const signOut = async () => {
5
+ await $fetch('/api/admin/session', { method: 'DELETE' })
6
+ await navigateTo('/admin/login')
7
+ }
8
+ </script>
9
+
10
+ <template>
11
+ <main v-if="admin">
12
+ <h1>Admin</h1>
13
+ <p>Signed in as {{ admin.email }}</p>
14
+ <button type="button" @click="signOut">Sign out</button>
15
+ </main>
16
+ </template>
@@ -0,0 +1,39 @@
1
+ <script setup lang="ts">
2
+ import { problemOf } from '~/utils/api-error'
3
+
4
+ const email = ref('')
5
+ const password = ref('')
6
+ const busy = ref(false)
7
+ const errorMessage = ref('')
8
+
9
+ const submit = async () => {
10
+ errorMessage.value = ''
11
+ busy.value = true
12
+ try {
13
+ await $fetch('/api/admin/session', { method: 'POST', body: { email: email.value, password: password.value } })
14
+ await navigateTo('/admin')
15
+ } catch (error) {
16
+ errorMessage.value = problemOf(error).message
17
+ } finally {
18
+ busy.value = false
19
+ }
20
+ }
21
+ </script>
22
+
23
+ <template>
24
+ <main>
25
+ <h1>Admin sign in</h1>
26
+ <p v-if="errorMessage" role="alert">{{ errorMessage }}</p>
27
+ <form @submit.prevent="submit">
28
+ <label>
29
+ Email
30
+ <input v-model="email" type="email" autocomplete="username" required />
31
+ </label>
32
+ <label>
33
+ Password
34
+ <input v-model="password" type="password" autocomplete="current-password" required />
35
+ </label>
36
+ <button type="submit" :disabled="busy">Sign in</button>
37
+ </form>
38
+ </main>
39
+ </template>
@@ -0,0 +1,18 @@
1
+ <script setup lang="ts">
2
+ const { loggedIn } = useUserSession()
3
+ const features = useFeatures()
4
+ </script>
5
+
6
+ <template>
7
+ <main>
8
+ <h1>Nuxtflare</h1>
9
+ <nav>
10
+ <NuxtLink v-if="loggedIn" to="/account">Account</NuxtLink>
11
+ <template v-else>
12
+ <NuxtLink to="/login">Sign in</NuxtLink>
13
+ <NuxtLink to="/register">Create account</NuxtLink>
14
+ </template>
15
+ <NuxtLink v-if="features.admin" to="/admin">Admin</NuxtLink>
16
+ </nav>
17
+ </main>
18
+ </template>
@@ -0,0 +1,110 @@
1
+ <script setup lang="ts">
2
+ import { isLocalPath } from '#shared/utils/local-path'
3
+ import { problemOf } from '~/utils/api-error'
4
+
5
+ definePageMeta({ middleware: 'guest' })
6
+
7
+ type LoginStep = { readonly kind: 'credentials' } | { readonly kind: 'totp'; readonly challenge: string }
8
+
9
+ const route = useRoute()
10
+ const features = useFeatures()
11
+ const { fetch: refreshSession } = useUserSession()
12
+
13
+ const step = ref<LoginStep>({ kind: 'credentials' })
14
+ const identity = ref('')
15
+ const password = ref('')
16
+ const code = ref('')
17
+ const busy = ref(false)
18
+ const errorMessage = ref('')
19
+
20
+ onMounted(() => {
21
+ if (location.hash.startsWith('#totp=')) {
22
+ const challenge = decodeURIComponent(location.hash.slice('#totp='.length))
23
+ if (challenge) {
24
+ step.value = { kind: 'totp', challenge }
25
+ history.replaceState(null, '', location.pathname + location.search)
26
+ }
27
+ }
28
+ })
29
+
30
+ const destination = () => {
31
+ const next = typeof route.query.next === 'string' ? route.query.next : ''
32
+ return isLocalPath(next) ? next : '/account'
33
+ }
34
+
35
+ const submitCredentials = async () => {
36
+ errorMessage.value = ''
37
+ busy.value = true
38
+ try {
39
+ const result = await $fetch('/api/auth/login', {
40
+ method: 'POST',
41
+ body: { identity: identity.value, password: password.value },
42
+ })
43
+ if (result.status === 'totp_required') {
44
+ password.value = ''
45
+ step.value = { kind: 'totp', challenge: result.challenge }
46
+ return
47
+ }
48
+ await refreshSession()
49
+ await navigateTo(destination())
50
+ } catch (error) {
51
+ errorMessage.value = problemOf(error).message
52
+ } finally {
53
+ busy.value = false
54
+ }
55
+ }
56
+
57
+ const submitTotp = async () => {
58
+ if (step.value.kind !== 'totp') return
59
+ errorMessage.value = ''
60
+ busy.value = true
61
+ try {
62
+ await $fetch('/api/auth/login/totp', {
63
+ method: 'POST',
64
+ body: { challenge: step.value.challenge, code: code.value },
65
+ })
66
+ await refreshSession()
67
+ await navigateTo(destination())
68
+ } catch (error) {
69
+ errorMessage.value = problemOf(error).message
70
+ } finally {
71
+ busy.value = false
72
+ }
73
+ }
74
+ </script>
75
+
76
+ <template>
77
+ <main>
78
+ <h1>Sign in</h1>
79
+ <p v-if="errorMessage" role="alert">{{ errorMessage }}</p>
80
+
81
+ <form v-if="step.kind === 'credentials'" @submit.prevent="submitCredentials">
82
+ <label>
83
+ Email or username
84
+ <input v-model="identity" autocomplete="username" required />
85
+ </label>
86
+ <label>
87
+ Password
88
+ <input v-model="password" type="password" autocomplete="current-password" required />
89
+ </label>
90
+ <button type="submit" :disabled="busy">Sign in</button>
91
+ </form>
92
+
93
+ <form v-else @submit.prevent="submitTotp">
94
+ <p>Enter the 6-digit code from your authenticator app or a recovery code.</p>
95
+ <label>
96
+ Verification code
97
+ <input v-model="code" autocomplete="one-time-code" required />
98
+ </label>
99
+ <button type="submit" :disabled="busy">Verify</button>
100
+ <button type="button" :disabled="busy" @click="step = { kind: 'credentials' }">Back</button>
101
+ </form>
102
+
103
+ <p v-if="features.googleOAuth && step.kind === 'credentials'">
104
+ <a href="/auth/google">Continue with Google</a>
105
+ </p>
106
+ <p v-if="features.githubOAuth && step.kind === 'credentials'">
107
+ <a href="/auth/github">Continue with GitHub</a>
108
+ </p>
109
+ </main>
110
+ </template>
@@ -0,0 +1,51 @@
1
+ <script setup lang="ts">
2
+ import { problemOf } from '~/utils/api-error'
3
+
4
+ definePageMeta({ middleware: 'guest' })
5
+
6
+ const { fetch: refreshSession } = useUserSession()
7
+ const username = ref('')
8
+ const email = ref('')
9
+ const password = ref('')
10
+ const busy = ref(false)
11
+ const errorMessage = ref('')
12
+
13
+ const submit = async () => {
14
+ errorMessage.value = ''
15
+ busy.value = true
16
+ try {
17
+ await $fetch('/api/auth/register', {
18
+ method: 'POST',
19
+ body: { username: username.value, email: email.value, password: password.value },
20
+ })
21
+ await refreshSession()
22
+ await navigateTo('/account')
23
+ } catch (error) {
24
+ errorMessage.value = problemOf(error).message
25
+ } finally {
26
+ busy.value = false
27
+ }
28
+ }
29
+ </script>
30
+
31
+ <template>
32
+ <main>
33
+ <h1>Create account</h1>
34
+ <p v-if="errorMessage" role="alert">{{ errorMessage }}</p>
35
+ <form @submit.prevent="submit">
36
+ <label>
37
+ Username
38
+ <input v-model="username" autocomplete="username" required />
39
+ </label>
40
+ <label>
41
+ Email
42
+ <input v-model="email" type="email" autocomplete="email" required />
43
+ </label>
44
+ <label>
45
+ Password
46
+ <input v-model="password" type="password" autocomplete="new-password" required />
47
+ </label>
48
+ <button type="submit" :disabled="busy">Create account</button>
49
+ </form>
50
+ </main>
51
+ </template>
@@ -0,0 +1,50 @@
1
+ import { asFinite, asString, readKey } from '#shared/utils/unknown'
2
+
3
+ export interface ApiProblem {
4
+ readonly status: number
5
+ readonly tag: string | undefined
6
+ readonly message: string
7
+ }
8
+
9
+ const readFailure = (error: unknown) => {
10
+ const body = readKey(error, 'data')
11
+ const detail = readKey(body, 'data')
12
+ const issues = readKey(detail, 'issues')
13
+ return {
14
+ status: asFinite(readKey(error, 'status')) ?? 0,
15
+ message: asString(readKey(body, 'message')),
16
+ tag: asString(readKey(detail, '_tag')),
17
+ field: asString(readKey(detail, 'field')),
18
+ fieldMessage: asString(readKey(detail, 'message')),
19
+ issues: Array.isArray(issues) ? issues : [],
20
+ }
21
+ }
22
+
23
+ const FALLBACK = 'Something went wrong. Please try again.'
24
+
25
+ export const problemOf = (error: unknown): ApiProblem => {
26
+ const failure = readFailure(error)
27
+ const { status } = failure
28
+ if (status === 0) return { status, tag: undefined, message: 'Could not connect. Check your connection and try again.' }
29
+ if (status === 429) return { status, tag: 'RateLimited', message: failure.message ?? 'Too many attempts. Please wait a minute and try again.' }
30
+ if (status >= 500) return { status, tag: failure.tag, message: FALLBACK }
31
+ return { status, tag: failure.tag, message: failure.message ?? FALLBACK }
32
+ }
33
+
34
+ export const fieldErrors =
35
+ (rename: Readonly<Record<string, string>> = {}) =>
36
+ (error: unknown): Record<string, string> | undefined => {
37
+ const failure = readFailure(error)
38
+ const mapped: Record<string, string> = {}
39
+ if (failure.tag === 'Conflict' && failure.field && failure.fieldMessage) mapped[rename[failure.field] ?? failure.field] = failure.fieldMessage
40
+ for (const issue of failure.issues) {
41
+ const message = asString(readKey(issue, 'message'))
42
+ const parts: unknown = readKey(issue, 'path')
43
+ const path = (Array.isArray(parts) ? parts : [])
44
+ .map((part: unknown) => String(typeof part === 'object' && part !== null ? readKey(part, 'key') : part))
45
+ .join('.')
46
+ const field = rename[path] ?? path
47
+ if (field && message && !mapped[field]) mapped[field] = message
48
+ }
49
+ return Object.keys(mapped).length ? mapped : undefined
50
+ }
@@ -0,0 +1,115 @@
1
+ import { flag, int, required, secret, text } from './server/core/platform/runtime-config-schema.ts'
2
+
3
+ /**
4
+ * Single source of truth for app feature toggles.
5
+ * Exposed through `runtimeConfig.public.features` so server services and client UI never drift.
6
+ * Any flag can also be overridden per environment via `NUXT_PUBLIC_FEATURES_<NAME>=true|false`.
7
+ */
8
+ const features = {
9
+ totp: true,
10
+ googleOAuth: false,
11
+ githubOAuth: false,
12
+ emailVerificationRequired: true,
13
+ admin: true,
14
+ } as const
15
+
16
+ export default defineNuxtConfig({
17
+ devtools: { enabled: true },
18
+ modules: ['nuxt-auth-utils', '@humanonlyweb/cf-access/nuxt'],
19
+
20
+ cfAccess: { protect: ['/admin/**', '/api/admin/**'] },
21
+ compatibilityDate: 'latest',
22
+
23
+ app: {
24
+ head: {
25
+ htmlAttrs: { lang: 'en' },
26
+ title: 'Nuxtflare App',
27
+ meta: [{ name: 'viewport', content: 'width=device-width, initial-scale=1' }],
28
+ },
29
+ },
30
+
31
+ experimental: {
32
+ typescriptPlugin: true,
33
+ strictRouteTypes: true,
34
+ typedPages: true,
35
+ // Client and server build sequentially to avoid intermittent vite:css-post asset resolution races in CI.
36
+ parallelBuild: false,
37
+ },
38
+
39
+ routeRules: {
40
+ '/api/**': { headers: { 'cache-control': 'no-store', 'content-security-policy': "default-src 'none'; frame-ancestors 'none'" } },
41
+ '/account': { headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store' } },
42
+ '/verify/**': { headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' } },
43
+ '/reset/**': { headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' } },
44
+ '/email/**': { headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' } },
45
+ '/admin': { ssr: false, headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store' } },
46
+ '/admin/**': { ssr: false, headers: { 'x-robots-tag': 'noindex', 'cache-control': 'no-store' } },
47
+ },
48
+
49
+ nitro: {
50
+ preset: 'cloudflare_module',
51
+ experimental: { database: true, tasks: true, envExpansion: true },
52
+ database: {
53
+ default: { connector: 'cloudflare-d1', options: { bindingName: 'DB' } },
54
+ },
55
+ $test: {
56
+ database: {
57
+ default: { connector: 'sqlite', options: { name: ':memory:' } },
58
+ },
59
+ },
60
+ kv: {
61
+ cache: { driver: 'cloudflare-kv-binding', binding: 'KV' },
62
+ },
63
+ },
64
+
65
+ vite: {
66
+ optimizeDeps: { include: ['effect'] },
67
+ },
68
+
69
+ $production: {
70
+ runtimeConfig: { appUrl: 'https://example.com' },
71
+ },
72
+
73
+ $development: {
74
+ runtimeConfig: {
75
+ appUrl: `http://localhost:${process.env.PORT ?? process.env.NUXT_PORT ?? 3000}`,
76
+ adminEmails: 'admin@example.test',
77
+ },
78
+ },
79
+
80
+ runtimeConfig: {
81
+ appName: text('Nuxtflare App', 'Display name used in emails and authenticator app labels.'),
82
+ appUrl: text('', 'Public origin of the site, for links in emails and redirects; set per environment or via NUXT_APP_URL.'),
83
+ mailFrom: text('Nuxtflare App <no-reply@example.com>', 'Sender address for transactional emails.'),
84
+ piiKey: required('64-hex AES-256-GCM key for field-level encryption (TOTP secrets, personal data). Generate with `openssl rand -hex 32`.'),
85
+ piiKeyPrevious: secret('Comma-separated older PII keys kept during key rotation.'),
86
+ piiIndexKey: required('64-hex HMAC key for blind indexes. Generate with `openssl rand -hex 32`.'),
87
+ turnstileSecret: secret('Cloudflare Turnstile secret key.'),
88
+ turnstileRequired: flag(false, 'Fail closed when Turnstile secret is unset instead of skipping verification.'),
89
+ rateLimitScale: int(1, 'Multiplies strict Durable Object rate limits; raise only for local work or load tests.'),
90
+ adminEmails: text('', 'Comma-separated allowlist of Cloudflare Access emails permitted to reach /admin.'),
91
+ oauth: {
92
+ google: { clientId: '', clientSecret: '' },
93
+ github: { clientId: '', clientSecret: '' },
94
+ },
95
+ session: { maxAge: 60 * 60 * 24 * 30, name: 'app-session' },
96
+ public: {
97
+ turnstileSiteKey: text('', 'Cloudflare Turnstile site key; when empty, client forms skip the widget.'),
98
+ features: {
99
+ totp: flag(features.totp, 'Enable TOTP two-factor authentication (authenticator app + QR code + recovery codes).'),
100
+ googleOAuth: flag(features.googleOAuth, 'Enable Sign in with Google.'),
101
+ githubOAuth: flag(features.githubOAuth, 'Enable Sign in with GitHub.'),
102
+ emailVerificationRequired: flag(features.emailVerificationRequired, 'Require confirmed email before calling authenticated API routes.'),
103
+ admin: flag(features.admin, 'Enable two-lock admin panel (Cloudflare Access + admin password).'),
104
+ },
105
+ },
106
+ },
107
+
108
+ typescript: {
109
+ serverTsConfig: {
110
+ compilerOptions: {
111
+ types: ['../worker-configuration.d.ts'],
112
+ },
113
+ },
114
+ },
115
+ })