login.vue
5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent } from '@nuxt/ui'
import { resolvePostLoginPath } from '~/utils/post-login'
definePageMeta({
layout: false
})
const route = useRoute()
const toast = useToast()
const { t } = useAppI18n()
const { login } = useAuth()
const isSubmitting = ref(false)
const errorMessage = ref('')
const buildSchema = () => z.object({
username: z.string().min(1, t('login.validation.usernameRequired')),
password: z.string().min(1, t('login.validation.passwordRequired'))
})
const formSchema = computed(buildSchema)
type LoginFormSchema = z.output<ReturnType<typeof buildSchema>>
const formState = reactive<LoginFormSchema>({
username: '',
password: ''
})
const demoAccounts = computed(() => [{
username: 'admin',
password: '123456',
label: t('login.demo.admin')
}, {
username: 'operator',
password: '123456',
label: t('login.demo.operator')
}])
function fillDemoAccount(username: string, password: string) {
formState.username = username
formState.password = password
errorMessage.value = ''
}
function getRedirectPath(roles?: string[]) {
return resolvePostLoginPath(route.query.redirect, roles)
}
function resolveErrorMessage(errorCode: string | null, fallback: string) {
if (errorCode === 'VALIDATION_ERROR') {
return t('login.errors.validation')
}
if (errorCode === 'INVALID_CREDENTIALS') {
return t('login.errors.invalidCredentials')
}
return fallback
}
async function onSubmit(event: FormSubmitEvent<LoginFormSchema>) {
isSubmitting.value = true
errorMessage.value = ''
try {
const result = await login(event.data)
if (!result.success || !result.token || !result.user) {
errorMessage.value = resolveErrorMessage(result.errorCode, result.message)
return
}
toast.add({
title: t('login.toast.successTitle'),
description: t('login.toast.successDescription', { name: result.user.name }),
color: 'success',
icon: 'i-lucide-check'
})
await navigateTo(getRedirectPath(result.user.roles))
} catch {
errorMessage.value = t('login.errors.requestFailed')
} finally {
isSubmitting.value = false
}
}
</script>
<template>
<main class="min-h-screen bg-gradient-to-br from-default via-elevated/30 to-primary/10">
<div class="mx-auto grid min-h-screen max-w-6xl items-center gap-8 px-4 py-10 lg:grid-cols-2 lg:px-8">
<section class="space-y-5">
<UBadge color="primary" variant="soft" class="rounded-full">
{{ t('login.badge') }}
</UBadge>
<h1 class="text-3xl font-semibold text-highlighted sm:text-4xl">
{{ t('login.title') }}
</h1>
<p class="text-toned text-base leading-7">
{{ t('login.description') }}
</p>
<div class="space-y-3">
<p class="text-sm font-medium text-muted">
{{ t('login.demo.title') }}
</p>
<div class="flex flex-wrap gap-2">
<UButton
v-for="account in demoAccounts"
:key="account.username"
color="neutral"
variant="soft"
size="sm"
@click="fillDemoAccount(account.username, account.password)"
>
{{ account.label }}
</UButton>
</div>
</div>
</section>
<section>
<UCard class="w-full shadow-sm ring-1 ring-default">
<template #header>
<div class="space-y-1">
<h2 class="text-xl font-semibold text-highlighted">
{{ t('login.form.title') }}
</h2>
<p class="text-sm text-muted">
{{ t('login.form.description') }}
</p>
</div>
</template>
<UForm
:schema="formSchema"
:state="formState"
class="space-y-5"
@submit="onSubmit"
>
<UFormField name="username" :label="t('login.form.username')" required>
<UInput
v-model="formState.username"
size="xl"
class="w-full"
:placeholder="t('login.form.usernamePlaceholder')"
autocomplete="username"
/>
</UFormField>
<UFormField name="password" :label="t('login.form.password')" required>
<UInput
v-model="formState.password"
size="xl"
class="w-full"
:placeholder="t('login.form.passwordPlaceholder')"
type="password"
autocomplete="current-password"
/>
</UFormField>
<UAlert
v-if="errorMessage"
color="error"
variant="soft"
icon="i-lucide-circle-alert"
:title="t('login.errors.title')"
:description="errorMessage"
/>
<UButton
type="submit"
color="primary"
block
size="xl"
:loading="isSubmitting"
:label="t('login.form.submit')"
/>
</UForm>
</UCard>
</section>
</div>
</main>
</template>