Phone Number Authentication Guide
Implementing phone number authentication with Firebase in Flutter, including testing considerations and best practices.
Phone Number Authentication
This guide builds upon our Firebase Auth Reference to implement phone number authentication in your Flutter app.
Prerequisites
- Firebase project setup with Phone Authentication enabled
- SHA-1 and SHA-256 fingerprints added to your Firebase project
- Firebase configuration files (
google-services.json
for Android,GoogleService-Info.plist
for iOS)
Implementation Steps
1. Update IAuthService Interface
First, extend the IAuthService
interface to include phone authentication methods:
2. Firebase Implementation
Here’s how to implement phone authentication in your FirebaseAuthService
:
Testing Considerations
1. Test Phone Numbers
Firebase provides test phone numbers that don’t require actual SMS verification:
- For Android: Use test phone numbers with the format
+1 650-555-XXXX
- For iOS: Use test phone numbers with the format
+1 650-555-XXXX
Example test numbers:
2. Emulator Testing
When testing on emulators:
-
Android Emulator:
- Use the emulator’s built-in SMS receiver
- OTPs will be automatically detected and filled
- No need for actual SMS verification
-
iOS Simulator:
- Use test phone numbers
- OTPs will be logged in the console
- No need for actual SMS verification
3. Production Testing
Before going to production:
- Enable phone authentication in Firebase Console
- Add your app’s SHA-1 and SHA-256 fingerprints
- Test with real phone numbers in a staging environment
- Monitor Firebase Console for any verification issues
Common Issues and Solutions
-
Verification Failed
- Check if phone authentication is enabled in Firebase Console
- Verify SHA-1/SHA-256 fingerprints are correctly added
- Ensure proper Firebase configuration files are included
-
OTP Not Received
- Check if the phone number format is correct (include country code)
- Verify the phone number is not blocked by Firebase
- Check Firebase Console for any rate limiting issues
-
Auto-retrieval Not Working
- Ensure proper permissions in Android/iOS configuration
- Check if the app has SMS reading permissions
- Verify the phone number format matches the device’s region
Best Practices
-
Error Handling
- Implement proper error handling for all authentication flows
- Show user-friendly error messages
- Log errors for debugging purposes
-
User Experience
- Show loading states during verification
- Implement proper timeout handling
- Provide clear instructions for OTP entry
-
Security
- Implement rate limiting on your backend
- Use reCAPTCHA for suspicious activities
- Monitor for abuse patterns
Example Usage
First, add the flutter_otp_text_field
package to your pubspec.yaml
:
📦 Package Reference: flutter_otp_text_field on pub.dev
Then run:
Here’s how to implement the OTP input field using flutter_otp_text_field
:
OTPTextField Properties
The OTPTextField
widget provides several customization options:
-
Basic Properties:
length
: Number of OTP digits (default: 6)width
: Total width of the OTP fieldfieldWidth
: Width of each digit fieldstyle
: Text style for the digitsfieldStyle
: Style of the input fields (box, underline, or none)
-
Appearance:
outlineBorderRadius
: Border radius for box stylefieldStyle
: Choose between box, underline, or noneinputDecoration
: Custom decoration for each fieldkeyboardType
: Type of keyboard to show
-
Functionality:
onCompleted
: Callback when all digits are enteredonChanged
: Callback when any digit changesautoFocus
: Whether to focus the first field automatically
-
Validation:
validator
: Custom validation functionerrorText
: Error message to display
Example with custom styling: