question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(authentication): implement `signInWithPhoneNumber` for web

See original GitHub issue

Is your feature request related to a problem? Please describe:

signInWithPhoneNumber is currently not supported on the web.

Describe the solution you’d like:

Add support for signInWithPhoneNumber.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maxbarrycommented, Oct 27, 2022

Thanks for the guidance. I have it working! For anyone looking, below is an Ionic + React implementation that provides phone sign-in on both native devices and the web, and signs the user into both layers of Firebase authentication.

import { useState, useEffect } from 'react'
import { Capacitor } from '@capacitor/core'
import { signInWithCredential, signInWithPhoneNumber, PhoneAuthProvider, RecaptchaVerifier } from 'firebase/auth'
import { useIonAlert, IonButton } from '@ionic/react'
import { FirebaseAuthentication } from '@capacitor-firebase/authentication'
import { getFirebaseAuth } from './firebase'    // As per https://github.com/firebase/firebase-js-sdk/issues/5019#issuecomment-1213244332

const Parent = () => (
    <>
        ...
        <PhoneNumberModal />
        <div id="recaptcha-container" />
    </>
)

const PhoneNumberModal = props => {

    const [ phoneNumber, setPhoneNumber ] = useState()     // Example: "+12133734253".
    const [ recaptchaVerifier, setRecaptchaVerifier ] = useState()
    const [ presentAlert ] = useIonAlert()

    useEffect(() => {
        if (!recaptchaVerifier && !Capacitor.isNativePlatform()) {
            // Note: The 'recaptcha-container' must be rendered by this point, or
            // else Firebase throws 'auth/argument-error'
            const auth = getFirebaseAuth()
            const rv = new RecaptchaVerifier('recaptcha-container', { size: 'invisible' }, auth)
            setRecaptchaVerifier(rv)
        }
    }, [ recaptchaVerifier ])

    const proceedWithVerificationCode = (verificationId) => {
        presentAlert({
            header: 'Please enter the verification code sent to your phone.',
            onWillDismiss: async (e) => {
                try {
                    const verificationCode = e.detail.data.values[0]
                    if (verificationCode) {
                        const credential = PhoneAuthProvider.credential(verificationId, verificationCode)
                        const auth = getFirebaseAuth()
                        await signInWithCredential(auth, credential)
                        // Success!
                    }
                } catch (err) {
                    // Failure!
                }
            },
            buttons: [ 'OK' ],
            inputs: [
                {
                    type: 'number',
                    placeholder: 'Enter code',
                },
            ],
        })
    }

    const onClick = async () => {
        let verificationId
        try {
            let result
            if (Capacitor.isNativePlatform()) {
                result = await FirebaseAuthentication.signInWithPhoneNumber({ phoneNumber })
            } else {
                result = await signInWithPhoneNumber(getFirebaseAuth(), phoneNumber, recaptchaVerifier)
            }
            verificationId = result?.verificationId
        } catch (err) { /* Error! */ }
        if (verificationId) {
            proceedWithVerificationCode(verificationId)
        }
    }

    return (
        <>
            <h1>Sign in with phone</h1>
            <PhoneInput
                placeholder="Enter phone number"
                value={phoneNumber}
                onChange={setPhoneNumber}
            />
            <IonButton onClick={onClick}>Verify</IonButton>
        </>
    )
}
0reactions
robingenzcommented, Dec 11, 2022

@alexookah Right. ETA is this year.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authenticate with Firebase with a Phone Number Using ...
You can use Firebase Authentication to sign in a user by sending an SMS message to the user's phone. The user signs in...
Read more >
[firebase_auth] web signInWithPhoneNumber method throws ...
Successfully merging a pull request may close this issue. feat(firebase_auth): implement signInWithPhoneNumber on web firebase/flutterfire. 3 ...
Read more >
firebase_auth 0.18.3+1 | Flutter Package - Pub.dev
Flutter plugin for Firebase Auth, enabling Android and iOS authentication using passwords, ... FEAT: implement signInWithPhoneNumber on web (#3205).
Read more >
Reactjs Firebase Auth Phone sign up flow? - Stack Overflow
I'm transitioning from Email+Password & integrating Firebase Auth signInWithPhoneNumber into my React web app & I'm confused on how to go ...
Read more >
Phone Authentication - FlutterFire
Phone authentication allows users to sign in to Firebase using their ... Note; Phone number sign-in is only available for use on real...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found