Added vuelidate to Login-form
This commit is contained in:
@@ -207,10 +207,6 @@ function onToggleEdit() {
|
|||||||
state.reset()
|
state.reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
function onCreateUser() {
|
|
||||||
createUser.value = true
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(() => [route.params, route.name], ([newParam, newName], [oldParam, oldName]) => {
|
watch(() => [route.params, route.name], ([newParam, newName], [oldParam, oldName]) => {
|
||||||
if(newName === oldName && newParam !== oldParam) {
|
if(newName === oldName && newParam !== oldParam) {
|
||||||
state.fetchFromApi(route.params.id)
|
state.fetchFromApi(route.params.id)
|
||||||
|
|||||||
@@ -1,36 +1,39 @@
|
|||||||
<template>
|
<template>
|
||||||
<img src="/src/assets/logo.png">
|
<img src="/src/assets/logo.png">
|
||||||
|
|
||||||
<form class="m-auto" novalidate>
|
<form class="m-auto" novalidate>
|
||||||
<h1 class="h3 mb-4">Bitte einloggen</h1>
|
<h1 class="h3 mb-4">Bitte einloggen</h1>
|
||||||
<div class="form-floating">
|
<div class="form-floating">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
:class="validation.username"
|
:class="classIsInvalid(v$.username)"
|
||||||
id="usernameInput"
|
id="usernameInput"
|
||||||
v-model="input.username"
|
v-model="input.username"
|
||||||
placeholder="Benutzername"
|
placeholder="Benutzername"
|
||||||
aria-labeledby="username-feedback"
|
aria-labeledby="username-feedback"
|
||||||
>
|
>
|
||||||
<label id="username-feedback" for="usernameInput">Benutzername</label>
|
<label
|
||||||
<div class="invalid-feedback">
|
id="username-feedback"
|
||||||
{{valMessages.username}}
|
for="usernameInput"
|
||||||
</div>
|
:style="styleIsInvalid(v$.username)"
|
||||||
|
>{{ v$.username.$errors[0] ? v$.username.$errors[0].$message : 'Benutzername' }}</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-floating mb-4">
|
<div class="form-floating mb-4">
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
:class="validation.password"
|
:class="classIsInvalid(v$.password)"
|
||||||
id="passwordInput"
|
id="passwordInput"
|
||||||
v-model="input.password"
|
v-model="input.password"
|
||||||
placeholder="Passwort"
|
placeholder="Passwort"
|
||||||
aria-describedby="passwordFeedback"
|
aria-describedby="passwordFeedback"
|
||||||
>
|
>
|
||||||
<label for="passwordInput">Passwort</label>
|
<label
|
||||||
<div id="passwordFeedback" class="invalid-feedback">
|
for="passwordInput"
|
||||||
{{valMessages.password}}
|
:style=" styleIsInvalid(v$.password) "
|
||||||
</div>
|
>{{ v$.password.$errors[0] ? v$.password.$errors[0].$message : 'Passwort' }}</label>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
@@ -45,54 +48,48 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useUser } from '@/stores/user'
|
import { useUser } from '@/stores/user'
|
||||||
import { reactive, watch } from 'vue'
|
import { reactive, computed, ref, watch } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
|
import useVuelidate from '@vuelidate/core'
|
||||||
|
import { helpers, required } from '@vuelidate/validators'
|
||||||
|
|
||||||
const userStore = useUser()
|
const userStore = useUser()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const validation = reactive({
|
|
||||||
failure: false,
|
|
||||||
username: {
|
|
||||||
'is-invalid': false,
|
|
||||||
},
|
|
||||||
password: {
|
|
||||||
'is-invalid': false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const valMessages = reactive({
|
|
||||||
username: '',
|
|
||||||
password: ''
|
|
||||||
})
|
|
||||||
|
|
||||||
const input = reactive({
|
const input = reactive({
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(() => input.username, (username) => {
|
const valPasswordCorrect = () => {
|
||||||
if(username.length > 0) validation.username['is-invalid'] = false
|
return apiResponse.value !== 'E_INVALID_AUTH_PASSWORD: Password mis-match'
|
||||||
})
|
}
|
||||||
watch(() => input.password, (password) => {
|
const valUsernameCorrect = () => {
|
||||||
if(password.length > 0) validation.password['is-invalid'] = false
|
return apiResponse.value !== 'E_INVALID_AUTH_UID: User not found'
|
||||||
|
}
|
||||||
|
|
||||||
|
const rules = computed(() => ({
|
||||||
|
username: {
|
||||||
|
required: helpers.withMessage("Benutzername erforderlich.", required),
|
||||||
|
valUsernameCorrect: helpers.withMessage('Benutzername nicht gefunden.', valUsernameCorrect)
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
required: helpers.withMessage("Passwort erforderlich.", required),
|
||||||
|
valPasswordCorrect: helpers.withMessage('Passwort falsch.', valPasswordCorrect)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
watch(input, () => {
|
||||||
|
apiResponse.value = ''
|
||||||
|
v$.value.$reset()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const v$ = useVuelidate(rules, input)
|
||||||
|
|
||||||
|
const apiResponse = ref('')
|
||||||
|
|
||||||
async function onClick() {
|
async function onClick() {
|
||||||
resetFailureState()
|
if(!(await v$.value.$validate())) return
|
||||||
|
|
||||||
if(input.username === '') {
|
|
||||||
validation.username['is-invalid'] = true
|
|
||||||
valMessages.username = 'Benutzername kann nicht leer sein.'
|
|
||||||
validation.failure = true
|
|
||||||
}
|
|
||||||
if(input.password === ''){
|
|
||||||
validation.password['is-invalid'] = true
|
|
||||||
valMessages.password = 'Passwort kann nicht leer sein'
|
|
||||||
validation.failure = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if(validation.failure) return
|
|
||||||
|
|
||||||
const result = await userStore.login(input.username, input.password)
|
const result = await userStore.login(input.username, input.password)
|
||||||
|
|
||||||
@@ -100,27 +97,23 @@ async function onClick() {
|
|||||||
case true:
|
case true:
|
||||||
router.push({name: 'Home'})
|
router.push({name: 'Home'})
|
||||||
break;
|
break;
|
||||||
case 'E_INVALID_AUTH_PASSWORD: Password mis-match':
|
case false:
|
||||||
validation.password['is-invalid'] = true
|
|
||||||
valMessages.password = 'Passwort falsch.'
|
|
||||||
break;
|
|
||||||
case 'E_INVALID_AUTH_UID: User not found':
|
|
||||||
validation.username['is-invalid'] = true
|
|
||||||
valMessages.username = 'Benutzer nicht gefunden'
|
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
apiResponse.value = result
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const resetFailureState = () => {
|
function classIsInvalid(object: any) : string {
|
||||||
validation.failure = false
|
return object.$dirty && object.$invalid ? 'is-invalid' : ''
|
||||||
validation.password['is-invalid'] = false
|
|
||||||
validation.username['is-invalid'] = false
|
|
||||||
valMessages.password = ''
|
|
||||||
valMessages.username = ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function styleIsInvalid(object: any) : string {
|
||||||
|
if(object.$error){
|
||||||
|
return 'color: red'
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user