android - expo-google-app-auth 的 Android 问题
问题描述
我在Android
. 它与IOS
.
在 Android 中成功登录而不是将我重定向回我的应用程序LoggedInPage
组件后,我再次进入LoginPage
组件。我认为这是因为Android再次打开应用程序并且我失去了状态,所以我也失去了登录状态,我必须再次登录......
在IOS中,它只是让我LoggedInPage
完美地重定向到组件..
import React, { useState } from "react";
import * as Google from "expo-google-app-auth";
import { StyleSheet, Text, View, Image, Button } from "react-native";
export default function App() {
const [signedIn, setSignIn] = useState(false);
const [name, setName] = useState("");
const [photoUrl, setPhotoUrl] = useState("");
signIn = async () => {
try {
console.log("Sign in 1");
const result = await Google.logInAsync({
androidClientId:
“<< android client id >>apps.googleusercontent.com",
iosClientId:
“<< iOS client id >>.apps.googleusercontent.com",
scopes: ["profile", "email"]
});
console.log("Sign in 2");
console.log("Result: ", result);
if (result.type === "success") {
setSignIn(true);
setName(result.user.name);
setPhotoUrl(result.user.photoUrl);
return result.accessToken;
} else {
return { cancelled: true };
}
} catch (e) {
return { error: true };
}
};
const LoginPage = () => {
console.log("Inside LoginPage");
return (
<View>
<Text style={styles.header}>Sign In With Google</Text>
<Button title="Sign in with Google" onPress={() => signIn()} />
</View>
);
};
const LoggedInPage = () => {
return (
<View style={styles.container}>
<Text style={styles.header}>Welcome:{name}</Text>
<Image style={styles.image} source={{ uri: photoUrl }} />
</View>
);
};
console.log("SignedIn: ", signedIn);
return (
<View style={styles.container}>
{signedIn ? <LoggedInPage /> : <LoginPage />}
</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
}
});
任何想法/指导我应该如何在 Android 中修复我的应用程序?
解决方案
您需要添加一个redirectUrl:
const result = await Google.logInAsync({
androidClientId:
“<< android client id >>apps.googleusercontent.com",
iosClientId:
“<< iOS client id >>.apps.googleusercontent.com",
scopes: ["profile", "email"],
redirectUrl: "{Your Bundle ID (com.example.app)}:/oauth2redirect/google",
});
推荐阅读
- android - “R”或“资源”在 Android Studio 中给我错误
- python - 如何通过点击而不是语音触发 Alexa 意图?
- json - json 文件在网页上的“< >”内不显示字符串(例如“abc
") - spring - 无法解析原型
- android - Flutter 永久隐藏导航栏
- php - 如何将数据库中的数据显示到 sb-admin 模板?
- angular - 在springboot java项目中捆绑angular ui项目并访问其UI
- assembly - 程序集 8086 中数组的未知大小
- javascript - 如何从对象中的特定参数中找到我的对象中的唯一对象
- javascript - Chrome 开发工具:实现您自己的元素层次结构视图(相同的 chrome,react 开发工具)