1

I know that I can reauthenticate with email like this.

var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credentials);

But how can I reauthenticate if I use phoneNumber as my sign in method?

KENdi
  • 7,576
  • 2
  • 16
  • 31
rendom
  • 3,417
  • 6
  • 38
  • 49
  • another way is to just logout the user and redirect to login page https://stackoverflow.com/a/62563840/3073272 – GorvGoyl Jun 24 '20 at 20:49

2 Answers2

1

It is very similar to how you sign in with phone number. You can do it in 2 ways:

Using reauthenticateWithPhoneNumber:

const recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
    container, parameters, firebase.app());
recaptchaVerifier.render();
user.reauthenticateWithPhoneNumber(user.phoneNumber, recaptchaVerifier)
  .then((confirmationResult) => {
    return confirmationResult.confirm(prompt('Enter your SMS code'));
  })
  .then((userCredential) => {
    // User successfully reauthenticated.
  });

Using reauthenticateWithCredential:

const recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
    container, parameters, firebase.app());
recaptchaVerifier.render();
const phoneAuthProvider = new firebase.auth.PhoneAuthProvider(auth);
phoneAuthProvider.verifyPhoneNumber(user.phoneNumber, recaptchaVerifier)
  .then((verificationId) => {
    const credential = firebase.auth.PhoneAuthProvider.credential(
        verificationId, prompt('Enter your code'));
    return user.reauthenticateWithCredential(credential);
  })
  .then((userCredential) => {
    // User successfully reauthenticated.
  });
bojeil
  • 29,642
  • 4
  • 69
  • 76
0

In React Native using expo-firebase-recaptcha

import React, { useState, useEffect, useRef } from 'react';
import { Alert, Text, TextInput, TouchableOpacity, View } from 'react-native';
import * as firebase from 'firebase';
import { FirebaseRecaptchaVerifierModal } from "expo-firebase-recaptcha";


const firebaseConfig = {
  // your firebaseConfig object
  //...
};

const Reauthentication = () => {

  const recaptchaRef = useRef();

  const [code, setCode] = useState("");
  const [verificationId, setVerificationId] = useState();

  useEffect(() => {
    (async () => {
      const user = firebase.auth().currentUser;
      const result = await user.reauthenticateWithPhoneNumber(user.phoneNumber, recaptchaRef.current);
      setVerificationId(result);
    })()
  }, []);


  const reauthenticate = () => {
    verificationId.confirm(code)
    .then((userCredential) => {
      // User is reauthenticated
    })
    .catch((err) => {
      Alert.alert(err);
    })
  }

  return (
    <View style={ styles.container }>
      <FirebaseRecaptchaVerifierModal
        ref={ recaptchaRef }
        firebaseConfig={ firebaseConfig }
      />
      <TextInput
        keyboardType="numeric"
        value={ code }
        onChangeText={ (text) => setCode(text) }
      />
      <TouchableOpacity onPress={ reauthenticate }>
        <Text>Reauthorize</Text>
      </TouchableOpacity>
    </View>
  )
}

export default Reauthentication;
Scott
  • 1,207
  • 2
  • 15
  • 38