Added a new feature to allow users to update their passwords.

This commit is contained in:
Leonid Nikitin 2023-07-07 18:08:14 +06:00
parent 00a0624eea
commit 39eae7f196
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
7 changed files with 73 additions and 3 deletions

View File

@ -0,0 +1,17 @@
<?php declare(strict_types=1);
namespace App\Dto\Request\Private\Profile;
use App\Dto\Request\Dto;
final readonly class UpdatePassword extends Dto
{
public function __construct(
private string $password
) { }
public function getPassword(): string
{
return $this->password;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Private;
use App\Http\Requests\Private\Profile\UpdatePasswordRequest;
use App\Http\Requests\Private\Profile\UpdateRequest;
use App\Services\Private\ProfileService;
use Illuminate\Http\RedirectResponse;
@ -39,4 +40,16 @@ final class ProfileController extends Controller
}
return redirect()->route('profile.edit')->withSuccess($result->getMessage());
}
public function updatePassword(UpdatePasswordRequest $request): RedirectResponse
{
$data = $request->getDto();
$user = $request->user();
$result = $this->profileService->updatePassword($data, $user);
if ($result->isError()) {
return redirect()->back()->withInput()->withErrors($result->getMessage());
}
return redirect()->route('profile.edit')->withSuccess($result->getMessage());
}
}

View File

@ -0,0 +1,26 @@
<?php declare(strict_types=1);
namespace App\Http\Requests\Private\Profile;
use App\Contracts\FormRequestDto;
use App\Dto\Request\Private\Profile\UpdatePassword;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
final class UpdatePasswordRequest extends FormRequest implements FormRequestDto
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'password' => ['required', 'confirmed', Password::default()],
];
}
public function getDto(): UpdatePassword
{
return new UpdatePassword(password: $this->input('password'));
}
}

View File

@ -3,6 +3,7 @@
namespace App\Services\Private;
use App\Dto\Request\Private\Profile\Update;
use App\Dto\Request\Private\Profile\UpdatePassword;
use App\Models\User;
use App\ServiceResults\ServiceResultError;
use App\ServiceResults\ServiceResultSuccess;
@ -28,4 +29,15 @@ final class ProfileService extends Service
}
return $this->ok(__('Profile saved successfully'));
}
public function updatePassword(UpdatePassword $update, User $user): ServiceResultError | ServiceResultSuccess
{
try {
$this->userCommandHandler->handleUpdatePassword($user, $update->getPassword());
} catch (\Throwable $e) {
report($e->getMessage());
return $this->errService($e->getMessage());
}
return $this->ok(__('The password has been changed'));
}
}

View File

@ -44,5 +44,6 @@
"Settings": "Settings",
"Dashboard": "Dashboard",
"Save": "Save",
"Profile saved successfully": "Profile saved successfully"
"Profile saved successfully": "Profile saved successfully",
"The password has been changed": "The password has been changed"
}

View File

@ -44,5 +44,6 @@
"Settings": "Настройки",
"Dashboard": "Dashboard",
"Save": "Сохранить",
"Profile saved successfully": "Профиль успешно сохранен"
"Profile saved successfully": "Профиль успешно сохранен",
"The password has been changed": "Пароль был изменен"
}

View File

@ -20,7 +20,7 @@
<div class="col-12 mb-4">
<div class="card border-0 shadow components-section">
<div class="card-body">
<form method="post" {{ route('profile.update-password') }}>
<form method="post" action="{{ route('profile.update-password') }}">
@csrf
@method('PUT')
<x-private.forms.input :title="__('validation.attributes.password')" name="password" type="password" required />