Firebase Auth,如何知道新用户注册,而不是现有用户登录? [英] Firebase Auth, how to know new user signed up, rather than existing user sign in?

查看:85
本文介绍了Firebase Auth,如何知道新用户注册,而不是现有用户登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是我想让新注册的用户充实基本信息,例如他们的姓名. 所以我希望这样做:

My use case is that I want to ask newly signed up users to enrich basic info like their names. So I was hoping to do it like:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    if (some indicator tells me it is newly signed up user)
        {redirect to a form to fill in more info}
  } else {
    // No user is signed in.
  }
});

我检查了文档,但找不到与此相关的任何内容...

I checked the doc, and could not find anything related to this...

谢谢您的帮助.

推荐答案

从4.6.0版开始: https://firebase.google.com/support/release-notes/js#4.6.0 您可以通过2种方式来确定用户是新用户还是现有用户:

Since version 4.6.0: https://firebase.google.com/support/release-notes/js#4.6.0 You can get if a user is new or existing in 2 ways:

  1. 如果要返回UserCredential结果,请检查result.additionalUserInfo.isNewUser

检查firebase.auth().currentUser.metadata.creationTime === firebase.auth().currentUser.metadata.lastSignInTime

以前,您必须自己完成操作,并使用Firebase Realtime Database跟踪用户.用户登录后,您将检查数据库中是否存在具有指定uid的用户.如果找不到该用户,则该用户是新用户,然后可以将该用户添加到数据库中.如果该用户已经在数据库中,则这是一个返回的现有用户.这是iOS中的示例.

Previously you had to do that on your own and keep track of the user using Firebase Realtime Database. When a user signs in, you check if a user with the specified uid exists in the database or not. If the user was not found, it is a new user, you can then add the user to the database. If the user is already in the database then this is a returning existing user. Here is an example in iOS.

处理Firebase + Facebook登录过程

使用result.additionalUserInfo.isNewUser的示例:

firebase.auth().signInWithPopup(provider).then((result) => {
  console.log(result.additionalUserInfo.isNewUser);
});

这篇关于Firebase Auth,如何知道新用户注册,而不是现有用户登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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