如何使用firebase认证检索和处理重定向错误

how to retrieve and handle Redirect errors using firebase Auth?

本文关键字:处理 重定向 错误 检索 何使用 firebase 认证      更新时间:2023-09-26

我正在开发移动应用程序,使用Firebase Auth。Firebase建议重定向而不是弹出。然而,我似乎找不到任何使用Oauth提供商(facebook,Google)检索错误的例子。Firebase有一个在SignwithPopup中处理错误的例子,但是在重定向之前,它只声明:

在重定向模式中以类似的方式处理此错误,使用挂起的凭据必须在页面之间缓存的区别重定向(例如,使用会话存储)。

我们在同一文档的前一节中展示了在哪里对重定向操作进行错误处理:只需在本页中搜索"firebase.auth().getRedirectResult()",特别是在catch中:

firebase.auth().getRedirectResult().then(function(result) {
  if (result.credential) {
    // This gives you a Google Access Token. You can use it to access the Google API.
    var token = result.credential.accessToken;
    // ...
  }
  // The signed-in user info.
  var user = result.user;
}).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;
  // ...
});
顺便说一下,添加多个认证提供者和处理链接帐户实际上是相当棘手的,因为有许多子流程需要考虑(例如,如果用户想要链接,但随后登录了一个帐户,其中电子邮件不匹配…)。我建议您使用Firebase UI,它提供了一个可配置的UI组件,它将为您处理所有这些棘手的流程。