繁体   English   中英

我无法实时保存用户数据 Firebase

[英]I can't save Users data on realtime Firebase

我想将每个用户的每个名称、密码和其他信息保存在 Firebase 实时数据库中的一个表中。

有了这个,我只能看到谁创建了一个帐户,但我无法访问 rest 的用户信息,如密码、名称。

用户通过 email 的身份验证方法和密码注册,并使用相同的详细信息登录。

如何也阻止注册访问某种类型的 email?

public class Register extends AppCompatActivity {

    public static final String TAG = "TAG";
    EditText mFullName,mEmail,mPassword,mPhone;
    Button mRegisterBtn;
    TextView mLoginBtn;
    FirebaseAuth fAuth;
    ProgressBar progressBar;
    FirebaseFirestore fStore;
    String userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        
        mFullName   = findViewById(R.id.fullName);
        mEmail      = findViewById(R.id.Email);
        mPassword   = findViewById(R.id.password);
        mPhone      = findViewById(R.id.phone);
        mRegisterBtn= findViewById(R.id.registerBtn);
        mLoginBtn   = findViewById(R.id.createText);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();
        progressBar = findViewById(R.id.progressBar);
        mRegisterBtn.setOnClickListener(v -> {
            final String email = mEmail.getText().toString().trim();
            String password = mPassword.getText().toString().trim();
            final String fullName = mFullName.getText().toString();
            final String phone    = mPhone.getText().toString();

            if(TextUtils.isEmpty(email)){
                mEmail.setError("Email manquant.");
                return;
            }
            if(TextUtils.isEmpty(password)){
                mPassword.setError("Mot de passe manquant.");
                return;
            }if(password.length() < 8){
                mPassword.setError("Le mot de passe doit contenir au moins 8 caractères");
                return;
            }
            progressBar.setVisibility(View.VISIBLE);
            // register the user in firebase
            fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(task -> {
                if(task.isSuccessful()){
                    // send verification link
                    FirebaseUser fuser = fAuth.getCurrentUser();
                    fuser.sendEmailVerification().addOnSuccessListener(aVoid -> Toast.makeText(Register.this,
                            "La vérification de l'email a été envoyée.", Toast.LENGTH_LONG).show()).addOnFailureListener(e ->
                            Log.d(TAG, "Echec: Email non envoyé " ));
                    Toast.makeText(Register.this, "Compte créé", Toast.LENGTH_LONG).show();
                    userID = fAuth.getCurrentUser().getUid();
                    DocumentReference documentReference = fStore.collection("users").document(userID);
                    Map<String,Object> user = new HashMap<>();
                    user.put("fName",fullName);
                    user.put("email",email);
                    user.put("phone",phone);
                    documentReference.set(user).addOnSuccessListener(aVoid -> Log.d(TAG, "Succès: profil utilisateur crée pour "+ userID))
                            .addOnFailureListener(e -> Log.d(TAG, "échec" + e.toString()));
                    startActivity(new Intent(getApplicationContext(),LoginActivity.class));
                }else {
                    Toast.makeText(Register.this, "Cette adresse email est déjà liée à un compte", Toast.LENGTH_LONG).show();
                    progressBar.setVisibility(View.GONE);
                }
            });
            
        });
        mLoginBtn.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(),LoginActivity.class)));

    }
}

我想将每个用户的每个名称、密码和其他信息保存在 Firebase 实时数据库中的一个表中。

永远不应该以您打算的方式存储用户的凭据,因为以明文形式存储密码是您可能给用户带来的最严重的安全风险之一。 参见例如:

有了这个,我只能看到谁创建了一个帐户,但我无法访问 rest 的用户信息,如密码、姓名。

您看不到任何其他信息,因为当您使用 email 和密码对用户进行身份验证时,您拥有的唯一信息是 email 和用户的 UID。 就是这样。 没有任何其他信息。

如果您想获得有关您的用户的其他信息,您应该考虑使用可用的提供者之一实施身份验证机制。

如何阻止注册访问某种类型的 email?

您应该考虑使用安全规则

如何也阻止注册访问某种类型的 email?

假设您的数据库树如下所示:

Users ---> uid ----> name 
                     password
                     email
                     etc...

您应该向您的用户添加另一个名为“blocked”的 boolean 变量,并将其定义为 false。 编写一段代码,让您将此变量更改为 true,这样您就可以阻止该用户。

然后 go 到您的 firebase 规则并放入此代码:

  {
     "rules": {

    "Users":{
      "$user":{
            ".read": "!data.child('blocked').val()",
            ".write":"auth.uid === $user"

}
}
}
}

If(,true) 用户被阻止并且读取 == false。 否则用户可以从数据库中读取他的数据并登录。

在上传用户之前,请确保您在用户 class 中创建了一个名为 isBlocked 的 boolean

user.put("isBlocked",false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM