Skip to content

Delete User password confirmation #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Http/Controllers/Inertia/CurrentUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Contracts\DeletesUsers;

class CurrentUserController extends Controller
Expand All @@ -18,6 +20,12 @@ class CurrentUserController extends Controller
*/
public function destroy(Request $request, StatefulGuard $auth)
{
if (! Hash::check($request->password, $request->user()->password)) {
throw ValidationException::withMessages([
'password' => [__('This password does not match our records.')],
])->errorBag('deleteUser');
}

app(DeletesUsers::class)->delete($request->user()->fresh());

$auth->logout();
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Livewire/DeleteUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Contracts\DeletesUsers;
use Livewire\Component;

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

/**
* The user's current password.
*
* @var string
*/
public $password = '';

/**
* Confirm that the user would like to delete their account.
*
* @return void
*/
public function confirmDelete()
{
$this->password = '';

$this->dispatchBrowserEvent('confirming-delete-user');

$this->confirmingUserDeletion = true;
}

/**
* Delete the current user.
*
Expand All @@ -25,6 +48,14 @@ class DeleteUserForm extends Component
*/
public function deleteUser(DeletesUsers $deleter, StatefulGuard $auth)
{
$this->resetErrorBag();

if (! Hash::check($this->password, Auth::user()->password)) {
throw ValidationException::withMessages([
'password' => [__('This password does not match our records.')],
]);
}

$deleter->delete(Auth::user()->fresh());

$auth->logout();
Expand Down
30 changes: 24 additions & 6 deletions stubs/inertia/resources/js/Pages/Profile/DeleteUserForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@
</template>

<template #content>
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted.
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.

<div class="mt-4">
<jet-input type="password" class="mt-1 block w-3/4" placeholder="Password"
ref="password"
v-model="form.password"
@keyup.enter.native="deleteUser" />

<jet-input-error :message="form.error('password')" class="mt-2" />
</div>
</template>

<template #footer>
<jet-secondary-button @click.native="confirmingUserDeletion = false">
Nevermind
</jet-secondary-button>

<jet-danger-button class="ml-2" @click.native="deleteTeam" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
<jet-danger-button class="ml-2" @click.native="deleteUser" :class="{ 'opacity-25': form.processing }" :disabled="form.processing">
Delete Account
</jet-danger-button>
</template>
Expand All @@ -48,6 +57,8 @@
import JetButton from './../../Jetstream/Button'
import JetConfirmationModal from './../../Jetstream/ConfirmationModal'
import JetDangerButton from './../../Jetstream/DangerButton'
import JetInput from './../../Jetstream/Input'
import JetInputError from './../../Jetstream/InputError'
import JetSecondaryButton from './../../Jetstream/SecondaryButton'

export default {
Expand All @@ -56,6 +67,8 @@
JetButton,
JetConfirmationModal,
JetDangerButton,
JetInput,
JetInputError,
JetSecondaryButton,
},

Expand All @@ -65,7 +78,8 @@
deleting: false,

form: this.$inertia.form({
//
'_method': 'DELETE',
password: '',
}, {
bag: 'deleteUser'
})
Expand All @@ -77,10 +91,14 @@
this.confirmingUserDeletion = true
},

deleteTeam() {
this.form.delete('/user', {
deleteUser() {
this.form.post('/user', {
preserveScroll: true
});
}).then(response => {
if (! this.form.hasErrors()) {
this.confirmingUserDeletion = false
}
})
},
},
}
Expand Down
15 changes: 12 additions & 3 deletions stubs/livewire/resources/views/profile/delete-user-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@
</div>

<!-- Delete User Confirmation Modal -->
<x-jet-confirmation-modal wire:model="confirmingUserDeletion">
<x-jet-dialog-modal wire:model="confirmingUserDeletion">
<x-slot name="title">
Delete Account
</x-slot>

<x-slot name="content">
Are you sure you want to delete your account? Once your account is deleted, all of its resources and data will be permanently deleted.
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.

<div class="mt-4" x-data="{}" x-on:confirming-delete-user.window="setTimeout(() => $refs.password.focus(), 250)">
<x-jet-input type="password" class="mt-1 block w-3/4" placeholder="Password"
x-ref="password"
wire:model.defer="password"
wire:keydown.enter="deleteUser" />

<x-jet-input-error for="password" class="mt-2" />
</div>
</x-slot>

<x-slot name="footer">
Expand All @@ -37,6 +46,6 @@
Delete Account
</x-jet-danger-button>
</x-slot>
</x-jet-confirmation-modal>
</x-jet-dialog-modal>
</x-slot>
</x-jet-action-section>