You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

React Native Expo谷歌登录重定向无法返回APP问题排查求助

React Native Expo谷歌登录重定向无法返回APP问题排查求助

问题描述

我用Expo + React Native开发的APP,通过eas build -p android --profile preview打包了Android预览版APK,安装后打开APP,点击谷歌登录按钮选择邮箱后,页面显示"one moment please"就直接跳转到浏览器了,没法自动回到APP里完成登录流程。代码逻辑本身能触发success回调拿到授权code,但就是重定向环节卡壳,有没有大佬能帮忙排查下问题?

相关代码

Google登录组件代码

import * as AuthSession from "expo-auth-session";
import * as Google from "expo-auth-session/providers/google";
import * as WebBrowser from "expo-web-browser";
import React from "react";
import { Button, Text, View } from "react-native";

WebBrowser.maybeCompleteAuthSession();

export default function GoogleSignInScreen() {
  const [log, setLog] = React.useState('');
  const [request, response, promptAsync] = Google.useAuthRequest({
    androidClientId: "xxxxxxxxxxxxxxxxxxxxxxxxx",
    clientId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    responseType: "code",
  });

  // console.log(AuthSession.makeRedirectUri());
  const redirectUri = AuthSession.makeRedirectUri({
    native: `com.subash0396.mynewapp:/oauthredirect`
  });

  React.useEffect(() => {
    if (response) {
      setLog(JSON.stringify(response));
    }
    console.log("Google Response:", response);
    if (response?.type === "success") {
      const code = response.params.code;
      console.log("success:", code);
      fetch("http://192.168.29.33:8000/api/google-signin/signin/", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({ code }),
      })
      .then(res => res.json())
      .then(data => console.log("Django Response:", data))
      .catch(err => console.log("Backend Error:", err));
    }
  }, [response]);

  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: "#fff" }}>
      <Text style={{ fontSize: 18, marginBottom: 20 }}>Google Sign-In</Text>
      <Text>{log}</Text>
      <Button title="Sign In" disabled={!request} onPress={() => promptAsync({ showInRecents: true })} />
    </View>
  );
}

项目配置文件

app.json

{
  "expo": {
    "name": "MyNewApp",
    "slug": "mynewapp",
    "scheme": "mynewapp",
    "platforms": [
      "android",
      "ios"
    ],
    "android": {
      "package": "com.subash0396.mynewapp"
    },
    "ios": {
      "bundleIdentifier": "com.subash0396.mynewapp"
    },
    "owner": "subash0396",
    "extra": {
      "eas": {
        "projectId": "0f8a2386-e6c5-45b0-ae27-f16f5899a581"
      }
    }
  }
}

eas.json

{
  "cli": {
    "version": ">= 16.27.0",
    "appVersionSource": "remote"
  },
  "build": {
    "development": {
      "android": {
        "buildType": "apk"
      },
      "developmentClient": true,
      "distribution": "internal"
    },
    "preview": {
      "android": {
        "buildType": "apk"
      },
      "distribution": "internal"
    },
    "production": {
      "android": {
        "buildType": "app-bundle"
      },
      "autoIncrement": true
    }
  },
  "submit": {
    "production": {}
  }
}

已完成的前置配置

  • 谷歌云控制台已添加安卓应用包名com.subash0396.mynewapp
  • 谷歌云控制台已添加对应APK的SHA-1证书指纹

核心疑问点

  1. 我手动指定的redirectUricom.subash0396.mynewapp:/oauthredirect,和app.json里配置的scheme有没有冲突?
  2. WebBrowser.maybeCompleteAuthSession()的调用位置是否正确?会不会影响重定向回调?
  3. EAS打包过程中有没有隐藏的配置项,会影响APP的Scheme重定向能力?

麻烦各位大佬帮忙梳理下可能的问题点,感激不尽!

火山引擎 最新活动