簡體   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