2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Fo\Auth;
|
|
|
|
|
|
2026-11-15 19:57:38 +01:00
|
|
|
use App\Contracts\Executor;
|
|
|
|
|
use App\Cqs\Actions\Auth\ResetCustomerPassword;
|
|
|
|
|
use App\Cqs\Actions\Auth\ResetCustomerPasswordInput;
|
|
|
|
|
use App\Enums\ResetCustomerPasswordStatus;
|
2026-11-14 22:55:40 +01:00
|
|
|
use App\Exceptions\CustomerNotEmployedException;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Models\Customer;
|
2026-11-14 22:55:40 +01:00
|
|
|
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;
|
2026-11-14 22:55:40 +01:00
|
|
|
use Webmozart\Assert\Assert;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
class ResetPasswordController extends AuthController
|
|
|
|
|
{
|
|
|
|
|
use ResetsPasswords;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
2026-11-15 19:57:38 +01:00
|
|
|
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]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 22:55:40 +01:00
|
|
|
protected function resetPassword(CanResetPassword $user, string $password): void
|
|
|
|
|
{
|
|
|
|
|
Assert::isInstanceOf($user, Customer::class);
|
|
|
|
|
|
2026-11-15 19:57:38 +01:00
|
|
|
$output = $this->executor->execute(
|
|
|
|
|
$this->resetCustomerPassword,
|
|
|
|
|
new ResetCustomerPasswordInput($user, $password),
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-15 19:57:38 +01: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'),
|
2026-11-15 19:57:38 +01:00
|
|
|
$output->errorMessage ?? ''
|
2026-06-14 21:32:07 +02:00
|
|
|
),
|
2026-11-15 19:57:38 +01: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');
|
|
|
|
|
}
|
|
|
|
|
}
|