105 lines
3.1 KiB
PHP
105 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Fo\Auth;
|
|
|
|
use App\Models\Customer;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Auth\Events\Verified;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Foundation\Auth\VerifiesEmails;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Str;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
class VerificationController extends AuthController
|
|
{
|
|
use VerifiesEmails;
|
|
|
|
public function __construct(
|
|
private AppCustomerAuthService $authService
|
|
) {
|
|
$this->middleware('auth:frontend');
|
|
$this->middleware('is-employed');
|
|
$this->middleware('signed:relative')->only('verify');
|
|
$this->middleware('throttle:6,1')->only(['verify', 'resend']);
|
|
parent::__construct();
|
|
}
|
|
|
|
public function show(Request $request): RedirectResponse | View
|
|
{
|
|
$customer = $this->getRequestCustomerFrom($request);
|
|
$isActivated = $this->authService->hasVerifiedEmail($customer);
|
|
|
|
return $isActivated
|
|
? redirect($this->redirectPath())
|
|
: view('fo.auth.verify');
|
|
}
|
|
|
|
public function verify(Request $request): View | RedirectResponse
|
|
{
|
|
$customer = $this->getRequestCustomerFrom($request);
|
|
$requestId = Str::of($request->route('id'));
|
|
$requestHash = Str::of($request->route('hash'));
|
|
|
|
throw_unless(
|
|
hash_equals($requestId->value(), strval($customer->id)),
|
|
AuthorizationException::class
|
|
);
|
|
|
|
throw_unless(
|
|
hash_equals(
|
|
$requestHash->value(),
|
|
hash_hmac(
|
|
'sha256',
|
|
$customer->email->value,
|
|
Config::string('app.key')
|
|
)
|
|
),
|
|
AuthorizationException::class
|
|
);
|
|
|
|
if ($this->authService->hasVerifiedEmail($customer)) {
|
|
return redirect($this->redirectPath());
|
|
}
|
|
|
|
if ($this->authService->verifyEmail($customer)) {
|
|
event(new Verified($customer));
|
|
|
|
return redirect($this->redirectPath())
|
|
->with('success', trans('Your account is now verified, you can start using the site.'));
|
|
}
|
|
|
|
return view('fo.auth.verify')->with(
|
|
'error',
|
|
trans('The verification link is invalid or the customer account was not found.')
|
|
);
|
|
}
|
|
|
|
public function resend(Request $request): RedirectResponse
|
|
{
|
|
$customer = $this->getRequestCustomerFrom($request);
|
|
$isActivated = $this->authService->hasVerifiedEmail($customer);
|
|
|
|
if ($isActivated) {
|
|
return redirect($this->redirectPath());
|
|
}
|
|
|
|
$customer->sendEmailVerificationNotification();
|
|
|
|
return back()->with('resent', true);
|
|
}
|
|
|
|
protected function redirectTo(): string
|
|
{
|
|
return route('fo.pages.home');
|
|
}
|
|
|
|
private function getRequestCustomerFrom(Request $request): Customer
|
|
{
|
|
return $request->user('frontend') ?? throw new AuthorizationException('Unauthenticated.');
|
|
}
|
|
}
|