只有在数据库字段有效的情况下,如何在Firebase中创建用户? [英] How to create user in Firebase only if database fields are valid?

查看:152
本文介绍了只有在数据库字段有效的情况下,如何在Firebase中创建用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了许多有关其他用户数据字段的文章(在我的情况下,它只是唯一的用户名),我接触到最好的(我希望)保存和验证用户名的变体 - 使用用户名作为节点键在<$
$ b

我正在验证客户端和数据库规则内的用户名:
/ users / c $ c>:$ user.matches(/ ^ [0-9A-Z] {3,18} $ / i)&& newData.hasChildren(['uid'])&&在我的SignUp方法中,我遇到了一些问题,比如:

 $  createUserWithEmailAndPassword 方法成功承诺解析后,我将用户名保存到数据库中。 ($ user)= $ {
.createUserWithEmailAndPassword(payload.email,payload.password)
.then((user)=> {
//保存额外的用户信息
firebase
.database()
.ref(`users / $ {payload.username}`)
.set({
uid:user.uid,
$ b .then(()=> {
...



客户端验证是好的,但显然是不够的。我的应用程序工作正常,但如果我们禁用客户端验证的用户名,并尝试提交无效的用户名 - 用户将被创建没有用户名根本。



我看到的唯一解决方案是首先创建用户名节点,如果创建成功 - 尝试签署用户,如果签名不会抛出任何错误 - 继续执行程序,否则在 .catch 块中删除已经创建的用户名节点。但对我来说,这似乎是一个非常肮脏的解决方案。



我如何以优雅的方式解决这个问题?
谢谢!

解决方案

解决方案对我来说似乎相当脏和不雅。我打算做的是不去规则的方法来验证用户名。因为,正如你所指出的 - 你无法知道验证失败的时间。相反,我建议你在你的Cloud Functions实现中编写一个数据库触发器函数。当username节点被填充时,这个函数应该被触发。在这里,你可以验证用户名,以确保它符合你设定的​​任何标准。如果验证成功,您的客户端应用程序可能会向用户显示并进一步处理。

如果验证不成功,您可以要求用户输入另一个用户名。你可以在验证过程中显示一个进度条或类似的东西。



然而,我想补充一点,这可能只是这可以解决的方式,并不一定是最好的方式。虽然我想不出更好的办法,但如果有其他方法出现,我完全打算探索一种不同的方法!


After reading many articles about additional user data fields (in my case it's just unique username) in Firebase ecosystem, I approached the best (I hope) variant of saving and validating username - use the username as a node key under /users/.

I'm validating username on the client side and inside database rules: ": "$user.matches(/^[0-9A-Z]{3,18}$/i)&& newData.hasChildren(['uid']) && !data.parent().hasChild($user)",

I'm facing problems when signing user up. In my SignUp method I save username to the database in successful promise resolve of createUserWithEmailAndPassword method, here's a piece of code:

...
.createUserWithEmailAndPassword(payload.email, payload.password)
.then((user) => {
  // saving additional user information
  firebase
    .database()
    .ref(`users/${payload.username}`)
    .set({
      uid: user.uid,
    })
    .then(() => {
...

Client side validations is good, but obviously not enough. My application works ok, but if we disable client side validation of the username and try to submit invalid username - user will be created without username at all.

The only solution I see is to create username node first, if creation was successful - try to sign user up, if signing up won't throw any errors - continue program execution, otherwise in .catch block just delete created node with the username. But it seems like a very dirty solution to me.

How can I solve this problem in elegant way? Thanks!

解决方案

The solution seems quite dirty and inelegant to me too. What I'd propose to do is to not go with the rules method to validate the username. Because, as you pointed out - you have no way to know when the validation fails.

Instead, I'd suggest you to write a database trigger function in your Cloud Functions implementation. This function should be triggered when the "username" node is populated. In this, you can validate the username to ensure that it meets whatever criteria that you've set. If, the validation is successful, your client app could indicate that to the user and proceed further.

If the validation is unsuccessful, you could ask the user to input another username. You could even display a progress bar or something similar in the client while the validation process takes places.

However, I'd like to add that this might just be one of the ways in which this can be solved and is not necessarily the best way. Although I can't think of a better way, I am entirely open to explore a different method if something else comes up!

这篇关于只有在数据库字段有效的情况下,如何在Firebase中创建用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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