ldc-cocotte/app/Http/Controllers/Fo/Auth/ResetPasswordController.php
Jérémy Munsch 47b0c16857
feat(auth): implement ResetCustomerPassword action and related classes
Adds functionality for resetting customer passwords, including input and output handling.
2026-11-15 19:57:38 +01:00

89 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Auth;
use App\Contracts\Executor;
use App\Cqs\Actions\Auth\ResetCustomerPassword;
use App\Cqs\Actions\Auth\ResetCustomerPasswordInput;
use App\Enums\ResetCustomerPasswordStatus;
use App\Exceptions\CustomerNotEmployedException;
use App\Models\Customer;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
use Webmozart\Assert\Assert;
class ResetPasswordController extends AuthController
{
use ResetsPasswords;
public function __construct(
private readonly Executor $executor,
private readonly ResetCustomerPassword $resetCustomerPassword,
) {
}
public function showResetForm(Request $request): View
{
$token = $request->route()?->parameter('token');
return view('fo.auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
protected function resetPassword(CanResetPassword $user, string $password): void
{
Assert::isInstanceOf($user, Customer::class);
$output = $this->executor->execute(
$this->resetCustomerPassword,
new ResetCustomerPasswordInput($user, $password),
);
match ($output->status) {
ResetCustomerPasswordStatus::NOT_EMPLOYED => throw new CustomerNotEmployedException(),
ResetCustomerPasswordStatus::INVALID_PASSWORD => throw ValidationException::withMessages([
'error' => sprintf(
'%s : %s',
trans('validation.attributes.password'),
$output->errorMessage ?? ''
),
]),
ResetCustomerPasswordStatus::SUCCESS => $this->afterPasswordReset($user, $output->emailVerified),
};
}
private function afterPasswordReset(Customer $user, bool $emailVerified): void
{
event(new PasswordReset($user));
if ($emailVerified) {
$this->guard()->login($user);
}
}
public function broker(): PasswordBroker
{
return Password::broker('customers');
}
protected function guard(): StatefulGuard
{
return Auth::guard('frontend');
}
protected function redirectTo(): string
{
return route('fo.pages.home');
}
}