Google身份验证在Expo托管应用程序中在生产版本中失败。

huangapple go评论65阅读模式
英文:

Google Authentication in Expo managed app fails in production build

问题

要获取Google Auth的id_token,用于Firebase身份验证。

我正在使用Expo 47。

在iOS上,身份验证正常工作。但在Android设备上失败。

使用的代码如下:

import * as Google from "expo-auth-session/providers/google";
import * as WebBrowser from "expo-web-browser";

WebBrowser.maybeCompleteAuthSession();

const isDev = process.env.NODE_ENV === 'development';

export default function GoogleAuth() {

  const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
    androidClientId: "",
    iosClientId: "",
    clientId: "",
  });

  useEffect(() => {
    if (!response) return;
    if (response?.params?.id_token) {
      // 使用Firebase进行登录
    } else {
      console.log(response); // 这在Android APK中记录为: {"type":"dismiss"}
    }
  }, [response]);

  if (!request) {
    return null;
  }

  return (
    <Button
      onPress={() => promptAsync({ useProxy: isDev, showInRecents: true })}
      title={"继续使用Google登录"}
    />
  );
};

我尝试使用useAuthRequestresponseType: 'id_token',但它显示响应类型无效。如何修复这个错误?

英文:

Requirement: Get the id_token from Google Auth, which is used with firebase authentication.

I'm using Expo 47.

The authentication works fine on iOS. But it fails on Android devices.

Code used:

import * as Google from &quot;expo-auth-session/providers/google&quot;;
import * as WebBrowser from &quot;expo-web-browser&quot;;

WebBrowser.maybeCompleteAuthSession();

const isDev = process.env.NODE_ENV === &#39;development&#39;;

export default function GoogleAuth() {

  const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
    androidClientId: &quot;&quot;,
    iosClientId: &quot;&quot;,
    clientId: &quot;&quot;,
  });

  useEffect(() =&gt; {
    if (!response) return;
    if (response?.params?.id_token) {
      // log in with firebase
    } else {
      console.log(response) // this logs: {&quot;type&quot;:&quot;dismiss&quot;} in android APK
    }
  }, [response]);

  if (!request) {
    return null;
  }

  return (
    &lt;Button
      onPress={() =&gt; promptAsync({ useProxy: isDev, showInRecents: true })}
      title={&quot;Continue with Google&quot;}
    /&gt;
  );
};


I tried using useAuthRequest with responseType: &#39;id_token&#39; but it says the response type is invalid. How to fix this error?

答案1

得分: 1

已经弄清楚了。app.jsonschema 不能包含任何大写字母。我的 packageNameschema 最初是以 com.myapp.mobileApp 的形式,当我将它们更改为 com.myapp.mobileapp 时,登录正常工作。

更新:在 Expo Go 中,这不起作用。您需要设置一个自定义开发客户端才能使其工作:https://blog.expo.dev/introducing-custom-development-clients-5a2c79a9ddf8

英文:

Figured it out. The schema of app.json cannot have any uppercase letters. My packageName and schema was in the form of com.myapp.mobileApp. The login worked fine when I changed them to com.myapp.mobileapp.

Update: This won't work in Expo Go. You'll need to set up a Custom Dev Client to get this work: https://blog.expo.dev/introducing-custom-development-clients-5a2c79a9ddf8

huangapple
  • 本文由 发表于 2023年3月15日 18:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定