|
1 | 1 | <script lang="ts" setup>
|
2 |
| -import { passwordPattern } from '~~/shared/types'; |
3 |
| -import { z } from 'zod'; |
4 |
| -import useAuth from '~/composables/useAuth'; |
| 2 | +import { postLoginBody } from '#shared/schemas'; |
| 3 | +import type { UserCredentials } from '~~/shared/types'; |
5 | 4 |
|
6 |
| -const { loginUser, logOut } = useAuth(); |
7 |
| -const profileStore = useProfileStore(); |
8 |
| -
|
9 |
| -const userCredentials = reactive({ |
| 5 | +const userCredentials = reactive<UserCredentials>({ |
10 | 6 | email: '',
|
11 | 7 | password: ''
|
12 | 8 | });
|
13 |
| -const loginSchema = z.object({ |
14 |
| - email: z |
15 |
| - .string({ message: 'Email is required' }) |
16 |
| - .email('Invalid email address'), |
17 |
| - password: z |
18 |
| - .string({ message: 'Password is required' }) |
19 |
| - .regex( |
20 |
| - passwordPattern, |
21 |
| - 'Password must contain at least 8 characters with one lowercase letter, one uppercase letter and one number' |
22 |
| - ) |
23 |
| -}); |
24 |
| -const errors = ref({ |
25 |
| - emailError: [] as string[], |
26 |
| - passwordError: [] as string[] |
27 |
| -}); |
28 |
| -const handleLogin = async () => { |
29 |
| - const validateSchema = async () => { |
30 |
| - const validatedSchema = await loginSchema.safeParseAsync(userCredentials); |
31 |
| - if (!validatedSchema.success) { |
32 |
| - const error = validatedSchema.error.format(); |
33 |
| - errors.value.emailError = error.email?._errors ?? []; |
34 |
| - errors.value.passwordError = error.password?._errors ?? []; |
35 |
| - return; |
36 |
| - } |
37 |
| - return validatedSchema.data; |
38 |
| - }; |
39 | 9 |
|
40 |
| - try { |
41 |
| - const data = validateSchema(); |
42 |
| - if (!data) return; |
| 10 | +const handleLogin = async () => { |
| 11 | + // UForm already validates the form |
| 12 | + const { loginUser } = useAuth(); |
| 13 | + const loginResult = await loginUser(userCredentials); |
43 | 14 |
|
44 |
| - const response = await loginUser(userCredentials); |
45 |
| - response.fold( |
46 |
| - userState => { |
47 |
| - console.log('We here =)'); |
48 |
| - profileStore.setProfile(userState); |
49 |
| - navigateTo('/'); |
50 |
| - }, |
51 |
| - error => { |
52 |
| - console.error(error); |
53 |
| - } |
54 |
| - ); |
55 |
| - } catch (error) { |
56 |
| - console.log('we here =('); |
57 |
| - console.log(error); |
58 |
| - } |
59 |
| - // handle form submission |
| 15 | + loginResult.fold( |
| 16 | + () => navigateTo('/'), |
| 17 | + error => { |
| 18 | + alert(`Server error: ${error.message}`); |
| 19 | + } |
| 20 | + ); |
60 | 21 | };
|
61 | 22 | </script>
|
62 | 23 |
|
63 | 24 | <template>
|
64 | 25 | <h2 class="font-semibold text-5xl text-center">Login Screen</h2>
|
65 | 26 | <UContainer class="flex flex-col justify-center items-center">
|
66 |
| - <UForm :schema="loginSchema" :state="userCredentials" @submit="handleLogin"> |
67 |
| - <UFormGroup label="Email" required> |
| 27 | + <UForm |
| 28 | + :schema="postLoginBody" |
| 29 | + :state="userCredentials" |
| 30 | + @submit="handleLogin"> |
| 31 | + <UFormGroup label="Email" name="email" required> |
68 | 32 | <UInput
|
69 | 33 | v-model="userCredentials.email"
|
70 | 34 | placeholder="Enter Email"
|
71 | 35 | type="email"
|
72 | 36 | icon="i-heroicons-envelope" />
|
73 | 37 | </UFormGroup>
|
74 | 38 |
|
75 |
| - <UFormGroup label="Password" required> |
| 39 | + <UFormGroup label="Password" name="password" required> |
76 | 40 | <UInput
|
77 | 41 | v-model="userCredentials.password"
|
78 | 42 | placeholder="Enter Password"
|
|
0 commit comments