laravel-otp maintained by itsmurumba
Laravel OTP
A flexible and feature-rich Laravel package for generating and sending One-Time Passwords (OTP) through multiple channels including Email, SMS, WhatsApp, Telegram, and Slack.
Features
- 📱 Multiple delivery channels (Email, SMS, WhatsApp, Telegram, Slack)
- 🔢 Configurable OTP formats (numeric, alphanumeric)
- ⏱️ Customizable expiration time
- 🔄 Easy channel switching
- ✅ Channel-specific validation
- 🛠️ Extensible architecture
Requirements
- PHP 8.1 or higher
- Laravel 10, 11, 12, or 13 (CI is tested against 12 and 13)
Installation
You can install the package via composer:
composer require itsmurumba/laravel-otp
After installation, publish the configuration file:
php artisan vendor:publish --provider="Itsmurumba\Otp\OtpServiceProvider"
Configuration
Copy the required environment variables from .env.example to your .env file and configure them according to your needs:
cp .env.example .env
The package supports the following channels, each requiring specific configurations:
- Requires Laravel mail settings
- Uses standard Laravel mail configuration
SMS
- Requires SMS API credentials
- Configure
SMS_API_KEYandSMS_API_URL
- Requires WhatsApp Business API credentials
- Configure
WHATSAPP_API_KEYandWHATSAPP_API_URL
Telegram
- Requires Telegram Bot Token
- Configure
TELEGRAM_BOT_TOKEN
Slack
- Requires Slack Webhook URL
- Configure
SLACK_WEBHOOK_URL
Usage
Basic Usage
use Itsmurumba\Otp\Facades\Otp;
// Generate and send an OTP (defaults to the "sms" channel)
$otp = Otp::generateAndSend('recipient@example.com', 'email');
// Verify an OTP
$isValid = Otp::verify('recipient@example.com', $otp);
You can also resolve the underlying service directly instead of the facade (via the otp() helper or the otp container binding):
$otpService = otp(); // same as app('otp')
$otp = $otpService->length(6)
->expiresIn(10)
->rateLimit(3, 15)
->generateAndSend('recipient@example.com', 'email');
$isValid = $otpService->verify('recipient@example.com', $otp);
Channel-Specific Usage
use Itsmurumba\Otp\Channels\EmailChannel;
$channel = new EmailChannel();
$result = $channel->send('user@example.com', '123456');
SMS
use Itsmurumba\Otp\Channels\SmsChannel;
$channel = new SmsChannel();
$result = $channel->send('+1234567890', '123456');
use Itsmurumba\Otp\Channels\WhatsAppChannel;
$channel = new WhatsAppChannel();
$result = $channel->send('+1234567890', '123456');
Telegram
use Itsmurumba\Otp\Channels\TelegramChannel;
$channel = new TelegramChannel();
$result = $channel->send('chat_id', '123456');
Slack
use Itsmurumba\Otp\Channels\SlackChannel;
$channel = new SlackChannel();
$result = $channel->send('#channel-name', '123456');
Sending via Multiple Channels
Pass an array of channel names to send the same OTP through more than one channel:
$otp = $otpService->generateAndSend('+1234567890', ['sms', 'whatsapp']);
OTP Verification
$isValid = $otpService->verify('+1234567890', $otp);
verify() checks the code against the stored, non-expired, unverified OTP for that identifier and marks it as verified on success.
Testing
composer test
To run the test suite with code coverage (requires Xdebug or PCOV):
composer test:coverage
Security
If you discover any security-related issues, please email kevmurumba@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.