81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Fo\Auth;
|
|
|
|
use App\Contracts\Directories\CustomerDirectory;
|
|
use App\Exceptions\CustomerNotEmployedException;
|
|
use App\Models\Customer;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Services\ShopperService;
|
|
use Illuminate\Contracts\Auth\PasswordBroker;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Password;
|
|
|
|
class ForgotPasswordController extends AuthController
|
|
{
|
|
use SendsPasswordResetEmails {
|
|
sendResetLinkEmail as traitSendResetLinkEmail;
|
|
}
|
|
|
|
public function __construct(
|
|
private readonly CustomerDirectory $customerDirectory,
|
|
private readonly ShopperService $shopperService
|
|
) {
|
|
$this->middleware('guest:frontend');
|
|
parent::__construct();
|
|
}
|
|
|
|
public function showLinkRequestForm(): View
|
|
{
|
|
return view('fo.auth.passwords.email');
|
|
}
|
|
|
|
public function sendResetLinkEmail(Request $request): JsonResponse | RedirectResponse
|
|
{
|
|
$this->validateEmail($request);
|
|
|
|
$customer = $this->customerDirectory
|
|
->getCustomerByEmail(new Email($request->string('email')->toString()));
|
|
|
|
if ($customer instanceof Customer) {
|
|
$shopper = $this->shopperService->getShopperFor($customer->getId());
|
|
if (! $shopper || ! $shopper->isActive()) {
|
|
throw new CustomerNotEmployedException();
|
|
}
|
|
}
|
|
|
|
return $this->traitSendResetLinkEmail($request);
|
|
}
|
|
|
|
protected function sendResetLinkResponse(Request $request, string $response): JsonResponse | RedirectResponse
|
|
{
|
|
return $this->sendResponse($request->wantsJson());
|
|
}
|
|
|
|
protected function sendResetLinkFailedResponse(Request $request, string $response): JsonResponse | RedirectResponse
|
|
{
|
|
return $this->sendResponse($request->wantsJson());
|
|
}
|
|
|
|
protected function sendResponse(bool $wantsJson): JsonResponse | RedirectResponse
|
|
{
|
|
$message = trans(
|
|
'Please check your email, if we know you you should receive a reset link'
|
|
);
|
|
|
|
return $wantsJson
|
|
? new JsonResponse(['message' => $message], 202)
|
|
: back()->with('status', $message);
|
|
}
|
|
|
|
public function broker(): PasswordBroker
|
|
{
|
|
return Password::broker('customers');
|
|
}
|
|
}
|