| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <template>
- <Form
- class="flex flex-col gap-2 p-5"
- v-slot="$form"
- :initialValues="initialValues"
- :resolver="resolver"
- :validateOnValueUpdate="false"
- :validateOnBlur="true"
- @submit="onFormSubmit"
- >
- <div class="flex flex-col">
- <label
- for="current_password"
- class="ml-1 text-sm text-primary dark:text-primary"
- >
- Current Password
- </label>
- <Password
- name="currentPassword"
- :feedback="false"
- fluid
- toggleMask
- inputId="current_password"
- placeholder="Current password"
- autocomplete="current-password"
- >
- <template #maskicon="{ toggleCallback }">
- <Icon
- class="absolute inset-e-3 top-1/2 -mt-2 h-4 w-4 text-surface-500 dark:text-surface-400"
- name="ph:eye-slash-bold"
- @click="toggleCallback"
- />
- </template>
- <template #unmaskicon="{ toggleCallback }">
- <Icon
- class="absolute inset-e-3 top-1/2 -mt-2 h-4 w-4 text-surface-500 dark:text-surface-400"
- name="ph:eye-bold"
- @click="toggleCallback"
- />
- </template>
- </Password>
- <Message
- v-if="$form.currentPassword?.invalid"
- severity="error"
- size="small"
- variant="simple"
- >
- {{ $form.currentPassword?.error?.message }}
- </Message>
- </div>
- <div class="flex flex-col">
- <label
- for="new_password"
- class="ml-1 text-sm text-primary dark:text-primary"
- >
- New Password
- </label>
- <Password
- name="newPassword"
- :feedback="false"
- fluid
- toggleMask
- inputId="new_password"
- placeholder="New password"
- autocomplete="new-password"
- >
- <template #maskicon="{ toggleCallback }">
- <Icon
- class="absolute inset-e-3 top-1/2 -mt-2 h-4 w-4 text-surface-500 dark:text-surface-400"
- name="ph:eye-slash-bold"
- @click="toggleCallback"
- />
- </template>
- <template #unmaskicon="{ toggleCallback }">
- <Icon
- class="absolute inset-e-3 top-1/2 -mt-2 h-4 w-4 text-surface-500 dark:text-surface-400"
- name="ph:eye-bold"
- @click="toggleCallback"
- />
- </template>
- </Password>
- <Message
- v-if="$form.newPassword?.invalid"
- severity="error"
- size="small"
- variant="simple"
- >
- {{ $form.newPassword?.error?.message }}
- </Message>
- </div>
- <Button
- label="Change Password"
- type="submit"
- fluid
- :disabled="!$form.valid"
- :loading="isLoading"
- class="mt-5"
- >
- <template #loadingicon>
- <Icon
- class="animate-spin"
- name="ph:circle-notch-bold"
- />
- </template>
- </Button>
- </Form>
- </template>
- <script setup>
- import { zodResolver } from "@primeuix/forms/resolvers/zod"
- const dialogRef = inject("dialogRef")
- const toast = useToast()
- const { authClient } = useAuthClient()
- const resolver = ref(
- zodResolver(
- z.object({
- currentPassword: z.string().min(1, "Current password is required."),
- confirmPassword: z
- .string()
- .min(12, "New password must be at least 12 characters.")
- })
- )
- )
- const initialValues = ref({
- currentPassword: "",
- newPassword: ""
- })
- const isLoading = ref(false)
- async function onFormSubmit({ valid, values }) {
- if (valid) {
- await changePassword(values)
- closeDialog()
- }
- }
- async function changePassword(values) {
- if (isLoading.value) {
- return
- }
- isLoading.value = true
- // noinspection JSUnresolvedReference
- const { error } = await authClient.changePassword(values)
- isLoading.value = false
- if (error) {
- console.error(error)
- toast.add({
- severity: "error",
- summary: "Change Password Error.",
- detail: error.data?.message || error.message
- })
- } else {
- toast.add({
- severity: "success",
- summary: "Password Changed.",
- detail: "Your password is changed.",
- life: 3000
- })
- }
- }
- function closeDialog() {
- dialogRef.value.close()
- }
- </script>
- <style scoped></style>
|