导入 Google 表单“expo-auth-session/providers/google”时发生错误

发布于 2025-01-16 08:18:53 字数 3518 浏览 4 评论 0原文

我在使用 Expo 管理的 React Native 应用程序中实现 Google Auth 时遇到问题。当我尝试导入它时,它返回未定义。当然,我安装了它和相关的软件包。这是我的代码。

我使用的是 Expo 42,我通过 Expo CLI 安装了 expo-auth-session,版本是 3.3.1。我不知道为什么会出现这个问题。当然,我尝试删除node_modules并重新安装。但它失败了。


import * as React from "react";
import * as WebBrowser from "expo-web-browser";
import { Button } from "react-native";
import * as Google from "expo-auth-session/providers/google";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { MAIN_SCREEN_NAME } from "constants/SCREEN_NAME";
import { useEffect, useState } from "react";
import { useNavigation } from "@react-navigation/native";

WebBrowser.maybeCompleteAuthSession();

const GoogleLogin = () => {
  const navigation = useNavigation();
  const [request, response, promptAsync] = Google.useAuthRequest({
    expoClientId:
      "",
    iosClientId:
      "",
    webClientId:
      "",
  });

  const asyncAuthRequest = async () => {
    if (response?.type === "success") {
      const { authentication } = response;
      // await AsyncStorage.setItem("accessTocken", "hihi");

      navigation.navigate("Root");
    }
  };

  useEffect(() => {
    asyncAuthRequest();
  }, [response]);

  return (
    <Button
      disabled={!request}
      title="Login"
      onPress={() => {
        promptAsync();
      }}
    />
  );
};

export default GoogleLogin;

这是我的 package.json 文件。


{
  "dependencies": {
    "expo": "42.0.1",
    "expo-application": "4.0.2",
    "expo-asset": "8.3.2",
    "expo-auth-session": "3.3.1",
    "expo-constants": "11.0.1",
    "expo-font": "9.2.1",
    "expo-image-picker": "12.0.2",
    "expo-image-picker-multiple": "1.4.1",
    "expo-linking": "2.3.1",
    "expo-location": "12.1.2",
    "expo-media-library": "12.1.2",
    "expo-random": "11.2.0",
    "expo-splash-screen": "0.11.2",
    "expo-status-bar": "1.0.4",
    "expo-web-browser": "9.2.0",
    "firebase": "8.2.3",
    "mobx": "6.3.7",
    "mobx-react": "7.2.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-hook-form": "7.21.2",
    "react-icons": "4.3.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz",
    "react-native-dotenv": "3.3.0",
    "react-native-gesture-handler": "1.10.2",
    "react-native-iphone-x-helper": "1.3.1",
    "react-native-maps": "0.28.0",
    "react-native-reanimated": "2.2.0",
    "react-native-safe-area-context": "3.2.0",
    "react-native-screens": "3.4.0",
    "react-native-snap-carousel": "3.9.1",
    "react-native-status-bar-height": "2.6.0",
    "react-native-svg": "12.1.1",
    "react-native-web": "0.13.12",
    "reflect-metadata": "0.1.13",
    "styled-components": "5.3.3",
    "unimodules-permissions-interface": "6.1.0",
    "unsafe-class-transformer": "0.4.0"
  },
  "devDependencies": {
    "@babel/core": "7.9.0",
    "@babel/plugin-proposal-decorators": "7.16.4",
    "@types/react": "16.9.35",
    "@types/react-native": "0.63.2",
    "@types/react-native-dotenv": "0.2.0",
    "@types/react-native-snap-carousel": "3.8.5",
    "@types/styled-components": "5.1.15",
    "@types/styled-components-react-native": "5.1.3",
    "babel-loader": "8.2.2",
    "babel-plugin-module-resolver": "4.1.0",
    "jest-expo": "41.0.0-beta.0",
    "metro-react-native-babel-preset": "0.66.2",
    "react-native-svg-transformer": "1.0.0",
    "typescript": "4.5.4"
  },
  "private": true
}

I have a problem while implementationg Google Auth within a React Native app managed with Expo. When i try to import it, it returns undefined. Sure, i installed it and related packages. Here is my code.

I'm using Expo 42, I installed expo-auth-session via Expo CLI which version is 3.3.1. I don't know why this problem occured. Sure i tried to remove node_modules and reinstalled. But it failed.


import * as React from "react";
import * as WebBrowser from "expo-web-browser";
import { Button } from "react-native";
import * as Google from "expo-auth-session/providers/google";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { MAIN_SCREEN_NAME } from "constants/SCREEN_NAME";
import { useEffect, useState } from "react";
import { useNavigation } from "@react-navigation/native";

WebBrowser.maybeCompleteAuthSession();

const GoogleLogin = () => {
  const navigation = useNavigation();
  const [request, response, promptAsync] = Google.useAuthRequest({
    expoClientId:
      "",
    iosClientId:
      "",
    webClientId:
      "",
  });

  const asyncAuthRequest = async () => {
    if (response?.type === "success") {
      const { authentication } = response;
      // await AsyncStorage.setItem("accessTocken", "hihi");

      navigation.navigate("Root");
    }
  };

  useEffect(() => {
    asyncAuthRequest();
  }, [response]);

  return (
    <Button
      disabled={!request}
      title="Login"
      onPress={() => {
        promptAsync();
      }}
    />
  );
};

export default GoogleLogin;

and this is my package.json file.


{
  "dependencies": {
    "expo": "42.0.1",
    "expo-application": "4.0.2",
    "expo-asset": "8.3.2",
    "expo-auth-session": "3.3.1",
    "expo-constants": "11.0.1",
    "expo-font": "9.2.1",
    "expo-image-picker": "12.0.2",
    "expo-image-picker-multiple": "1.4.1",
    "expo-linking": "2.3.1",
    "expo-location": "12.1.2",
    "expo-media-library": "12.1.2",
    "expo-random": "11.2.0",
    "expo-splash-screen": "0.11.2",
    "expo-status-bar": "1.0.4",
    "expo-web-browser": "9.2.0",
    "firebase": "8.2.3",
    "mobx": "6.3.7",
    "mobx-react": "7.2.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-hook-form": "7.21.2",
    "react-icons": "4.3.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz",
    "react-native-dotenv": "3.3.0",
    "react-native-gesture-handler": "1.10.2",
    "react-native-iphone-x-helper": "1.3.1",
    "react-native-maps": "0.28.0",
    "react-native-reanimated": "2.2.0",
    "react-native-safe-area-context": "3.2.0",
    "react-native-screens": "3.4.0",
    "react-native-snap-carousel": "3.9.1",
    "react-native-status-bar-height": "2.6.0",
    "react-native-svg": "12.1.1",
    "react-native-web": "0.13.12",
    "reflect-metadata": "0.1.13",
    "styled-components": "5.3.3",
    "unimodules-permissions-interface": "6.1.0",
    "unsafe-class-transformer": "0.4.0"
  },
  "devDependencies": {
    "@babel/core": "7.9.0",
    "@babel/plugin-proposal-decorators": "7.16.4",
    "@types/react": "16.9.35",
    "@types/react-native": "0.63.2",
    "@types/react-native-dotenv": "0.2.0",
    "@types/react-native-snap-carousel": "3.8.5",
    "@types/styled-components": "5.1.15",
    "@types/styled-components-react-native": "5.1.3",
    "babel-loader": "8.2.2",
    "babel-plugin-module-resolver": "4.1.0",
    "jest-expo": "41.0.0-beta.0",
    "metro-react-native-babel-preset": "0.66.2",
    "react-native-svg-transformer": "1.0.0",
    "typescript": "4.5.4"
  },
  "private": true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-01-23 08:18:53

您安装了此对等依赖项吗?

yarn add expo-application

它是 AuthSession 文档中 Google 部分下的一小行。请参阅:

文档快照

Did you install this peer dependency?

yarn add expo-application

It is a small line under the Google section in the AuthSession documentation. See:

Documentation Snapshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文