Skip to content

Commit 1f24b0c

Browse files
committed
feature(validation) refactor login validation
1 parent 129e4e2 commit 1f24b0c

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

composables/useAuth.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useRouter, useState } from "#app";
22
import { ISession } from "~~/types/ISession";
33
import { IUser } from "~/types/IUser";
4+
import useErrorMapper from "./useErrorMapper";
45

56
export const useAuthCookie = () => useCookie('auth_token')
67

@@ -62,9 +63,7 @@ export async function registerWithEmail(
6263

6364
return { hasErrors: false, loggedIn: true }
6465
} catch (error: any) {
65-
const parsedErrors = JSON.parse(error.data.data)
66-
const errorMap = new Map<string, { message: InputValidation; }>(Object.entries(parsedErrors))
67-
return { hasErrors: true, errors: errorMap }
66+
return useErrorMapper(error.data.data)
6867
}
6968
}
7069

@@ -73,16 +72,13 @@ export async function loginWithEmail(usernameOrEmail: string, password: string):
7372
const result = await $fetch('/api/auth/login', { method: 'POST', body: { usernameOrEmail: usernameOrEmail, password: password } })
7473

7574
if (!result?.id) {
76-
debugger
7775
throw Error('something went wrong')
7876
}
7977
useState('user').value = result
8078
await useRouter().push('/topics')
8179

8280
return { hasErrors: false, loggedIn: true }
8381
} catch (error: any) {
84-
const parsedErrors = JSON.parse(error.data.data)
85-
const errorMap = new Map<string, { message: InputValidation; }>(Object.entries(parsedErrors))
86-
return { hasErrors: true, errors: errorMap }
82+
return useErrorMapper(error.data.data)
8783
}
8884
}

composables/useErrorMapper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default (errorData: string) => {
2+
const parsedErrors = JSON.parse(errorData)
3+
const errorMap = new Map<string, { message: InputValidation; }>(Object.entries(parsedErrors))
4+
return { hasErrors: true, errors: errorMap }
5+
}

pages/login.vue

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ import type { Ref } from "vue"
55
66
const usernameOrEmail = ref('')
77
const password = ref('')
8-
const hasError: Ref<boolean | null> = ref(null)
9-
const errorMessage: Ref<string | null> = ref(null)
10-
const errors: Ref<Map<string, { message: InputValidation }> | undefined> = ref(new Map<string, { message: InputValidation }>())
11-
12-
// definePageMeta({
13-
// middleware: 'guest'
14-
// })
158
9+
const errors: Ref<Map<string, { message: InputValidation; }> | undefined> = ref(new Map<string, { message: InputValidation }>())
1610
let response: FormValidation
1711
12+
definePageMeta({
13+
middleware: 'guest'
14+
})
15+
16+
17+
1818
async function postLoginForm() {
1919
response = await loginWithEmail(usernameOrEmail.value, password.value)
2020
errors.value = response.errors
21-
const test = errors.value
22-
debugger
23-
console.log('errors', errors.value)
2421
}
2522
</script>
2623

@@ -50,18 +47,6 @@ async function postLoginForm() {
5047
</ul>
5148
</div>
5249

53-
<div v-if="hasError" class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative"
54-
role="alert">
55-
<strong class="font-bold">Oops, try again! </strong>
56-
<span class="block sm:inline">{{ errorMessage }}</span>
57-
<span class="absolute top-0 bottom-0 right-0 px-4 py-3">
58-
<svg class="fill-current h-6 w-6 text-red-500" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
59-
<title>Close</title>
60-
<path
61-
d="M14.348 14.849a1.2 1.2 0 0 1-1.697 0L10 11.819l-2.651 3.029a1.2 1.2 0 1 1-1.697-1.697l2.758-3.15-2.759-3.152a1.2 1.2 0 1 1 1.697-1.697L10 8.183l2.651-3.031a1.2 1.2 0 1 1 1.697 1.697l-2.758 3.152 2.758 3.15a1.2 1.2 0 0 1 0 1.698z" />
62-
</svg>
63-
</span>
64-
</div>
6550
<form v-if="true" v-on:submit.prevent class="mt-8 space-y-6" action="#" method="POST">
6651
<input type="hidden" name="remember" value="true">
6752
<div class="rounded-md shadow-sm -space-y-px mb-1">

pages/register.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const email: Ref<string> = ref('');
77
const password: Ref<string> = ref('');
88
const username: Ref<string> = ref('');
99
const name: Ref<string> = ref('');
10+
11+
1012
const errors: Ref<Map<string, { message: InputValidation; }> | undefined> = ref(new Map<string, { message: InputValidation }>())
1113
let response: FormValidation
1214
1315
async function postRegisterForm() {
1416
response = await registerWithEmail(username.value, name.value, email.value, password.value);
1517
errors.value = response.errors
16-
const test = errors.value
17-
debugger
1818
};
1919
2020
</script>

0 commit comments

Comments
 (0)