React Native Google 身份验证不起作用 - TypeError: (0, _auth.signInWithPopup) 不是函数

Ada*_*rdt 9 javascript firebase react-native firebase-authentication

所以我正在尝试将 Google 身份验证与 Firebase 合并到我的 React Native 项目中。我使用 createUserWithEmailAndPassword() 注册用户没有问题,但 signInWithPopUp() 给我带来了麻烦

图像到我的代码

我已在 Firebase 上启用 Google 作为提供商。这是整个错误 - TypeError: (0, _auth.signInWithPopup) is not a function。(在“(0,_auth.signInWithPopup)(_firebase.authentication,provider)”中,“(0,_auth.signInWithPopup)”未定义)

不知道如何解决它,我希望得到建议。

这是文本格式的代码:

import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Image, Text, ScrollView, KeyboardAvoidingView, Button } from 'react-native';
import AppButton from '../components/AppButton';
import colors from '../config/colors';
import AppTextInput from '../components/AppTextInput';

import  {createUserWithEmailAndPassword, GoogleAuthProvider, signInWithPopup} from 'firebase/auth';
import {authentication} from "../../firebase/firebase";

function SignUp({navigation}) {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');

    const RegisterUser = () => {
        createUserWithEmailAndPassword(authentication, email, password)
        .then((userCredentials) => {
            let user = userCredentials.user
            navigation.replace('WelcomeScreen')
            console.log("Registered with:", user.email);
        })
        .catch((error)=> alert(error.message))
    }

    const googleSignIn = () =>{
        let provider = new GoogleAuthProvider();
        signInWithPopup(authentication, provider)
        .then((re)=>{
            console.log(re);
        })
        .catch((err) => alert(err.message))
    }

    
    return (
        <View style = {styles.background}>
            <ScrollView>
                <Image style = {styles.frontImage} source={require("../assets/bg.jpg")}/>
                
                <KeyboardAvoidingView
                    behavior={Platform.OS === "ios" ? "padding" : null}
                    keyboardVerticalOffset={Platform.OS === "ios" ? 64 : 0}
                >
                    
                    <AppTextInput 
                            placeholder = "Email" 
                            icon="email"
                            autoCapitalize = "none"
                            value = {email}
                            onChangeText={text=>setEmail(text)}
                            autoCorrect={false}
                    />
                    <AppTextInput 
                            autoCapitalize="none"
                            autoCorrect={false}
                            icon="lock"
                            placeholder="Password"
                            secureTextEntry={true}
                            value = {password}
                            onChangeText={text=>setPassword(text)}
                            textContentType = "password"
                        />
                </KeyboardAvoidingView>
                <AppButton title="Create Account" color = "lightYellow" onPress={RegisterUser}/>
                <AppButton title="Sign in With Google" color= "lightYellow" onPress={googleSignIn}/>
                <View style = {styles.footerText}>
                    <Text onPress={() => navigation.navigate('Login')}>Already have an account?   </Text>
                    <Text style = {styles.loginText} onPress={() => navigation.navigate('Login')}>Log in</Text>
                </View>
                <Text style={{
                    left: 30,
                    textDecorationLine: 'underline',
                }}>By signing up you agree to Terms Of Service and Privacy</Text>
            </ScrollView>
            
        </View>
    );
}
Run Code Online (Sandbox Code Playgroud)

这是我初始化 firebase 的地方(空字符串中有信息,只是用空字符串替换它们):

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAuth } from 'firebase/auth'

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const authentication = getAuth(app)
Run Code Online (Sandbox Code Playgroud)

小智 1

我也遇到了同样的问题并设法解决了,这确实是一个小错误。

就是这部分代码:

const googleSignIn = () => {
  let provider = new GoogleAuthProvider();
  signInWithPopup(authentication, provider)
    .then((re) => {
      console.log(re);
    })
    .catch((err) => alert(err.message));
};
Run Code Online (Sandbox Code Playgroud)

声明时GoogleAuthProvider(),应用程序的配置必须作为参数传递,如下所示:

const provider = new GoogleAuthProvider(app);
Run Code Online (Sandbox Code Playgroud)