2017-08-06 41 views
1

我的使用案例是我想问新近注册的用户,以丰富他们的姓名等基本信息。 所以我希望能做到这一点,如:Firebase Auth,如何知道注册新用户,而不是现有用户登录?

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

我查了文档,无法找到与此相关的任何东西......

感谢提前的帮助。

+0

我将用户存储在数据库中,并带有“onboarded”字段。我检查了这一点,以确保它们在继续使用之前已经上报。在此身份验证状态中没有“开箱即用”功能。 – theblindprophet

回答

2

由于4.6.0版本:https://firebase.google.com/support/release-notes/js#4.6.0 你可以,如果用户是新的或现有的2种方式获得:

  1. 如果要取回一个UserCredential结果,检查result.additionalUserInfo.isNewUser

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

此前,你必须自己做,并且记住使用Firebase实时数据库的用户跟踪。当用户登录时,您检查数据库中是否存在具有指定uid的用户。如果找不到该用户,则它是一个新用户,然后可以将该用户添加到数据库。如果用户已经在数据库中,那么这是一个返回的现有用户。这里是iOS中的一个例子。

Handing Firebase + Facebook login process

+0

谢谢。到目前为止,这也是唯一的想法。毕竟,在Firebase身份验证控制台中,我希望auth本身提供API来执行此操作,它拥有注册用户列表。如果我可以直接在auth控制台中查看uid列表,那将会非常棒。但只是一厢情愿的想法... – LeonTan

+0

Firebase添加了一个标志来检查用户是新的还是现有的:https://firebase.google.com/support/release-notes/js#4.6.0 – bojeil

+0

我在这里找到它:任务.getResult()。getAdditionalUserInfo()。isNewUser() –

1

有一两件事你可以做的是做事情的注册功能的回调函数,该函数注册做回一个承诺。你可以做这样的事情:

firebase.auth().createUserWithEmailAndPassword(email, password) 
.then(function(user) { 
    //I believe the user variable here is the same as firebase.auth().currentUser 
    //take the user to some form you want them to fill 
}) 
.catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    // ... 
}); 

enter image description here

不过,我真的不建议做这种方式,因为客户端代码是不可靠的。想一想,如果用户在填写表单之前突然断开连接,该怎么办?他们的数据在您的数据库中不完整。因此,如果您这样做,请在提交表单时在用户个人资料中设置一个标志,以便知道谁填充了详细信息,谁不填写。

另一个更好的方法是使用firebase云功能。你可以在你的云功能中拥有这样的代码。云功能是用node.js编写的,所以你不需要花时间在另一种语言上。

exports.someoneSignedUp = functions.auth.user().onCreate(event => { 
    // you can send them a cloud function to lead them to the detail information form 
    //or you can send them an welcome email which will also lead them to where you want them to fill detailed information 
}); 

这种方式好多了,因为您可以安全地假设您的云服务器永远不会关闭或受到威胁。有关云功能的更多信息,请参阅他们的文档:https://firebase.google.com/docs/functions/auth-events