使用Facebook进行Firebase身份验证-React Native

时间:2018-10-16 16:05:03

标签: firebase react-native fbsdk

我正在尝试通过Facebook登录访问我的React本机应用程序。我在android上使用fbsdk和firebase。这是我的功能:

loginConFacebookHandler = () => {
LoginManager.logInWithReadPermissions(['public_profile'])
.then(result => {
  if (result.isCancelled) {
    console.log('Cancelado')
    return Promise.reject(new Error('The user cancelled the request'));
  }
  console.log(`Login success with permissions: ${result.grantedPermissions.toString()}`);
  return AccessToken.getCurrentAccessToken();
})
.then(data => {
  const credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken);      
  return firebase.auth().signInWithCredential(credential);
})
.then(currentUser => {
  console.log(`Facebook Login with user : ${JSON.stringify(currentUser.toJSON())}`);
  () => {startMainTabs()}
})
.catch(error => {
  console.log(`Login con facebook falló con error: ${error}`);
})

}

其中startMainTabs()重定向到另一个屏幕。当我使用用户名和密码登录时,它可以完美运行:

loginUsuarioYContraseñaHandler = (email, password) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(() => {startMainTabs()})
.catch(error => {alert(error)})
};

1 个答案:

答案 0 :(得分:0)

我认为问题是您没有等待响应,您需要执行同步功能,然后导航到另一个屏幕,这是我在另一个应用程序中使用过的示例,希望您不能使用它。

export async function facebookLogin() {
let response;
try {
    const result = await LoginManager.logInWithReadPermissions(['public_profile', 'email']);
    if (result.isCancelled) {
        response = "El usuario cancelo el inicio de sesion"
    } else {
        const data = await AccessToken.getCurrentAccessToken();
        if (!data) {
            response = "Hubo un error obteniendo los datos, cheque su internet";
        } else {
            const credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken);
            await firebase.auth().signInAndRetrieveDataWithCredential(credential).then((user) => {
                response = user;
            });
        }
    }
    return response;
} catch (e) {
    response = (e.toString() === " An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.") ? "Usuario existente con el mismo coreo, intente con otro." : e.toString();
    return response;
}

}