这是我在RNFB facebook登录时遇到的第二个问题。
我遵循RNFB提供的官方代码示例。。。。低于的代码
线路firebase().signInWithCredential(facebookCredential);
出现问题。。。。它没有触发firebase监听器auth().onAuthStateChanged
所有其他代码都在正常运行,并且facebookCredential
变量已正确填充
import { firebase } from '@react-native-firebase/auth';
onFacebookButtonPress = async () => {
// Attempt login with permissions
const result = await LoginManager.logInWithPermissions([
'public_profile',
'email',
]);
if (result.isCancelled) {
throw 'User cancelled the login process';
}
// Once signed in, get the users AccesToken
const data = await AccessToken.getCurrentAccessToken();
if (!data) {
throw 'Something went wrong obtaining access token';
}
// Create a Firebase credential with the AccessToken
//const facebookCredential = firebase.FacebookAuthProvider.credential(data.accessToken);
const facebookCredential = firebase.auth.FacebookAuthProvider.credential(
data.accessToken,
);
// Sign-in the user with the credential
firebase().signInWithCredential(facebookCredential);
};
我通过在有问题的行周围放一个try/catch来解决这个问题。结果是因为我已经用谷歌登录过一次了。
An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address
您不应该等到出现问题才添加try/catch。signInWithCredential
返回许多不同类型的错误,您可以处理这些错误。来自文档:
firebase.auth().signInWithCredential(credential).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
if (errorCode === 'auth/account-exists-with-different-credential') {
alert('Email already associated with another account.');
// Handle account linking here, if using.
} else {
console.error(error);
}
});
请处理其他错误情况🙂:
身份验证/帐户存在不同的凭据
身份验证/无效凭证
不允许身份验证/操作
身份验证/用户禁用
等