|
1 | 1 | <template> |
2 | 2 | <div> |
3 | | - <form action="" method="post"> |
| 3 | + <form method="post" v-on:submit.prevent="submitForm"> |
4 | 4 | <div class="content-box"> |
5 | 5 | <h2 class="mb-6 text-grey-darkest font-medium">Change your password</h2> |
6 | 6 | <div class="mt-6"> |
7 | 7 | <label for="grid-password-current" class="input-label">Current password</label> |
8 | | - <input id="grid-password-current" name="password" type="password" class="input" required> |
| 8 | + <input id="grid-password-current" name="current_password" type="password" class="input" required |
| 9 | + ref="current" |
| 10 | + v-model="current" |
| 11 | + > |
9 | 12 | </div> |
10 | 13 | <div class="mt-6"> |
11 | 14 | <label for="grid-password-new" class="input-label">New password</label> |
12 | | - <input id="grid-password-new" name="password" type="password" class="input" required> |
13 | | - <p class="input-help">Your new password should be at least 8 characters in length, contain one number, and be mixed case.</p> |
| 15 | + <input id="grid-password-new" name="password" type="password" class="input" required |
| 16 | + :class="{ error: errors.has('password') }" |
| 17 | + v-model="newPassword" |
| 18 | + v-validate="'min:8'" |
| 19 | + > |
| 20 | + <p class="input-help error" v-show="errors.has('password')">{{ errors.first('password') }}</p> |
| 21 | + <p class="input-help">Your new password should be at least 8 characters in length.</p> |
14 | 22 | </div> |
15 | 23 | <div class="mt-6"> |
16 | 24 | <label for="grid-password-new-confirm" class="input-label">Confirm new password</label> |
17 | | - <input id="grid-password-new-confirm" name="password_confirmation" type="password" class="input" required> |
| 25 | + <input id="grid-password-new-confirm" name="password_confirmation" type="password" class="input" required |
| 26 | + :class="{ error: errors.has('password_confirmation') }" |
| 27 | + v-model="confirmNew" |
| 28 | + v-validate="{is: newPassword}" |
| 29 | + data-vv-as="password" |
| 30 | + > |
| 31 | + <p class="input-help error" v-show="errors.has('password_confirmation')">{{ errors.first('password_confirmation') }}</p> |
18 | 32 | </div> |
19 | 33 | <div class="mt-6 text-right"> |
20 | 34 | <button class="btn btn-blue btn-sm text-right" type="submit">Save</button> |
|
25 | 39 | </template> |
26 | 40 |
|
27 | 41 | <script> |
| 42 | + import isObject from 'lodash/isObject'; |
| 43 | +
|
28 | 44 | export default { |
29 | | - name: 'change-password' |
| 45 | + name: 'change-password', |
| 46 | + data: function () { |
| 47 | + return { |
| 48 | + current: '', |
| 49 | + newPassword: '', |
| 50 | + confirmNew: '', |
| 51 | + }; |
| 52 | + }, |
| 53 | +
|
| 54 | + methods: { |
| 55 | + submitForm: function () { |
| 56 | + window.axios.put(this.route('api.client.account.update-password'), { |
| 57 | + current_password: this.$data.current, |
| 58 | + password: this.$data.newPassword, |
| 59 | + password_confirmation: this.$data.confirmNew, |
| 60 | + }) |
| 61 | + .finally(() => { |
| 62 | + this.clearFlashes(); |
| 63 | + this.$validator.pause(); |
| 64 | + this.$data.current = ''; |
| 65 | + this.$refs.current.focus(); |
| 66 | + }) |
| 67 | + .then(() => { |
| 68 | + this.$data.newPassword = ''; |
| 69 | + this.$data.confirmNew = ''; |
| 70 | +
|
| 71 | + this.success('Your password has been updated.'); |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + if (!err.response) { |
| 75 | + return console.error(err); |
| 76 | + } |
| 77 | +
|
| 78 | + const response = err.response; |
| 79 | + if (response.data && isObject(response.data.errors)) { |
| 80 | + response.data.errors.forEach(error => { |
| 81 | + this.error(error.detail); |
| 82 | + }); |
| 83 | + } |
| 84 | + }) |
| 85 | + .finally(() => { |
| 86 | + this.$validator.resume(); |
| 87 | + }) |
| 88 | + } |
| 89 | + } |
30 | 90 | }; |
31 | 91 | </script> |
0 commit comments