tethys.backend/resources/js/Pages/Admin/User/Edit.vue
Arno Kaimbacher 70f016422c hotfix(admin/user): implement password reset and update user password
- Implemented password reset functionality for admin users.
- Updated the user edit and create forms to use a password meter component for password strength validation.
- Modified the `AdminuserController` to handle the new password field and update user passwords.
- Updated the `createUserValidator` and `updateUserValidator` to validate the new password field.
- Updated the password field to `new_password` in the `Edit.vue` and `Create.vue` components.
- Added `showRequiredMessage` prop to `SimplePasswordMeter` component.
- Added conditional rendering for password strength bar in `SimplePasswordMeter` component.
- Added `fieldLabel` prop to `SimplePasswordMeter` component.
- Updated form submission to handle errors and reset password field.
2025-03-19 15:52:37 +01:00

177 lines
7.4 KiB
Vue

<script setup lang="ts">
import { ref } from 'vue';
import { Head, useForm, router } from '@inertiajs/vue3';
import { mdiAccountKey, mdiArrowLeftBoldOutline } from '@mdi/js';
import LayoutAuthenticated from '@/Layouts/LayoutAuthenticated.vue';
import SectionMain from '@/Components/SectionMain.vue';
import SectionTitleLineWithButton from '@/Components/SectionTitleLineWithButton.vue';
import CardBox from '@/Components/CardBox.vue';
import FormField from '@/Components/FormField.vue';
import FormControl from '@/Components/FormControl.vue';
import FormCheckRadioGroup from '@/Components/FormCheckRadioGroup.vue';
import BaseDivider from '@/Components/BaseDivider.vue';
import BaseButton from '@/Components/BaseButton.vue';
import BaseButtons from '@/Components/BaseButtons.vue';
import { stardust } from '@eidellev/adonis-stardust/client';
// import { Inertia } from '@inertiajs/inertia';
import PasswordMeter from '@/Components/SimplePasswordMeter/password-meter.vue';
const enabled = ref(false);
const props = defineProps({
user: {
type: Object,
default: () => ({}),
},
roles: {
type: Object,
default: () => ({}),
},
userHasRoles: {
type: Object,
default: () => ({}),
},
errors: {
type: Object,
default: () => ({}),
},
});
const form = useForm({
_method: 'put',
login: props.user.login,
first_name: props.user.first_name,
last_name: props.user.last_name,
email: props.user.email,
new_password: '',
password_confirmation: '',
roles: props.userHasRoles, // fill actual user roles from db
});
const submit = async () => {
// await Inertia.post(stardust.route('user.store'), form);
// await router.put(stardust.route('settings.user.update', [props.user.id]), form);
await form.put(stardust.route('settings.user.update', [props.user.id]), {
preserveScroll: true,
onSuccess: () => {
form.reset();
},
onError: () => {
if (form.errors.new_password) {
form.reset('new_password');
enabled.value = false;
// newPasswordInput.value.focus();
// newPasswordInput.value?.focus();
}
},
});
};
const handleScore = (score: number) => {
if (score >= 4){
enabled.value = true;
} else {
enabled.value = false;
}
};
</script>
<template>
<LayoutAuthenticated>
<Head title="Update user" />
<SectionMain>
<SectionTitleLineWithButton :icon="mdiAccountKey" title="Update user" main>
<BaseButton
:route-name="stardust.route('settings.user.index')"
:icon="mdiArrowLeftBoldOutline"
label="Back"
color="white"
rounded-full
small
/>
</SectionTitleLineWithButton>
<!-- <CardBox form @submit.prevent="form.put(stardust.route('user.update', [props.user.id]))"> -->
<CardBox form @submit.prevent="submit()">
<FormField label="Enter Login" :class="{ 'text-red-400': errors.name }">
<FormControl v-model="form.login" type="text" placeholder="Name" :errors="errors.login">
<div class="text-red-400 text-sm" v-if="errors.login">
{{ errors.login }}
</div>
</FormControl>
</FormField>
<FormField label="First Name" :class="{ 'text-red-400': errors.first_name }">
<FormControl v-model="form.first_name" type="text" placeholder="Enter First Name" :errors="errors.first_name">
<div class="text-red-400 text-sm" v-if="errors.first_name && Array.isArray(errors.first_name)">
{{ errors.first_name.join(', ') }}
</div>
</FormControl>
</FormField>
<FormField label="Last Name" :class="{ 'text-red-400': errors.last_name }">
<FormControl v-model="form.last_name" type="text" placeholder="Enter Last Name" :errors="errors.last_name">
<div class="text-red-400 text-sm" v-if="errors.last_name && Array.isArray(errors.last_name)">
{{ errors.last_name.join(', ') }}
</div>
</FormControl>
</FormField>
<FormField label="Enter Email" :class="{ 'text-red-400': errors.email }">
<FormControl v-model="form.email" type="text" placeholder="Email" :errors="errors.email">
<div class="text-red-400 text-sm" v-if="errors.email && Array.isArray(errors.email)">
{{ errors.email.join(', ') }}
</div>
</FormControl>
</FormField>
<!-- <FormField label="Password" :class="{ 'text-red-400': errors.password }">
<FormControl v-model="form.password" type="password" placeholder="Enter Password" :errors="errors.password">
<div class="text-red-400 text-sm" v-if="errors.password">
{{ errors.password }}
</div>
</FormControl>
</FormField> -->
<PasswordMeter field-label="Reset User Password" :show-required-message="false" ref="newPasswordInput" v-model="form.new_password" :errors="form.errors" @score="handleScore" />
<FormField label="Password Confirmation" :class="{ 'text-red-400': errors.password_confirmation }">
<FormControl
v-model="form.password_confirmation"
type="password"
placeholder="Enter Password Confirmation"
:errors="errors.password"
>
<div
class="text-red-400 text-sm"
v-if="errors.password_confirmation && Array.isArray(errors.password_confirmation)"
>
{{ errors.password_confirmation.join(', ') }}
</div>
</FormControl>
</FormField>
<BaseDivider />
<FormField label="Roles" wrap-body :class="{ 'text-red-400': errors.roles }">
<FormCheckRadioGroup v-model="form.roles" name="roles" is-column :options="props.roles" />
</FormField>
<div class="text-red-400 text-sm" v-if="errors.roles && Array.isArray(errors.roles)">
<!-- {{ errors.password_confirmation }} -->
{{ errors.roles.join(', ') }}
</div>
<template #footer>
<BaseButtons>
<BaseButton
type="submit"
color="info"
label="Submit"
:class="{ 'opacity-25': form.processing }"
:disabled="form.processing == true|| (form.new_password != '' && enabled == false)"
/>
</BaseButtons>
</template>
</CardBox>
</SectionMain>
</LayoutAuthenticated>
</template>