Google 使用 expo 登录:使用 Google 登录不调用异步

Google SignIn with expo: signInWithGoogleAsync not being invoked

我正在学习本教程 https://github.com/nathvarun/Expo-Google-Login-Firebase,下面是我的代码。但是,似乎没有调用 signInWithGoogleAsync() 函数。单击按钮什么也不做。会不会是stack Navigator的问题?

firebase 没有问题,因为电子邮件登录正常

下面是我的LoginScreen.js

import { useNavigation } from '@react-navigation/core'
import React, { useEffect, useState, Component } from 'react'
import { Button, KeyboardAvoidingView, StyleSheet, Text,TextInput, TouchableOpacity, View } from 'react-native'
import { auth } from '../firebase'
import * as GoogleSignIn from 'expo-google-sign-in';

class LoginScreen extends Component {
signInWithGoogleAsync = async() => {
try {
  const result = await Google.logInAsync({       
    behavior:'web',
    iosClientId: 'lorem ipsum',
    scopes: ['profile', 'email'],
  });

  if (result.type === 'success') {
    return result.accessToken;
  } else {
    return { cancelled: true };
  }
} catch (e) {
  return { error: true };
}
};
render(){
return (
<KeyboardAvoidingView
  style={styles.container}
  behavior="padding"
>
  <View style={styles.inputContainer}>
   <Button
    title='sign'
    onPress={()=>this.signInWithGoogleAsync()} />
  </View>
</KeyboardAvoidingView>
 );
 }
}

export default LoginScreen

这是 App.js:

import { StatusBar } from 'expo-status-bar'; 
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
import HomeScreen from './screens/HomeScreen';
import * as firebase from "firebase/compat";

const Stack = createNativeStackNavigator();

export default class App extends React.Component {
 render(){
 return (
<NavigationContainer>
  <Stack.Navigator>
    <Stack.Screen options={{ headerShown: false }} name="Login" component={LoginScreen} />
    <Stack.Screen name="Home" component={HomeScreen} />
  </Stack.Navigator>
</NavigationContainer>
);
}
}

const styles = StyleSheet.create({
 container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
registerRootComponent(App);

您正在将所有内容导入为 GoogleSignIn,然后在下方执行 await Google.logInAsync,因此您必须将其更改为 await GoogleSignIn.loadInAsync。第 9 行

另外,看看文档,logInAsync 看起来不像是一个函数 https://docs.expo.dev/versions/latest/sdk/google-sign-in/