如何使用Firebase Auth检索和处理重定向错误? [英] how to retrieve and handle Redirect errors using firebase Auth?

本文介绍了如何使用Firebase Auth检索和处理重定向错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用Firebase身份验证的移动第一个应用程序。 Firebase建议使用重定向而不是弹出式菜单。但是,我似乎无法找到任何使用Oauth提供程序(Facebook,Google)检索错误的示例。 Firebase有一个在SignwithPopup中处理错误的例子,但是前面的重定向只说明:

lockquote

这个错误在重定向中以类似的方式处理模式,其中
的差异是待处理凭证必须缓存在页面
重定向之间(例如使用会话存储)。


<我们显示了在同一个文档的上一节中重定向操作的错误处理:只搜索firebase.auth()。getRedirectResult()in 此页面专门在 catch here:



$ p $ firebase.auth()。getRedirectResult()。then(function(result) {
if(result.credential){
//这会为您提供一个Google Access令牌,您可以使用它来访问Google API
var token = result.credential.accessToken;
// ...
}
//登录的用户信息。
var user = result.user;
})。catch(function(error){
// Handle Errors here。
var errorCode = error.code;
var errorMessage = error.message;

var email = error.email;
//使用的firebase.auth.AuthCredential类型
var credential = error.credential;
// ...
});

顺便说一句,添加多个auth提供程序和处理连接帐户是非常棘手的,因为有很多要考虑的子流程(例如,如果用户想要链接,但是然后登录电子邮件不匹配的帐户)。我建议您使用 Firebase UI ,它提供了一个可配置的UI组件,可以处理所有这些棘手的流程。


I'm developing mobile first app, using Firebase Auth. Firebase recommends redirect instead of popup. However, I can't seem to find any example of retrieving errors on using Oauth providers (facebook ,Google). Firebase has an example of handling error in SignwithPopup , but fore redirect it only states:

This error is handled in a similar way in the redirect mode, with the difference that the pending credential has to be cached between page redirects (for example, using session storage).

解决方案

We show where to do error handling for redirect operation in the previous section of the same doc: Just search for "firebase.auth().getRedirectResult()" in this page specifically in the catch here:

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;
  // ...
});

By the way, adding multiple auth providers and handling linking accounts perfectly is actually quite tricky because there are many sub-flows to take into account (e.g. what if the user wants to link but then signs-in an account where the emails do not match...). I recommend you use Firebase UI which provides a configurable UI component that will handle all these tricky flows for you.

这篇关于如何使用Firebase Auth检索和处理重定向错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆