OAuth的Google.logInAsync函数在React Native中不做任何事情

问题描述 投票:2回答:1

Google.logInAsync函数本应在React Native中用于OAuth,未给出任何响应,或被卡住。看一下我的代码,从第14行到第36行。此signIn函数正在调用Google.logInAsync,并停留在该位置。没错,没事。但是,当我再次按下按钮并再次调用signIn函数时,会传递错误:Error: Cannot start a new task while another task is currently in progress: Get Auth

这是我目前的代码。它在屏幕上显示标签“使用Google登录”和一个按钮。轻击按钮可启动this.signIn功能,在调试器屏幕上打印“ Started logInAsync”,但Google.logInAsync永远不会完成,不会在任何地方引发错误。 console.log('Completed logInAsync')永远不会执行!

import {Google} from "expo"
...
            console.log('Starting logInAsync')
            const result = await Google.logInAsync({
                androidClientId:
                "<CLIENT_ID>",
                scopes: ["profile", "email"]
            })
            console.log('Completed logInAsync')

给出了错误提示,说安装“ expo-google-app-auth”我安装了该模块,然后更改了导入和功能

import * as Google from "expo-google-app-auth"
...
            console.log('Starting logInAsync')
            const result = await Google.logInAsync({
            ...
            })
            console.log('Completed logInAsync')

之后,

import { StyleSheet, Text, View, Image, Button } from "react-native"
import * as Google from "expo-google-app-auth"

export default class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      signedIn: false,
      name: "",
      photoUrl: ""
    }
  }
  signIn = async () => {
    try {
      console.log('Starting logInAsync')
      const result = await Google.logInAsync({
        androidClientId:
          "860657237026-jfosg5hu52u1vedclccs1vgihghva534.apps.googleusercontent.com",
        scopes: ["profile", "email"]
      })
      console.log('Completed logInAsync')

      if (result.type === "success") {
        this.setState({
          signedIn: true,
          name: result.user.name,
          photoUrl: result.user.photoUrl
        })
      } else {
        console.log("Cancelled!")
      }
    } catch (e) {
      console.log("Error: ", e)
    }
  }
  render() {
    return (
      <View style={styles.container}>
        {this.state.signedIn ? (
          <LoggedInPage name={this.state.name} photoUrl={this.state.photoUrl} />
        ) : (
          <LoginPage signIn={this.signIn} />
        )}
      </View>
    )
  }
}

const LoginPage = props => {
  return (
    <View>
      <Text style={styles.header}>Sign In With Google</Text>
      <Button title="Sign in with Google" onPress={() => props.signIn()} />
    </View>
  )
}

const LoggedInPage = props => {
  return (
    <View style={styles.container}>
      <Text style={styles.header}>Welcome:{props.name}</Text>
      <Image style={styles.image} source={{ uri: props.photoUrl }} />
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  },
  header: {
    fontSize: 25
  },
  image: {
    marginTop: 15,
    width: 150,
    height: 150,
    borderColor: "rgba(0,0,0,0.2)",
    borderWidth: 3,
    borderRadius: 150
  }
})

此代码中没有错误,除非您两次按登录按钮。到目前为止,我正在使用最新版本的Node,React-Native和Expo。

reactjs react-native oauth-2.0 google-oauth google-oauth2
1个回答
0
投票

我有同样的问题。按登录按钮没有任何反应。

© www.soinside.com 2019 - 2024. All rights reserved.