Firebase身份验证,如何知道新用户已注册,而不是现有用户登录?
我的用例是,我想让新注册的用户丰富基本信息,比如他们的名字。 所以我希望这样做:Firebase身份验证,如何知道新用户已注册,而不是现有用户登录?,firebase,firebase-authentication,Firebase,Firebase Authentication,我的用例是,我想让新注册的用户丰富基本信息,比如他们的名字。 所以我希望这样做: 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} } els
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.
}
});
我查了一下文件,找不到任何与此相关的东西
提前感谢您的帮助。自4.6.0版起:
如果用户是新用户或现有用户,可以通过以下两种方式获取:
UserCredential
结果,请检查result.additionalUserInfo.isNewUser
firebase.auth().currentUser.metadata.creationTime===firebase.auth().currentUser.metadata.lastSignInTime
result.additionalUserInfo.isNewUser
的示例:
firebase.auth().signInWithPopup(provider).then((result) => {
console.log(result.additionalUserInfo.isNewUser);
});
您可以做的一件事是在注册函数的回调函数中执行操作,注册函数返回一个承诺。您可以这样做:
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;
// ...
});
但是,我并不建议这样做,因为客户端代码可能不可靠。想想如果用户在填写表单之前突然断开连接会怎么样。他们的数据在您的数据库中不完整。因此,如果您这样做,请在用户提交表单时在其个人资料中设置一个标志,以便您知道谁填写了详细信息,谁没有填写
另一个更好的方法是使用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
});
这种方式要好得多,因为您可以安全地假设您的云功能服务器永远不会停机或受损。有关云功能的更多信息,您可以参考其文档:I在数据库中使用
内置的
字段存储用户。在他们继续之前,我检查了一下以确保他们已经上船了。在身份验证状态下没有“开箱即用”功能。谢谢。到目前为止,这也是唯一的想法。我希望auth本身提供一个API来完成这项工作:)毕竟,在Firebase auth控制台中,它有一个注册用户列表。如果我可以直接在auth控制台中对照列表检查uid,那就太好了。但这只是一厢情愿……Firebase添加了一个标志来检查用户是新用户还是现有用户:我在这里找到它:task.getResult().getAdditionalUserInfo().isNewUser()Firebase.auth().currentUser.metadata.creationTime==Firebase.auth().currentUser.metadata.lastSignInTime
它不工作!!每次我在注册后重新加载页面时,它都返回true。@只有在注销/登录间隔超过2分钟时,才会更新knotri lastSignInTime: