简体   繁体   中英

React Native Firebase Phone Auth - auth/session-expired error. (Android)

This is my code from rnfirebase.io.

I receive the SMS code after pressing the sign-in button. But after typing the Verification code, firebase returns me this [auth/session-expired] error.

[Error: [auth/session-expired] The sms code has expired. Please re-send the verification code to try again.]

Also setConfirm(confirmation) does not works. It returns a null again.

How can I solve this problem? Thanks!

import React, { useState } from "react";
import { View, Text, StyleSheet, Button, TextInput } from "react-native";
import auth from "@react-native-firebase/auth";
import colors from "../../config/colors";
// create a component
const UyeOlScreen = () => {
 // If null, no SMS has been sent
 const [confirm, setConfirm] = useState(null);

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

 // Handle the button press
 async function signInWithPhoneNumber(phoneNumber) {
   const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
   setConfirm(confirmation);
 }

 async function confirmCode() {
   try {
     await confirm.confirm(code);
   } catch (error) {
     console.log(error);
   }
 }

 if (!confirm) {
   return (
     <Button
       title="Phone Number Sign In"
       onPress={() => signInWithPhoneNumber(`+123456789`)}
     />
   );
 }

 return (
   <>
     <TextInput value={code} onChangeText={(text) => setCode(text)} />
     <Button title="Confirm Code" onPress={() => confirmCode()} />
   </>
 );
};
//make this component available to the app
export default UyeOlScreen; ```

This helped me fix this issue: How to use Firebase's 'verifyPhoneNumber()' to confirm phone # ownership without using # to sign-in?

Of course, I had to make a few tweaks to make it work for me. Specifically, I took out the code:

firebase
  .firestore()
  .collection('users')
  .where('phoneNumber', '==', this.state.phoneNumber)
  .get()
  .then((querySnapshot) => {
    if (!querySnapshot.empty) {
      // User found with this phone number.
      throw new Error('already-exists');
    }

I think that is dealing with storage that can be used with firebase that I do not use.

I also took out:

let fbWorkerApp = firebase.apps.find(app => app.name === 'auth-worker')
                 || firebase.initializeApp(firebase.app().options, 'auth-worker');
  fbWorkerAuth = fbWorkerApp.auth();  
  fbWorkerAuth.setPersistence(firebase.auth.Auth.Persistence.NONE); // disables caching of account credentials

and just used "auth()" in place of fbWorkerAuth because I was already doing:

import auth from '@react-native-firebase/auth';

Also there is a small bug in one of the catch blocks that has "err" and it should be "error".

Otherwise it solved this issue for me. Too bad all of the documentation around this is pointing us to a sample that doesn't work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM