0

I want the user to get a specified amount (500) of money (int) once they get registered into the system with Firebase Auth. The email (with a , instead of a .) is taken from the input of the user which will be the userid (child) in realtime database. And the money should be the child of userid.

I tried it with my writeNewUserMoney method: realtime database

I tried it with my writeNewUserMoney method:

public void writeNewUserMoney() {
    String email = encodeUserEmail(signupEmail.getText().toString().trim());
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();
    mDatabaseReference.child("user").child(email).setValue(500);
}

Here is encodeUserEmail:

static String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

Here is the relevant part of the class:

@Override
public void onClick(View view) {
    String user = signupEmail.getText().toString().trim();
    String pass = signupPassword.getText().toString().trim();
    if (user.isEmpty()){
        signupEmail.setError("Email eingeben");
    }
    if (pass.isEmpty()){
        signupPassword.setError("Passwort eingeben");
    } else{
        auth.createUserWithEmailAndPassword(user, pass).addOnCompleteListener(new                         OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(SignUpActivity.this, "SignUp Successful", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
                    } else {
                        Toast.makeText(SignUpActivity.this, "SignUp Failed" + task.getException().getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
        });
    }
//
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseUser kuser = auth.getCurrentUser();
    //bestätigungs mail
    kuser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    System.out.println("Email sent");
                }
            }
    });
    //bestätigungs mail
    //Kontoinstandsetzung
    writeNewUserMoney();
}

The database doesn't change when initiating an onClick. I already tried this : Fail to Write data to Firebase Realtime Database Android

If you know any other methods to assign a value in realtime database after the or with reg without the user seeing it, feel free to comment.

The Region

Jonasoos
  • 1
  • 4
  • I don't immediately see what's going wrong in the code. You might want to [add a completion listener](https://firebase.google.com/docs/database/android/read-and-write#add_a_completion_callback) to your `setValue` call to see if it raises any error. --- If that doesn't show anything: is there any other place in your code where you access the database that *does* work? If not, you might want to try a simpler `FirebaseDatabase.getInstance().getReference().child("Test").setValue(true)` call. If that also doesn't work, it may be that your database URL is not set correctly. – Frank van Puffelen Apr 12 '23 at 17:03
  • Please edit your question and add the information Frank asked for, and please also respond using @. – Alex Mamo Apr 13 '23 at 05:40
  • @FrankvanPuffelen van Puffelen, used a completion listener and it turns out it get never got completed. I found this error: Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Database lives in a different region. Please change your database URL to-- but i already choose my region ( eu- west). I added a picture of it in my question – Jonasoos Apr 13 '23 at 09:28
  • Read it wrong: I habe to change the URL, you were right @Frank van Puffelen. Thank you – Jonasoos Apr 13 '23 at 09:35

0 Answers0