Skip to content

Commit

Permalink
refactor: auth separated view from logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Aug 5, 2024
1 parent cb75eec commit 89aa092
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 208 deletions.
48 changes: 48 additions & 0 deletions apps/web/app/components/auth/ForgotForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<template>
<div>
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center">
{{ $t('auth.forgot.title') }}
</h1>
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center">
{{ $t('auth.forgot.description') }}
</p>

<UForm
ref="form"
class="space-y-4"
:state="state"
:schema="authForgotSchema"
@submit="onSubmit"
>
<UFormGroup :label="$t('auth.form.email.label')" name="email" required>
<UInput v-model="state.email" type="email" size="md" :placeholder="$t('auth.form.email.placeholder')" />
</UFormGroup>

<UButton type="submit" size="md" :loading="status === 'pending'" block>
{{ $t('auth.form.action.forgot') }}
</UButton>
</UForm>
</div>
</template>

<script setup lang="ts">
import type { Form } from '#ui/types'
import { type AuthForgotSchema, authForgotSchema } from '#schema'
const form = ref<Form<AuthForgotSchema>>()
const state = reactive<AuthForgotSchema>({
email: 'test@ta.ru',
})
const { status, execute: onSubmit } = useAPI('/api/auth/forgot', {
method: 'POST',
body: state,
immediate: false,
watch: false,
})
const { onChangeLocale } = useI18nUtils()
onChangeLocale(() => {
form.value?.clear()
})
</script>
77 changes: 77 additions & 0 deletions apps/web/app/components/auth/LoginForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<template>
<div>
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center">
{{ $t('auth.login.title') }}
</h1>
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center">
{{ $t('auth.login.description') }}
</p>

<UForm
ref="form"
class="space-y-4"
:state="state"
:schema="authLoginSchema"
@submit="onSubmit"
>
<UFormGroup :label="$t('auth.form.email.label')" name="email" required>
<UInput v-model="state.email" type="email" size="md" :placeholder="$t('auth.form.email.placeholder')" />
</UFormGroup>

<UFormGroup :label="$t('auth.form.password.label')" name="password" required>
<UInput v-model="state.password" type="password" size="md" :placeholder="$t('auth.form.password.placeholder')" />
</UFormGroup>

<div class="flex justify-end">
<ULink to="/auth/forgot" class="text-sm text-gray-700 dark:text-gray-200 hover:underline">
{{ $t('auth.links.forgot') }}
</ULink>
</div>

<UButton type="submit" size="md" :loading="status === 'pending'" block>
{{ $t('auth.form.action.login') }}
</UButton>
</UForm>

<div class="mt-4 text-sm text-gray-600 dark:text-gray-500 text-center">
<I18nT keypath="auth.links.register" scope="global">
<template #link>
<ULink to="/auth/register" class="text-primary hover:underline">
{{ $t('auth.register.title') }}
</ULink>
</template>
</I18nT>
</div>
</div>
</template>

<script setup lang="ts">
import type { Form } from '#ui/types'
import { type AuthLoginSchema, authLoginSchema } from '#schema'
const form = ref<Form<AuthLoginSchema>>()
// todo: remove credentials from state
const state = reactive<AuthLoginSchema>({
email: 'admin@example2.com',
password: 'password',
})
const { fetch: refreshSession } = useUserSession()
const { status, execute: onSubmit } = useAPI('/api/auth/login', {
method: 'POST',
body: state,
immediate: false,
watch: false,
onResponse: async ({ response }) => {
if (response.ok) {
await refreshSession()
await navigateTo('/')
}
},
})
const { onChangeLocale } = useI18nUtils()
onChangeLocale(() => {
form.value?.clear()
})
</script>
95 changes: 95 additions & 0 deletions apps/web/app/components/auth/RegisterForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<template>
<div>
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center">
{{ $t('auth.register.title') }}
</h1>
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center">
{{ $t('auth.register.description') }}
</p>

<!--
<div class="mt-4 flex flex-col lg:flex-row items-center justify-between">
<div class="w-full lg:w-1/2 mb-2 lg:mb-0">
<UButton icon="i-logos:google-icon" color="gray" variant="solid" label="Sign Up with Google" size="md" />
</div>
<div class="w-full lg:w-1/2 ml-0 lg:ml-2">
<UButton icon="i-logos:github-icon" color="gray" variant="solid" label="Sign Up with Google" size="md" />
</div>
</div>
<div class="mt-4 text-sm text-gray-500 dark:text-gray-400 text-center">
<p>or with email</p>
</div>
-->

<UForm
ref="form"
class="space-y-4"
:state="state"
:schema="authRegisterSchema"
@submit="onSubmit"
>
<UFormGroup :label="$t('auth.form.name.label')" name="name" required>
<UInput v-model="state.name" size="md" :placeholder="$t('auth.form.name.placeholder')" />
</UFormGroup>

<UFormGroup :label="$t('auth.form.email.label')" name="email" required>
<UInput v-model="state.email" type="email" size="md" :placeholder="$t('auth.form.email.placeholder')" />
</UFormGroup>

<UFormGroup :label="$t('auth.form.password.label')" name="password" required>
<UInput v-model="state.password" type="password" size="md" :placeholder="$t('auth.form.password.placeholder')" />
</UFormGroup>

<div class="flex justify-end">
<ULink to="/auth/forgot" class="text-sm text-gray-700 dark:text-gray-200 hover:underline">
{{ $t('auth.links.forgot') }}
</ULink>
</div>

<UButton type="submit" size="md" :loading="status === 'pending'" block>
{{ $t('auth.form.action.register') }}
</UButton>
</UForm>

<div class="mt-4 text-sm text-gray-600 dark:text-gray-500 text-center">
<I18nT keypath="auth.links.login" scope="global">
<template #link>
<ULink to="/auth/login" class="text-primary hover:underline">
{{ $t('auth.login.title') }}
</ULink>
</template>
</I18nT>
</div>
</div>
</template>

<script setup lang="ts">
import type { Form } from '#ui/types'
import { type AuthRegisterSchema, authRegisterSchema } from '#schema'
const form = ref<Form<AuthRegisterSchema>>()
const state = reactive<AuthRegisterSchema>({
name: '',
email: '',
password: '',
})
const { fetch: refreshSession } = useUserSession()
const { status, execute: onSubmit } = useAPI('/api/auth/register', {
method: 'POST',
body: state,
immediate: false,
watch: false,
onResponse: async ({ response }) => {
if (response.ok) {
await refreshSession()
await navigateTo('/')
}
},
})
const { onChangeLocale } = useI18nUtils()
onChangeLocale(() => {
form.value?.clear()
})
</script>
45 changes: 1 addition & 44 deletions apps/web/app/pages/auth/forgot.vue
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
<template>
<div>
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center">
{{ $t('auth.forgot.title') }}
</h1>
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center">
{{ $t('auth.forgot.description') }}
</p>

<UForm
ref="form"
class="space-y-4"
:state="state"
:schema="authForgotSchema"
@submit="onSubmit"
>
<UFormGroup :label="$t('auth.form.email.label')" name="email" required>
<UInput v-model="state.email" type="email" size="md" :placeholder="$t('auth.form.email.placeholder')" />
</UFormGroup>

<UButton type="submit" size="md" :loading="status === 'pending'" block>
{{ $t('auth.form.action.forgot') }}
</UButton>
</UForm>
</div>
<AuthForgotForm />
</template>

<script setup lang="ts">
import type { Form } from '#ui/types'
import { type AuthForgotSchema, authForgotSchema } from '#schema'
definePageMeta({
layout: 'auth',
middleware: ['guest'],
Expand All @@ -37,21 +11,4 @@ definePageMeta({
useHead({
title: () => $t('auth.forgot.title'),
})
const form = ref<Form<AuthForgotSchema>>()
const state = reactive({
email: 'test@ta.ru',
})
const { status, execute: onSubmit } = useAPI('/api/auth/forgot', {
method: 'POST',
body: state,
immediate: false,
watch: false,
})
const { onChangeLocale } = useI18nUtils()
onChangeLocale(() => {
form.value?.clear()
})
</script>
74 changes: 1 addition & 73 deletions apps/web/app/pages/auth/login.vue
Original file line number Diff line number Diff line change
@@ -1,54 +1,8 @@
<template>
<div>
<h1 class="text-3xl font-semibold mb-6 text-black dark:text-white text-center">
{{ $t('auth.login.title') }}
</h1>
<p class="text-sm mb-6 text-gray-500 dark:text-gray-400 text-center">
{{ $t('auth.login.description') }}
</p>

<UForm
ref="form"
class="space-y-4"
:state="state"
:schema="authLoginSchema"
@submit="onSubmit"
>
<UFormGroup :label="$t('auth.form.email.label')" name="email" required>
<UInput v-model="state.email" type="email" size="md" :placeholder="$t('auth.form.email.placeholder')" />
</UFormGroup>

<UFormGroup :label="$t('auth.form.password.label')" name="password" required>
<UInput v-model="state.password" type="password" size="md" :placeholder="$t('auth.form.password.placeholder')" />
</UFormGroup>

<div class="flex justify-end">
<ULink to="/auth/forgot" class="text-sm text-gray-700 dark:text-gray-200 hover:underline">
{{ $t('auth.links.forgot') }}
</ULink>
</div>

<UButton type="submit" size="md" :loading="status === 'pending'" block>
{{ $t('auth.form.action.login') }}
</UButton>
</UForm>

<div class="mt-4 text-sm text-gray-600 dark:text-gray-500 text-center">
<I18nT keypath="auth.links.register" scope="global">
<template #link>
<ULink to="/auth/register" class="text-primary hover:underline">
{{ $t('auth.register.title') }}
</ULink>
</template>
</I18nT>
</div>
</div>
<AuthLoginForm />
</template>

<script setup lang="ts">
import type { Form } from '#ui/types'
import { type AuthLoginSchema, authLoginSchema } from '#schema'
definePageMeta({
layout: 'auth',
middleware: ['guest'],
Expand All @@ -57,30 +11,4 @@ definePageMeta({
useHead({
title: () => $t('auth.login.title'),
})
const form = ref<Form<AuthLoginSchema>>()
// todo: remove credentials from state
const state = reactive({
email: 'admin@example2.com',
password: 'password',
})
const { fetch: refreshSession } = useUserSession()
const { status, execute: onSubmit } = useAPI('/api/auth/login', {
method: 'POST',
body: state,
immediate: false,
watch: false,
onResponse: async ({ response }) => {
if (response.ok) {
await refreshSession()
await navigateTo('/')
}
},
})
const { onChangeLocale } = useI18nUtils()
onChangeLocale(() => {
form.value?.clear()
})
</script>
Loading

0 comments on commit 89aa092

Please sign in to comment.