alexgeno/phone-verification-bundle
## Getting Started
### Minimal Setup
1. **Install the package** with dependencies:
```bash
composer require alexgeno/phone-verification-bundle snc/redis-bundle predis/predis symfony/vonage-notifier
(Replace symfony/vonage-notifier with your preferred SMS provider, e.g., symfony/twilio-notifier.)
Configure .env with required variables:
PHONE_VERIFICATION_OTP_LENGTH=4
PHONE_VERIFICATION_RATE_LIMIT_INITIATE_PERIOD_SECS=86400
PHONE_VERIFICATION_RATE_LIMIT_INITIATE_COUNT=10
PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_PERIOD_SECS=300
PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_COUNT=5
REDIS_URL=redis://localhost:6379
Verify auto-configuration (Flex recipe handles most setup). If not using Flex, manually:
AlexGeno\PhoneVerificationBundle\AlexGenoPhoneVerificationBundle::class to config/bundles.php.config/routes/alex_geno_phone_verification.yaml (or use the default Flex-generated ones).First use case: Trigger verification via DI:
use AlexGeno\PhoneVerification\Manager\Initiator;
public function register(Request $request, Initiator $initiator)
{
$phone = $request->input('phone');
$initiator->initiate($phone); // Sends OTP via SMS
}
Initiation Flow:
Initiator service to trigger OTP generation/sending.
$initiator->initiate('+1234567890');
php bin/console phone-verification:initiate --to=+1234567890
/phone-verification/initiate/{to} (POST).Completion Flow:
Completer.
$completer->complete('+1234567890', '1234'); // Returns bool
php bin/console phone-verification:complete --to=+1234567890 --otp=1234
/phone-verification/complete/{to}/{otp} (POST).Rate Limiting:
.env (e.g., 10 initiations/day, 5 failed completions/5 mins).RateLimitExceededException in logs.Storage Integration:
snc/redis-bundle for in-memory storage.
alex_geno_phone_verification:
storage:
driver: redis
redis:
connection: default
doctrine/mongodb-odm-bundle and manual index setup (see MIGRATIONS.md).
alex_geno_phone_verification:
storage:
driver: mongodb
mongodb:
connection: default
SMS Provider Switching:
vonage in config with your provider (e.g., twilio, messagebird).
alex_geno_phone_verification:
sender:
transport: twilio
.env (e.g., TWILIO_DSN=...).$request->session()->put('verified_phone', $phone);
PhoneVerificationSentEvent or PhoneVerificationCompletedEvent:
use AlexGeno\PhoneVerification\Event\PhoneVerificationSentEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class VerificationLogger implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
PhoneVerificationSentEvent::class => 'logVerification',
];
}
public function logVerification(PhoneVerificationSentEvent $event): void
{
// Log or notify (e.g., Slack) when OTP is sent.
}
}
Sender service (see Gotchas).MongoDB Indexes:
db.session.createIndex({"updated": 1}, { expireAfterSeconds: 300 }); // OTP expiry
expireAfterSeconds doesn’t match .env values, OTPs may expire prematurely or persist too long.
Fix: Recreate indexes after changing PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_PERIOD_SECS.Rate Limiting:
to). Test with multiple phones to avoid false positives.RateLimitExceededException to debug blocked requests.OTP Expiration:
.env: The complete rate limit period (PHONE_VERIFICATION_RATE_LIMIT_COMPLETE_PERIOD_SECS) also sets OTP expiry.
Example: 300 = 5-minute OTP validity.Redis Connection:
pv:1) may conflict with other bundles. Customize in config:
alex_geno_phone_verification:
storage:
redis:
settings:
prefix: myapp_pv:
SMS Provider Quirks:
TWILIO_DSN in .env (format: twilio://ACCOUNT_SID:TOKEN@host).VONAGE_DSN (format: vonage://KEY:SECRET@host).m6web/redis-mock) in tests to avoid real SMS costs.Enable Debug Mode:
// config/packages/dev/alex_geno_phone_verification.yaml
alex_geno_phone_verification:
debug: true
Logs detailed OTP generation/sending steps to Symfony’s debug log.
Check Storage Directly:
redis-cli KEYS pv:*.db.session.find().pretty(); // List pending OTPs
Common Errors:
snc/redis-bundle or doctrine/mongodb-odm-bundle is installed and configured.symfony/twilio-notifier) is installed.+1234567890), not 1234567890.Custom Storage:
Implement \AlexGeno\PhoneVerification\Storage\I and decorate the service:
# config/services.yaml
services:
AlexGeno\PhoneVerificationBundle\Storage\RedisStorage:
decorates: 'alex_geno_phone_verification.storage'
arguments:
$inner: '@.inner'
Use case: Add analytics to storage operations.
Custom Sender:
Implement \AlexGeno\PhoneVerification\Sender\I for non-Symfony Notifier providers (e.g., custom HTTP API):
class CustomSender implements SenderInterface {
public function send(string $to, string $message): void {
// Your logic (e.g., cURL to a SMS API)
}
}
Decorate the alex_geno_phone_verification.sender service.
Localization: Add translations for non-English SMS templates:
# config/packages/alex_geno_phone_verification.yaml
alex_geno_phone_verification:
locale: es # or 'fr', 'de', etc.
Note: Only en and es are bundled; extend via
How can I help you explore Laravel packages today?