game-village-politics-frontend/app/pages/register.vue

76 lines
4.1 KiB
Vue

<script setup lang="ts">
import type { GameError } from '~/types/game'
const { register } = useAuth()
const errorText = useErrorText()
const { t } = useI18n()
const mock = isMockServer
const form = reactive({ name: '', email: '', password: '', again: '', terms: false })
/** The field the server (or the form) refused, and why. */
const problem = ref<{ field: string | null; text: string } | null>(null)
const busy = ref(false)
async function submit() {
if (form.password !== form.again) {
problem.value = { field: 'again', text: t('auth.password_mismatch') }
return
}
busy.value = true
problem.value = null
try {
await register(form.name, form.email, form.password, form.terms)
await navigateTo('/worlds')
} catch (e) {
problem.value = { field: (e as GameError)?.field ?? null, text: errorText(e) }
} finally {
busy.value = false
}
}
const wrong = (field: string) => problem.value?.field === field
</script>
<template>
<div>
<UiAuthCard :title="$t('auth.register.title')">
<p v-if="mock" class="mb-4 border border-gold-600 bg-parchment-200 px-3 py-2 text-xs text-wood-900">{{ $t('auth.mock_hint') }}</p>
<form class="space-y-4" novalidate @submit.prevent="submit">
<label class="block">
<span class="label">{{ $t('auth.name') }}</span>
<input v-model="form.name" class="input" :class="wrong('name') ? 'border-blood-700' : ''" type="text" autocomplete="nickname" maxlength="24" required :aria-invalid="wrong('name')" />
<span class="mt-1 block text-xs" :class="wrong('name') ? 'font-semibold text-blood-700' : 'text-ink-500'">{{ wrong('name') ? problem!.text : $t('auth.name_hint') }}</span>
</label>
<label class="block">
<span class="label">{{ $t('auth.email') }}</span>
<input v-model="form.email" class="input" :class="wrong('email') ? 'border-blood-700' : ''" type="email" autocomplete="email" required :aria-invalid="wrong('email')" />
<span v-if="wrong('email')" class="mt-1 block text-xs font-semibold text-blood-700">{{ problem!.text }}</span>
</label>
<label class="block">
<span class="label">{{ $t('auth.password') }}</span>
<input v-model="form.password" class="input" :class="wrong('password') ? 'border-blood-700' : ''" type="password" autocomplete="new-password" required :aria-invalid="wrong('password')" />
<span class="mt-1 block text-xs" :class="wrong('password') ? 'font-semibold text-blood-700' : 'text-ink-500'">{{ wrong('password') ? problem!.text : $t('auth.password_hint') }}</span>
</label>
<label class="block">
<span class="label">{{ $t('auth.password_again') }}</span>
<input v-model="form.again" class="input" :class="wrong('again') ? 'border-blood-700' : ''" type="password" autocomplete="new-password" required :aria-invalid="wrong('again')" />
<span v-if="wrong('again')" class="mt-1 block text-xs font-semibold text-blood-700">{{ problem!.text }}</span>
</label>
<label class="flex items-start gap-2 text-sm">
<input v-model="form.terms" type="checkbox" class="mt-1" :aria-invalid="wrong('terms')" />
<i18n-t keypath="auth.terms" tag="span" :class="wrong('terms') ? 'font-semibold text-blood-700' : ''">
<template #terms><NuxtLink to="/terms" target="_blank" class="font-semibold text-blood-700 underline">{{ $t('auth.terms_link') }}</NuxtLink></template>
<template #privacy><NuxtLink to="/privacy" target="_blank" class="font-semibold text-blood-700 underline">{{ $t('auth.privacy_link') }}</NuxtLink></template>
</i18n-t>
</label>
<p v-if="problem && !problem.field" class="alert" role="alert">{{ problem.text }}</p>
<p v-else-if="problem?.field === 'terms'" class="alert" role="alert">{{ problem.text }}</p>
<button type="submit" class="btn btn-primary w-full py-2" :disabled="busy">{{ $t('auth.register.submit') }}</button>
</form>
<template #footer>
<NuxtLink to="/login" class="font-semibold text-blood-700 hover:underline">{{ $t('auth.register.to_login') }}</NuxtLink>
</template>
</UiAuthCard>
<GameFooter />
</div>
</template>