Skip to content

Commit 3cb1ade

Browse files
committed
[fix] changed password
1 parent cee0bda commit 3cb1ade

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

backend/app/Http/Requests/Api/Account/UpdateRequest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Rules\Email;
99
use App\Rules\RoleOwner;
1010
use App\Rules\RoleRequiredAdmin;
11+
use Illuminate\Support\Facades\Hash;
1112

1213
final class UpdateRequest extends Request
1314
{
@@ -26,20 +27,37 @@ public function rules(): array
2627
{
2728
return [
2829
'name' => 'required|string|max:255',
29-
'email' => ['email','max:255',new Email($this->user)],
30+
'email' => ['email', 'max:255', new Email($this->user)],
3031
'role_id' => [new RoleOwner(), new RoleRequiredAdmin($this->user)],
3132
];
3233
}
3334

35+
/**
36+
* @param \Illuminate\Contracts\Validation\Validator $validator
37+
* @return void
38+
*/
39+
public function withValidator(\Illuminate\Contracts\Validation\Validator $validator): void
40+
{
41+
$validator->sometimes('password', 'required|min:8|confirmed', function ($input) {
42+
return array_key_exists('password', $input->toArray());
43+
});
44+
}
45+
3446
/**
3547
* @return array
3648
*/
3749
public function makeInput(): array
3850
{
39-
return [
51+
$data = [
4052
'name' => $this->name,
4153
'email' => $this->email,
4254
'role_id' => $this->role_id,
4355
];
56+
57+
if (isset($this->password)) {
58+
$data = array_merge($data, ['password' => Hash::make($this->password),]);
59+
}
60+
61+
return $data;
4462
}
4563
}

frontend/components/account/UpdateDialog.vue

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@
3535
placeholder="Role Name"
3636
:error-messages="errors.role_id"
3737
></v-autocomplete>
38+
<v-switch
39+
v-model="isPasswordChange"
40+
color="light-green"
41+
label="change password"
42+
>
43+
</v-switch>
44+
<v-text-field
45+
v-show="isPasswordChange"
46+
v-model="accountModel.password"
47+
:append-icon="showPassword ? 'mdi-eye' : 'mdi-eye-off'"
48+
:type="showPassword ? 'text' : 'password'"
49+
name="password"
50+
label="Password"
51+
hint="At least 8 characters"
52+
counter
53+
required
54+
:error-messages="errors.password"
55+
@click:append="showPassword = !showPassword"
56+
></v-text-field>
57+
<v-text-field
58+
v-show="isPasswordChange"
59+
v-model="accountModel.password_confirmation"
60+
type="password"
61+
name="password_confirmation"
62+
label="Confirm Password"
63+
counter
64+
required
65+
:error-messages="errors.password_confirmation"
66+
></v-text-field>
3867
</v-col>
3968
</v-row>
4069
<div class="my-5"></div>
@@ -74,6 +103,8 @@ export default {
74103
data() {
75104
return {
76105
loading: false,
106+
showPassword: false,
107+
isPasswordChange: false,
77108
errors: {},
78109
}
79110
},
@@ -99,11 +130,33 @@ export default {
99130
]),
100131
close() {
101132
this.errors = {}
133+
this.isPasswordChange = false
102134
this.$emit('close')
103135
},
136+
resetPassword() {
137+
delete this.accountModel.password
138+
delete this.accountModel.password_confirmation
139+
},
140+
initPasswordIfNoInput() {
141+
if (!('password' in this.accountModel)) {
142+
this.accountModel.password = ''
143+
}
144+
if (!('password_confirmation' in this.accountModel)) {
145+
this.accountModel.password_confirmation = ''
146+
}
147+
},
148+
applyPassword() {
149+
if (!this.isPasswordChange) {
150+
this.resetPassword()
151+
} else {
152+
this.initPasswordIfNoInput()
153+
}
154+
},
104155
async update() {
105156
this.loading = true
106157
try {
158+
this.applyPassword()
159+
107160
await this.updateAccount(this.accountModel)
108161
109162
this.sendMessage({

0 commit comments

Comments
 (0)