Phone number¶
Sign in with a code sent by SMS. Enable Phone in the console (Authentication > Sign-in method).
Before you start¶
Firebase checks that requests come from your app before it sends an SMS:
- Silent push (on a device): upload your APNs key in the console (Project settings > Cloud Messaging), and give the app the Push Notifications capability. Firebase then verifies the app with an invisible notification.
-
reCAPTCHA (the fallback, and the Simulator): Firebase shows a web page. It needs a callback URL scheme in
CFBundleURLTypes: theREVERSED_CLIENT_IDfromGoogleService-Info.plist, or, without one,app-+ yourGOOGLE_APP_IDwith-for::
Without the URL scheme
The Firebase SDK stops the app (NSException) when the scheme is missing.
pyios-firebase checks the scheme first, and gives an AuthError naming
the exact scheme to add.
Send the code, then sign in¶
from pyios_firebase.auth import Auth, PhoneAuthProvider
auth = Auth.auth()
phone = PhoneAuthProvider()
def send_code(number): # E.164: "+15555550100"
def on_code_sent(verification_id, error):
if error is not None:
return show(error.description)
save_on_device("verification_id", verification_id) # survives an app restart
show_code_field()
phone.verify_phone_number(number, on_code_sent)
def finish(code): # what the user typed
credential = phone.credential(load_from_device("verification_id"), code)
auth.sign_in_with_credential(credential, on_signed_in)
| Error | Means |
|---|---|
INVALID_PHONE_NUMBER |
Not a valid number (use the E.164 format, with + and the country code) |
INVALID_VERIFICATION_CODE |
The code is wrong |
SESSION_EXPIRED |
The code expired: send a new one |
QUOTA_EXCEEDED |
The project's SMS quota is used up |
TOO_MANY_REQUESTS |
Too many codes for this device |
Change a user's phone number¶
def on_code_sent(verification_id, error):
credential = phone.credential(verification_id, code)
auth.current_user.update_phone_number(credential, on_updated)
Testing¶
- Test numbers: in the console (Phone > Phone numbers for testing), add numbers with a fixed code. No SMS is sent and no quota is used.
-
The emulator sends no SMS, and needs no app verification. The codes it would have sent are on its REST API:
-
auth.app_verification_disabled_for_testing = Trueskips app verification for test numbers, against a real project.
When Firebase app delegate swizzling is off¶
With FirebaseAppDelegateProxyEnabled = NO in Info.plist, forward the
APNs token, notifications and URLs to Auth yourself: