Home > Mobile >  I can't save Users data on realtime Firebase
I can't save Users data on realtime Firebase

Time:03-12

I want to save every name, password, and other information of each user in a table in Firebase Realtime Database.

With this, I can only see who create an account but I have no access to the rest of the user's information like passwords, names.

Users register through the Authentification method of email and password and log in using the same details.

How also Block registration access to a certain type of 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)));

    }
}

CodePudding user response:

I want to save every name, password, and other information of each user in a table in Firebase Realtime Database.

You just should never store the user's credentials in the way you intend to, as storing passwords in cleartext is one of the worst security risks you can inflict on your users. See for example:

With this I can only see who create an account but I have no access to the rest of the user's information like password, name.

You cannot see any other information because when you authenticate your users with email and password, the only information that you have is the email and the UID of the user. That's it. There isn't any other information.

If you want to have additional information about your users, you should consider implementing an authentication mechanism with one of the available providers.

How also to Block registration access to a certain type of email?

You should consider using security rules.

CodePudding user response:

How also Block registration access to a certain type of email?

Assuming your database tree looks like this:

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

You should add another boolean variable to your users called 'blocked' and define it as false. Write a code that lets you change this variable to true, so you could block the user.

Then go to your firebase rules and put this code:

  {
     "rules": {

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

}
}
}
}

If(!true) the user is blocked and the read == false, otherwise the user can read from his data from the databse and log in.

  • Related