我一直在使用后端的React Native(带有Expo)和Firebase开发一个应用程序。当通过iPhone上的Expo客户端运行项目时,通常可以使用电子邮件和密码登录,然后从Firebase数据库中获取数据。但是,当我使用Facebook登录时,数据库无法读取,并且无法解决任何问题。代码的重要部分如下:
firebase.initializeApp(firebaseConfig);
// This works everywhere
export const login = async (email, password) => {
await firebase.auth().signInWithEmailAndPassword(email, password);
const userId = firebase.auth().currentUser.uid;
return userId + '';
};
export const loginByFacebook = async () => {
const { type, token } = await Expo.Facebook.logInWithReadPermissionsAsync(FB_APP_ID, {
permissions: ['public_profile'],
});
if (type === 'success') {
const credential = firebase.auth.FacebookAuthProvider.credential(token);
try {
await firebase.auth().signInAndRetrieveDataWithCredential(credential);
} catch (error) {
console.log('cannot login ', error);
}
}
};
export const readData = (key) => {
console.log('getWins ');
const userId = firebase.auth().currentUser.uid;
return firebase
.database()
.ref(`/${key}/${userId}`)
.once('value');
};
...
class PostList extends React.Component {
async componentDidMount() {
// it normally resolves when logged with email & password,
// resolves with facebook auth on iPhone simulator
// does not resolve with facebook auth on Expo client on iPhone
const data = await readData('posts');
}
}
但是,真正奇怪的是,它不能在iPhone + Expo客户端上运行,但可以在iPhone模拟器上运行。关键部分在async componentDidMount()
中。
数据库配置仍处于开发模式(允许所有读取和写入):
{
"rules": {
".read": true,
".write": true
}
}
我使用了以下指南:https://docs.expo.io/versions/latest/sdk/facebook https://docs.expo.io/versions/latest/guides/using-firebase#listening-for-authentication
我忘了设置其他前提条件吗?还是Expo客户在使用Facebook auth正确处理呼叫方面有局限性?