Skip to content

Commit f61de57

Browse files
authored
Delete User password confirmation (#91)
* Password confirmation on user deletetion inertia * Livewire support * Typo fix * code style * update comment
1 parent 2474049 commit f61de57

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

Diff for: src/Http/Controllers/Inertia/CurrentUserController.php

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Contracts\Auth\StatefulGuard;
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
8+
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Validation\ValidationException;
810
use Laravel\Jetstream\Contracts\DeletesUsers;
911

1012
class CurrentUserController extends Controller
@@ -18,6 +20,12 @@ class CurrentUserController extends Controller
1820
*/
1921
public function destroy(Request $request, StatefulGuard $auth)
2022
{
23+
if (! Hash::check($request->password, $request->user()->password)) {
24+
throw ValidationException::withMessages([
25+
'password' => [__('This password does not match our records.')],
26+
])->errorBag('deleteUser');
27+
}
28+
2129
app(DeletesUsers::class)->delete($request->user()->fresh());
2230

2331
$auth->logout();

Diff for: src/Http/Livewire/DeleteUserForm.php

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Contracts\Auth\StatefulGuard;
66
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Support\Facades\Hash;
8+
use Illuminate\Validation\ValidationException;
79
use Laravel\Jetstream\Contracts\DeletesUsers;
810
use Livewire\Component;
911

@@ -16,6 +18,27 @@ class DeleteUserForm extends Component
1618
*/
1719
public $confirmingUserDeletion = false;
1820

21+
/**
22+
* The user's current password.
23+
*
24+
* @var string
25+
*/
26+
public $password = '';
27+
28+
/**
29+
* Confirm that the user would like to delete their account.
30+
*
31+
* @return void
32+
*/
33+
public function confirmDelete()
34+
{
35+
$this->password = '';
36+
37+
$this->dispatchBrowserEvent('confirming-delete-user');
38+
39+
$this->confirmingUserDeletion = true;
40+
}
41+
1942
/**
2043
* Delete the current user.
2144
*
@@ -25,6 +48,14 @@ class DeleteUserForm extends Component
2548
*/
2649
public function deleteUser(DeletesUsers $deleter, StatefulGuard $auth)
2750
{
51+
$this->resetErrorBag();
52+
53+
if (! Hash::check($this->password, Auth::user()->password)) {
54+
throw ValidationException::withMessages([
55+
'password' => [__('This password does not match our records.')],
56+
]);
57+
}
58+
2859
$deleter->delete(Auth::user()->fresh());
2960

3061
$auth->logout();

Diff for: stubs/inertia/resources/js/Pages/Profile/DeleteUserForm.vue

+24-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,24 @@
2626
</template>
2727

2828
<template #content>
29-
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted.
29+
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted. Please enter your password to confirm you would like to permanently delete your account.
30+
31+
<div class="mt-4">
32+
<jet-input type="password" class="mt-1 block w-3/4" placeholder="Password"
33+
ref="password"
34+
v-model="form.password"
35+
@keyup.enter.native="deleteUser" />
36+
37+
<jet-input-error :message="form.error('password')" class="mt-2" />
38+
</div>
3039
</template>
3140

3241
<template #footer>
3342
<jet-secondary-button @click.native="confirmingUserDeletion = false">
3443
Nevermind
3544
</jet-secondary-button>
3645

37-
<jet-danger-button class="ml-2" @click.native="deleteTeam" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
46+
<jet-danger-button class="ml-2" @click.native="deleteUser" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
3847
Delete Account
3948
</jet-danger-button>
4049
</template>
@@ -48,6 +57,8 @@
4857
import JetButton from './../../Jetstream/Button'
4958
import JetConfirmationModal from './../../Jetstream/ConfirmationModal'
5059
import JetDangerButton from './../../Jetstream/DangerButton'
60+
import JetInput from './../../Jetstream/Input'
61+
import JetInputError from './../../Jetstream/InputError'
5162
import JetSecondaryButton from './../../Jetstream/SecondaryButton'
5263
5364
export default {
@@ -56,6 +67,8 @@
5667
JetButton,
5768
JetConfirmationModal,
5869
JetDangerButton,
70+
JetInput,
71+
JetInputError,
5972
JetSecondaryButton,
6073
},
6174
@@ -65,7 +78,8 @@
6578
deleting: false,
6679
6780
form: this.$inertia.form({
68-
//
81+
'_method': 'DELETE',
82+
password: '',
6983
}, {
7084
bag: 'deleteUser'
7185
})
@@ -77,10 +91,14 @@
7791
this.confirmingUserDeletion = true
7892
},
7993
80-
deleteTeam() {
81-
this.form.delete('/user', {
94+
deleteUser() {
95+
this.form.post('/user', {
8296
preserveScroll: true
83-
});
97+
}).then(response => {
98+
if (! this.form.hasErrors()) {
99+
this.confirmingUserDeletion = false
100+
}
101+
})
84102
},
85103
},
86104
}

Diff for: stubs/livewire/resources/views/profile/delete-user-form.blade.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,22 @@
1919
</div>
2020

2121
<!-- Delete User Confirmation Modal -->
22-
<x-jet-confirmation-modal wire:model="confirmingUserDeletion">
22+
<x-jet-dialog-modal wire:model="confirmingUserDeletion">
2323
<x-slot name="title">
2424
Delete Account
2525
</x-slot>
2626

2727
<x-slot name="content">
28-
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted.
28+
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted. Please enter your password to confirm you would like to permanently delete your account.
29+
30+
<div class="mt-4" x-data="{}" x-on:confirming-delete-user.window="setTimeout(() => $refs.password.focus(), 250)">
31+
<x-jet-input type="password" class="mt-1 block w-3/4" placeholder="Password"
32+
x-ref="password"
33+
wire:model.defer="password"
34+
wire:keydown.enter="deleteUser" />
35+
36+
<x-jet-input-error for="password" class="mt-2" />
37+
</div>
2938
</x-slot>
3039

3140
<x-slot name="footer">
@@ -37,6 +46,6 @@
3746
Delete Account
3847
</x-jet-danger-button>
3948
</x-slot>
40-
</x-jet-confirmation-modal>
49+
</x-jet-dialog-modal>
4150
</x-slot>
4251
</x-jet-action-section>

0 commit comments

Comments
 (0)