ldc-cocotte/app/Http/Controllers/Fo/Auth/ResetPasswordController.php

90 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?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;
2026-06-14 21:32:07 +02:00
use App\Models\Customer;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\Auth\CanResetPassword;
2026-06-14 21:32:07 +02:00
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;
2026-06-14 21:32:07 +02:00
class ResetPasswordController extends AuthController
{
use ResetsPasswords;
public function __construct(
private readonly Executor $executor,
private readonly ResetCustomerPassword $resetCustomerPassword,
2026-06-14 21:32:07 +02:00
) {
}
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),
);
2026-06-14 21:32:07 +02:00
match ($output->status) {
ResetCustomerPasswordStatus::NOT_EMPLOYED => throw new CustomerNotEmployedException(),
ResetCustomerPasswordStatus::INVALID_PASSWORD => throw ValidationException::withMessages([
2026-06-14 21:32:07 +02:00
'error' => sprintf(
'%s : %s',
trans('validation.attributes.password'),
$output->errorMessage ?? ''
2026-06-14 21:32:07 +02:00
),
]),
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);
2026-06-14 21:32:07 +02:00
}
}
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');
}
}