feat(auth): add optional domain parameter to send OTP code for site personalization
This commit is contained in:
@@ -142,6 +142,7 @@ class AuthController extends BaseController
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$mobile = trim($data['mobile'] ?? '');
|
||||
$domain = isset($data['domain']) ? substr(trim((string) $data['domain']), 0, 253) : null;
|
||||
|
||||
if (!preg_match('/^09[0-9]{9}$/', $mobile)) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'فرمت شماره موبایل نادرست است', 422, 'mobile');
|
||||
@@ -154,7 +155,7 @@ class AuthController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_RATE_LIMIT_001, ErrorCodes::message(ErrorCodes::ERR_RATE_LIMIT_001), 429);
|
||||
}
|
||||
|
||||
$uuid = $this->otpService->sendCode($mobile);
|
||||
$uuid = $this->otpService->sendCode($mobile, $domain ?: null);
|
||||
|
||||
return new JsonResponse(['uuid' => $uuid, 'message' => 'کد تایید با موفقیت ارسال شد.']);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Auth\Service;
|
||||
|
||||
use App\Location\Repository\CityRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Exception\AppException;
|
||||
use App\Sms\Entity\SmsLog;
|
||||
@@ -16,6 +17,7 @@ class OtpService
|
||||
private readonly CacheInterface $cache,
|
||||
private readonly SmsService $sms,
|
||||
private readonly SmsTextResolver $smsText,
|
||||
private readonly CityRepository $cityRepo,
|
||||
private readonly int $otpTtl = 1200,
|
||||
private readonly string $appEnv = 'dev',
|
||||
) {}
|
||||
@@ -56,7 +58,7 @@ class OtpService
|
||||
return $mobile;
|
||||
}
|
||||
|
||||
public function sendCode(string $mobile): string
|
||||
public function sendCode(string $mobile, ?string $domain = null): string
|
||||
{
|
||||
$uuid = Uuid::v4()->toRfc4122();
|
||||
$code = $this->appEnv === 'dev'
|
||||
@@ -69,13 +71,28 @@ class OtpService
|
||||
$this->cache->save($item);
|
||||
|
||||
if ($this->appEnv !== 'dev') {
|
||||
$message = $this->smsText->resolve(SmsLog::TAG_OTP, ['code' => $code]);
|
||||
$site = $this->resolveSiteName($domain);
|
||||
$message = $this->smsText->resolve(SmsLog::TAG_OTP, ['code' => $code, 'site' => $site]);
|
||||
$this->sms->dispatchAsync($mobile, $message, tag: SmsLog::TAG_OTP);
|
||||
}
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
|
||||
private function resolveSiteName(?string $domain): string
|
||||
{
|
||||
$default = 'کلینیک پرو';
|
||||
if (!$domain) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$domain = strtolower(trim((string) preg_replace('#^https?://#', '', $domain)));
|
||||
$domain = (string) preg_replace('#/.*$#', '', $domain);
|
||||
$domain = (string) preg_replace('#^www\.#', '', $domain);
|
||||
|
||||
return $this->cityRepo->findOneBy(['domain' => $domain])?->getSiteName() ?: $default;
|
||||
}
|
||||
|
||||
public function verifyCode(string $uuid, string $submittedCode): array
|
||||
{
|
||||
$item = $this->cache->getItem($this->key($uuid));
|
||||
|
||||
@@ -21,7 +21,7 @@ class SmsMessageTemplate
|
||||
SmsLog::TAG_OTP => [
|
||||
'title' => 'کد تأیید ورود',
|
||||
'body' => 'کد تأیید شما: {code}',
|
||||
'variables' => ['code'],
|
||||
'variables' => ['code', 'site'],
|
||||
],
|
||||
SmsLog::TAG_PAYMENT => [
|
||||
'title' => 'تأیید پرداخت و نوبت',
|
||||
|
||||
Reference in New Issue
Block a user