Prerequisites:
composer require livewire/livewirecomposer require doctrine/dbalInstall Package:
composer require lee-to/laravel-phone-auth
php artisan vendor:publish --provider="Leeto\PhoneAuth\Providers\PhoneAuthServiceProvider"
config/phone_auth.php) and update as needed.User Model Setup:
PhoneVerification trait and PhoneCast to your User model:
use Leeto\PhoneAuth\Traits\PhoneVerification;
use Leeto\PhoneAuth\Casts\PhoneCast;
protected $casts = [
'phone' => PhoneCast::class,
];
First Use Case:
@livewire('phone-verification')
User Input:
@livewire('phone-verification') component renders a form where users input their phone number.PhoneCast.OTP Generation and Delivery:
config/phone_auth.php).'sms' => [
'provider' => 'twilio',
'twilio' => [
'sid' => env('TWILIO_SID'),
'token' => env('TWILIO_TOKEN'),
'from' => env('TWILIO_FROM'),
],
],
OTP Verification:
Post-Verification Actions:
customRedirectTo property or default config.loginAndRegister property to allow users to either log in (if their phone is already verified) or register (if not):
@livewire('phone-verification', ['loginAndRegister' => true])
Custom Fields:
emptyCustomFields:
@livewire('phone-verification', ['emptyCustomFields' => true])
@livewire('phone-verification', [
'customParams' => [
'btn' => 'Verify My Number',
'title' => 'Phone Verification',
]
])
Event Handling:
emitBefore, emitAfter) with stopEvents:
@livewire('phone-verification', ['stopEvents' => true])
Redirect Logic:
@livewire('phone-verification', ['customRedirectTo' => '/dashboard'])
Livewire Hooks:
verified event to trigger additional logic:
Livewire::hook('phone.verified', function ($user) {
// Custom logic after verification
});
Database Schema:
users table has a phone column (e.g., phone_number or phone). The PhoneCast handles formatting, but the column should store raw numbers (e.g., 1234567890).Testing:
fake method for SMS:
$this->fake();
$this->actingAs($user);
$response = $this->post('/verify-phone', ['phone' => '1234567890']);
Multi-Language Support:
config/phone_auth.php to support multiple languages:
'sms' => [
'templates' => [
'en' => 'Your verification code is {code}.',
'es' => 'Su código de verificación es {code}.',
],
],
Rate Limiting:
throttle middleware or a package like spatie/rate-limiter.SMS Provider Configuration:
config/phone_auth.php will result in silent failures. Always verify your provider settings (e.g., Twilio credentials, AWS keys).Phone Number Format:
PhoneCast expects raw phone numbers (e.g., 1234567890). Storing formatted numbers (e.g., +1 (123) 456-7890) may cause issues during verification.Livewire Component Caching:
phone-verification component’s view, clear Livewire’s view cache:
php artisan livewire:discover
User Model Not Updated:
PhoneVerification trait or PhoneCast to your User model will break phone verification functionality.User model’s uses and casts properties.OTP Expiry:
verified_at column in your users table and validate OTPs against this timestamp.Tailwind CSS Dependencies:
Log OTPs:
config/phone_auth.php:
'debug' => [
'log_otp' => true,
],
storage/logs/laravel.log for OTP details.Verify Webhook Responses:
\Log::info('SMS Provider Response', ['response' => $response]);
Database Conflicts:
users table or conflicts in the confirmed_phones table (created by the ConfirmedPhone model).Livewire Wire:ignore:
wire:ignore. The component expects dynamic updates.Custom SMS Providers:
Leeto\PhoneAuth\Services\SmsService class or bind a custom implementation in the service provider:
$this->app->bind(
\Leeto\PhoneAuth\Contracts\SmsService::class,
\App\Services\CustomSmsService::class
);
Custom Verification Logic:
PhoneVerification trait or the PhoneVerificationController:
namespace App\Traits;
use Leeto\PhoneAuth\Traits\PhoneVerification as BasePhoneVerification;
trait PhoneVerification extends BasePhoneVerification {
public function verifyPhone($otp) {
// Custom verification logic
}
}
Event Listeners:
phone.verified: Triggered after successful verification.phone.verification.failed: Triggered on verification failure.use Leeto\PhoneAuth\Events\PhoneVerified;
PhoneVerified::listen(function (PhoneVerified $event) {
// Send welcome email or notify admin
});
Custom Templates:
php artisan vendor:publish --tag=phone-auth-views
How can I help you explore Laravel packages today?