0
0
Firebasecloud~5 mins

Phone number authentication in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Phone number authentication lets users sign in using their phone number. It sends a code via SMS to verify the user without needing a password.
When you want users to sign in quickly without creating a password.
When you want to verify users by their phone number for security.
When your app targets users who prefer SMS verification over email.
When you want to reduce friction in the sign-in process on mobile devices.
When you want to support multi-factor authentication using phone numbers.
Config File - firebase.json
firebase.json
{
  "hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
  },
  "auth": {
    "signInMethods": ["phone"]
  }
}

This file configures Firebase hosting and enables phone sign-in method under authentication settings.

hosting.public: folder for web files.

auth.signInMethods: enables phone number sign-in.

Commands
Log in to your Firebase account to access your projects and deploy configurations.
Terminal
firebase login
Expected OutputExpected
✔ Success! Logged in as user@example.com
Initialize Firebase Authentication in your project to enable phone number sign-in.
Terminal
firebase init auth
Expected OutputExpected
=== Authentication Setup === ✔ Enabled phone sign-in method Firebase Authentication has been initialized.
Deploy the authentication configuration to Firebase to activate phone number authentication.
Terminal
firebase deploy --only auth
Expected OutputExpected
=== Deploying to 'your-project-id' === ✔ auth: Finished running predeploy script. ✔ auth: deployed phone sign-in method ✔ Deploy complete!
--only - Deploy only the specified Firebase feature, here authentication.
Start the phone number sign-in process by sending an SMS code to the specified number.
Terminal
firebase auth:sign-in-with-phone-number +12345678901
Expected OutputExpected
Verification code sent to +12345678901
Key Concept

If you remember nothing else from this pattern, remember: phone number authentication verifies users by sending a code via SMS to their phone.

Common Mistakes
Not enabling phone sign-in method in Firebase console or config.
The authentication will fail because Firebase does not allow phone sign-in without enabling it.
Always enable phone sign-in method in Firebase console or via configuration before deploying.
Using an invalid or incorrectly formatted phone number.
Firebase will not send the verification code if the phone number format is wrong.
Use E.164 format for phone numbers, e.g., +12345678901.
Not deploying authentication changes after updating config.
Changes won't take effect until deployed, so phone sign-in won't work.
Run 'firebase deploy --only auth' after config changes.
Summary
Log in to Firebase and initialize authentication with phone sign-in enabled.
Deploy the authentication configuration to activate phone number sign-in.
Start the sign-in process by sending an SMS verification code to the user's phone.