|
| 1 | +<script setup> |
| 2 | +import { useTemplateRef } from 'vue'; |
| 3 | +import { useToast } from 'primevue/usetoast'; |
| 4 | +import { useAxiosForm } from '@/composables/useAxiosForm'; |
| 5 | +import { useAuthStore } from '@/stores/auth'; |
| 6 | +import Password from 'primevue/password'; |
| 7 | +import AppLayout from '@/layouts/AppLayout.vue'; |
| 8 | +import SettingsLayout from '@/layouts/UserSettingsLayout.vue'; |
| 9 | +import InputErrors from '@/components/InputErrors.vue'; |
| 10 | +
|
| 11 | +const toast = useToast(); |
| 12 | +const authStore = useAuthStore(); |
| 13 | +
|
| 14 | +const currentPasswordInput = useTemplateRef('current-password-input'); |
| 15 | +const newPasswordInput = useTemplateRef('new-password-input'); |
| 16 | +
|
| 17 | +const { |
| 18 | + data: formData, |
| 19 | + validationErrors, |
| 20 | + processing: updating, |
| 21 | + put: submitForm, |
| 22 | + reset: resetFormFields, |
| 23 | +} = useAxiosForm({ |
| 24 | + current_password: '', |
| 25 | + password: '', |
| 26 | + password_confirmation: '', |
| 27 | +}); |
| 28 | +
|
| 29 | +const submit = () => { |
| 30 | + submitForm('/password', { |
| 31 | + onSuccess: async () => { |
| 32 | + toast.add({ |
| 33 | + severity: 'success', |
| 34 | + summary: 'Saved', |
| 35 | + detail: 'Your password has been updated', |
| 36 | + life: 3000, |
| 37 | + }); |
| 38 | + resetFormFields(); |
| 39 | + authStore.fetchUser(); |
| 40 | + }, |
| 41 | + onError: () => { |
| 42 | + if (validationErrors.value?.password) { |
| 43 | + resetFormFields('password', 'password_confirmation'); |
| 44 | + newPasswordInput.value.$el.focus(); |
| 45 | + } |
| 46 | + if (validationErrors.value?.current_password) { |
| 47 | + resetFormFields('current_password'); |
| 48 | + currentPasswordInput.value.$el.focus(); |
| 49 | + } |
| 50 | + }, |
| 51 | + }); |
| 52 | +}; |
| 53 | +</script> |
| 54 | +
|
| 55 | +<template> |
| 56 | + <AppLayout> |
| 57 | + <InertiaHead title="Password Settings" /> |
| 58 | +
|
| 59 | + <SettingsLayout> |
| 60 | + <Card |
| 61 | + pt:body:class="max-w-2xl space-y-3" |
| 62 | + pt:caption:class="space-y-1" |
| 63 | + > |
| 64 | + <template #title>Update Password</template> |
| 65 | + <template #subtitle> |
| 66 | + Ensure your account is using a long, random password to stay secure |
| 67 | + </template> |
| 68 | + <template #content> |
| 69 | + <form |
| 70 | + class="space-y-6" |
| 71 | + @submit.prevent="submit" |
| 72 | + > |
| 73 | + <div class="flex flex-col gap-2"> |
| 74 | + <label for="current_password">Current Password</label> |
| 75 | + <InputText |
| 76 | + id="current_password" |
| 77 | + ref="current-password-input" |
| 78 | + v-model="formData.current_password" |
| 79 | + type="password" |
| 80 | + class="w-full" |
| 81 | + :invalid="Boolean(validationErrors?.current_password)" |
| 82 | + autocomplete="current-password" |
| 83 | + required |
| 84 | + /> |
| 85 | + <InputErrors :errors="validationErrors?.current_password" /> |
| 86 | + </div> |
| 87 | +
|
| 88 | + <div class="flex flex-col gap-2"> |
| 89 | + <label for="password">New Password</label> |
| 90 | + <InputText |
| 91 | + id="password" |
| 92 | + ref="new-password-input" |
| 93 | + v-model="formData.password" |
| 94 | + type="password" |
| 95 | + class="w-full" |
| 96 | + :invalid="Boolean(validationErrors?.password)" |
| 97 | + autocomplete="new-password" |
| 98 | + required |
| 99 | + /> |
| 100 | + <InputErrors :errors="validationErrors?.password" /> |
| 101 | + </div> |
| 102 | +
|
| 103 | + <div class="flex flex-col gap-2"> |
| 104 | + <label for="password_confirmation">Confirm Password</label> |
| 105 | + <InputText |
| 106 | + id="password_confirmation" |
| 107 | + v-model="formData.password_confirmation" |
| 108 | + type="password" |
| 109 | + class="w-full" |
| 110 | + :invalid="Boolean(validationErrors?.password_confirmation)" |
| 111 | + autocomplete="new-password" |
| 112 | + required |
| 113 | + /> |
| 114 | + <InputErrors :errors="validationErrors?.password_confirmation" /> |
| 115 | + </div> |
| 116 | +
|
| 117 | + <div class="flex items-center gap-4"> |
| 118 | + <Button |
| 119 | + type="submit" |
| 120 | + label="Save" |
| 121 | + :loading="updating" |
| 122 | + /> |
| 123 | + </div> |
| 124 | + </form> |
| 125 | + </template> |
| 126 | + </Card> |
| 127 | + </SettingsLayout> |
| 128 | + </AppLayout> |
| 129 | +</template> |
0 commit comments