[SOLVED] Issues reauthenticaing a user with credentials inside of react native app

Issue

This Content is from Stack Overflow. Question asked by ojandali

I have a react native application. I want to be able to reauthenticate a user before submitting the updated email. I am running into the issue of reautneticaing the user.

tried

  const saveProfile = () => {
    let credential = EmailAuthProvider.credential(user.email, 'hellohello')
    user.reauthenticateWithCredential(credential)
      .then(() => {
        updateEmail(auth.currentUser, newEmail.toString())
          .then(() => {
            console.log('updated email')
            setUser(auth.currentUser)
          })
          .catch((error) => {
            console.log(error)
          })
      })
      .catch((error) => {
        console.log(error)
      })
  }

and

  const saveProfile = () => {
    let credential = EmailAuthProvider.credential(user.email, 'hellohello')
    reauthenticateWithCredential(credential)
      .then(() => {
        updateEmail(auth.currentUser, newEmail.toString())
          .then(() => {
            console.log('updated email')
            setUser(auth.currentUser)
          })
          .catch((error) => {
            console.log(error)
          })
      })
      .catch((error) => {
        console.log(error)
      })
  }

When i try to update the email with firebase, it says I have to Firebase: Error (auth/requires-recent-login).

error:

undefined is not an object (evaluating ‘credential._getReauthenticationResolver’)
at node_modules/@firebase/auth/dist/rn/phone-eec7f987.js:5048:20 in _processCredentialSavingMfaContextIfNecessary
at node_modules/@firebase/auth/dist/rn/phone-eec7f987.js:5212:113 in tslib.__generator$argument_1
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:144:21 in step
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:125:60 in
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:118:17 in
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:114:15 in __awaiter
at node_modules/@firebase/auth/dist/rn/phone-eec7f987.js:5333:49 in tslib.__generator$argument_1
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:144:21 in step
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:125:60 in
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:118:17 in
at node_modules/@firebase/auth/node_modules/tslib/tslib.js:114:15 in __awaiter
at src/screens/SettingsScreen.js:158:4 in saveProfile
at src/screens/SettingsScreen.js:186:81 in TouchableOpacity.props.onPress
at node_modules/react-native/Libraries/Pressability/Pressability.js:702:17 in _performTransitionSideEffects
at node_modules/react-native/Libraries/Pressability/Pressability.js:639:6 in _receiveSignal
at node_modules/react-native/Libraries/Pressability/Pressability.js:520:8 in responderEventHandlers.onResponderRelease



Solution

The reauthenticateWithCredential() function takes currentUser as first parameter. Try refactoring the code as shown below:

const saveProfile = async () => {
  const credential = EmailAuthProvider.credential(user.email, 'hellohello')

  // auth.currentUser as first param
  await reauthenticateWithCredential(auth.currentUser, credential);

  await updateEmail(auth.currentUser, newEmail.toString())
  console.log('updated email')
  setUser(auth.currentUser)
}


This Question was asked in StackOverflow by ojandali and Answered by Dharmaraj It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?