英文:
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登录"}
/>
);
};
我尝试使用useAuthRequest
和responseType: '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 "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) {
// log in with firebase
} else {
console.log(response) // this logs: {"type":"dismiss"} in android APK
}
}, [response]);
if (!request) {
return null;
}
return (
<Button
onPress={() => promptAsync({ useProxy: isDev, showInRecents: true })}
title={"Continue with Google"}
/>
);
};
I tried using useAuthRequest
with responseType: 'id_token'
but it says the response type is invalid. How to fix this error?
答案1
得分: 1
已经弄清楚了。app.json
的 schema
不能包含任何大写字母。我的 packageName
和 schema
最初是以 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论