Firebase Auth检查Facebook上是否有新用户登录 [英] Firebase Auth check if new user on Facebook login

查看:89
本文介绍了Firebase Auth检查Facebook上是否有新用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase身份验证创建我的用户的Web应用程序,并使用MongoDB数据库存储一些特定于我的站点的相关用户信息.我正在尝试添加Facebook作为身份验证的另一种方式.

I am creating a web app using Firebase auth to authenticate my users and a MongoDB database to store some relevant user information specific to my site. I am trying to add Facebook as another means of authentication.

使用Facebook登录正在工作,但是我想做的是使用Firebase拦截用户生成/身份验证,以检查Firebase是否在首次Facebook登录时正在创建新用户.这样,如果要在Firebase中创建新用户,我也可以在mongo数据库中创建新用户(本质上是处理注册).我的facebook登录功能如下:

Logging in with Facebook is working, but what I want to do is intercept the user generation/authentication with Firebase to check if Firebase is creating a new user on a first-time Facebook login. This way if a new user is being created in Firebase I can also create a new user in my mongo database (essentially handling a signup). My facebook login function looks like this:

handleFacebookLogin() {
 firebase.auth().signInWithPopup(provider).then(function(result) {

  var token = result.credential.accessToken;
  var user = result.user;

  // check if new user here
  // handleNewFacebookUser()

  this.props.update(user);

 }).catch(function(error) {

  var errorCode = error.code;
  var errorMessage = error.message;
  var email = error.email;
  var credential = error.credential;

 });
}

我找不到任何可以检查Firebase文档中是否正在生成新用户的东西.首次通过Facebook登录时,它似乎默默地处理了新的Firebase身份验证用户.如果Firebase没有内置方法可以解决此问题,我将仅检查用户是否存在于数据库中,但我想确保没有更简单的选择.

I couldn't find anything that allowed me to check if a new user was being generated in the Firebase docs. It seems to silently handle new Firebase auth user generation when logging in through Facebook for the first time. If there is no built-in way for Firebase to handle this I will just check if the user exists in my database, but I wanted to be sure there wasn't a simpler alternative.

推荐答案

您可以按照以下步骤检测用户是新用户还是现有用户:

You can detect if a user is new or existing as follows:

firebase.auth().signInWithPopup(provider)
  .then(function(result) {
    console.log(result.additionalUserInfo.isNewUser);
  })
  .catch(function(error) {
    // Handle error.
  });

有关此内容的更多信息,请参阅文档.

For more on this refer to the documentation.

这篇关于Firebase Auth检查Facebook上是否有新用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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