FitTrackee/fittrackee_client/src/components/User/UserAuthForm.vue

269 lines
7.2 KiB
Vue
Raw Normal View History

2021-08-14 19:43:19 +02:00
<template>
2022-02-26 14:04:48 +01:00
<div
id="user-auth-form"
:class="`${
['reset', 'reset-request'].includes(action) ? action : 'user-form'
}`"
>
2021-08-14 19:43:19 +02:00
<div id="user-form">
<div
class="form-box"
:class="{
disabled: registration_disabled,
}"
>
<AlertMessage
message="user.REGISTER_DISABLED"
v-if="registration_disabled"
/>
<form
:class="{ errors: formErrors }"
@submit.prevent="onSubmit(action)"
>
2021-08-14 19:43:19 +02:00
<div class="form-items">
<input
v-if="action === 'register'"
id="username"
:disabled="registration_disabled"
2021-08-14 19:43:19 +02:00
required
pattern="[a-zA-Z0-9_]+"
minlength="3"
maxlength="30"
@invalid="invalidateForm"
2021-08-14 19:43:19 +02:00
v-model="formData.username"
2021-10-20 17:38:25 +02:00
:placeholder="$t('user.USERNAME')"
2021-08-14 19:43:19 +02:00
/>
<div v-if="action === 'register'" class="form-info">
<i class="fa fa-info-circle" aria-hidden="true" />
{{ $t('user.USERNAME_INFO') }}
</div>
2021-08-14 19:43:19 +02:00
<input
2021-10-20 17:38:25 +02:00
v-if="action !== 'reset'"
2021-08-14 19:43:19 +02:00
id="email"
:disabled="registration_disabled"
2021-08-14 19:43:19 +02:00
required
@invalid="invalidateForm"
2021-08-14 19:43:19 +02:00
type="email"
v-model="formData.email"
:placeholder="$t('user.EMAIL')"
2021-08-14 19:43:19 +02:00
/>
<div
v-if="['reset-request', 'register'].includes(action)"
class="form-info"
>
<i class="fa fa-info-circle" aria-hidden="true" />
{{ $t('user.EMAIL_INFO') }}
</div>
2022-02-23 18:08:45 +01:00
<PasswordInput
2021-10-20 17:38:25 +02:00
v-if="action !== 'reset-request'"
:disabled="registration_disabled"
2022-02-23 18:08:45 +01:00
:required="true"
2021-10-20 17:38:25 +02:00
:placeholder="
action === 'reset'
? $t('user.ENTER_PASSWORD')
: $t('user.PASSWORD')
"
:password="formData.password"
2022-02-23 18:08:45 +01:00
@updatePassword="updatePassword"
@passwordError="invalidateForm"
2021-08-14 19:43:19 +02:00
/>
<div
v-if="['reset', 'register'].includes(action)"
class="form-info"
>
<i class="fa fa-info-circle" aria-hidden="true" />
{{ $t('user.PASSWORD_INFO') }}
</div>
<PasswordStrength
v-if="['reset', 'register'].includes(action)"
:password="formData.password"
/>
2021-08-14 19:43:19 +02:00
</div>
<button type="submit" :disabled="registration_disabled">
2021-10-20 17:38:25 +02:00
{{ $t(buttonText) }}
</button>
2021-08-14 19:43:19 +02:00
</form>
2021-10-20 17:38:25 +02:00
<div v-if="action === 'login'">
<router-link class="links" to="/register">
{{ $t('user.REGISTER') }}
</router-link>
-
<router-link class="links" to="/password-reset/request">
2021-10-20 17:38:25 +02:00
{{ $t('user.PASSWORD_FORGOTTEN') }}
</router-link>
</div>
<div v-if="action === 'register'">
<span class="account">{{ $t('user.ALREADY_HAVE_ACCOUNT') }}</span>
<router-link class="links" to="/login">
{{ $t('user.LOGIN') }}
</router-link>
</div>
<ErrorMessage :message="errorMessages" v-if="errorMessages" />
2021-08-14 19:43:19 +02:00
</div>
</div>
</div>
</template>
<script setup lang="ts">
import {
ComputedRef,
computed,
reactive,
ref,
toRefs,
watch,
withDefaults,
} from 'vue'
import { useRoute } from 'vue-router'
2021-08-14 19:43:19 +02:00
2022-02-23 18:08:45 +01:00
import PasswordInput from '@/components/Common/PasswordInput.vue'
import PasswordStrength from '@/components/Common/PasswordStength.vue'
import { AUTH_USER_STORE, ROOT_STORE } from '@/store/constants'
import { TAppConfig } from '@/types/application'
2021-08-21 18:36:44 +02:00
import { ILoginRegisterFormData } from '@/types/user'
2021-08-14 19:43:19 +02:00
import { useStore } from '@/use/useStore'
interface Props {
action: string
token?: string
}
const props = withDefaults(defineProps<Props>(), {
token: '',
})
const route = useRoute()
const store = useStore()
const { action } = toRefs(props)
const formData: ILoginRegisterFormData = reactive({
username: '',
email: '',
password: '',
2021-08-14 19:43:19 +02:00
})
const buttonText: ComputedRef<string> = computed(() =>
getButtonText(props.action)
)
const errorMessages: ComputedRef<string | string[] | null> = computed(
() => store.getters[ROOT_STORE.GETTERS.ERROR_MESSAGES]
)
const appConfig: ComputedRef<TAppConfig> = computed(
() => store.getters[ROOT_STORE.GETTERS.APP_CONFIG]
)
const registration_disabled: ComputedRef<boolean> = computed(
() =>
props.action === 'register' && !appConfig.value.is_registration_enabled
)
const formErrors = ref(false)
function getButtonText(action: string): string {
switch (action) {
case 'reset-request':
case 'reset':
return 'buttons.SUBMIT'
default:
return `buttons.${props.action.toUpperCase()}`
}
}
function invalidateForm() {
formErrors.value = true
}
2022-02-23 18:08:45 +01:00
function updatePassword(password: string) {
formData.password = password
}
function onSubmit(actionType: string) {
switch (actionType) {
case 'reset':
if (!props.token) {
return store.commit(
ROOT_STORE.MUTATIONS.SET_ERROR_MESSAGES,
'user.INVALID_TOKEN'
)
}
return store.dispatch(AUTH_USER_STORE.ACTIONS.RESET_USER_PASSWORD, {
password: formData.password,
token: props.token,
})
case 'reset-request':
return store.dispatch(
AUTH_USER_STORE.ACTIONS.SEND_PASSWORD_RESET_REQUEST,
{
email: formData.email,
}
)
default:
store.dispatch(AUTH_USER_STORE.ACTIONS.LOGIN_OR_REGISTER, {
actionType,
formData,
redirectUrl: route.query.from,
})
}
}
function resetFormData() {
formData.username = ''
formData.email = ''
formData.password = ''
}
watch(
() => route.path,
async () => {
store.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
formErrors.value = false
resetFormData()
}
)
2021-08-14 19:43:19 +02:00
</script>
<style scoped lang="scss">
2021-11-29 11:23:21 +01:00
@import '~@/scss/colors.scss';
@import '~@/scss/vars.scss';
2021-08-14 19:43:19 +02:00
2021-10-20 17:38:25 +02:00
#user-auth-form {
2021-08-14 19:43:19 +02:00
display: flex;
#user-form {
width: 60%;
.account {
2021-10-20 17:38:25 +02:00
font-size: 0.9em;
padding-left: $default-padding;
}
.links {
font-size: 0.9em;
font-style: italic;
padding: 0 $default-padding;
}
.form-info {
color: var(--alert-color);
font-size: 0.8em;
margin-top: -0.2 * $default-margin;
padding: 0 $default-padding * 1.5;
}
2021-10-20 17:38:25 +02:00
2021-08-14 19:43:19 +02:00
button {
margin: $default-margin;
border: solid 1px var(--app-color);
&:disabled {
border-color: var(--disabled-color);
}
2021-08-14 19:43:19 +02:00
}
}
@media screen and (max-width: $medium-limit) {
margin-bottom: 50px;
#user-form {
width: 100%;
}
2021-08-14 19:43:19 +02:00
}
}
2022-02-26 14:04:48 +01:00
.user-form {
margin-top: 200px;
@media screen and (max-width: $small-limit) {
margin-top: $default-margin;
}
}
2021-08-14 19:43:19 +02:00
</style>