Reputation: 5
I want to send data to my Realtime Database when i create new account. Creating new account i will see that in Authentication it has added new account but in my Realtime Database it hasn't added anything.
When debugging i see that in console Log.d(TAG, "onDataChange: Continue...");
doesn't show up and anything past that.
I'm following this tutorial https://www.youtube.com/watch?v=zPYqEpbihCs&list=PLgCYzUzKIBE9XqkckEJJA0I1wVKbUAOdv&index=28
Database structure: enter image description here
RegisterActivity.java Code that sends data to Firebase database
private void setupFirebaseAuth(){
Log.d(TAG, "setupFirebaseAuth: setting up firebase auth.");
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: Continue...");
//1st check: Make sure the username is not already in use
if(firebaseMethods.checkIfUsernameExists(username,
dataSnapshot)){
append = myRef.push().getKey().substring(3,10);
Log.d(TAG, "onDataChange: username already exists.
Appending random string to name: " + append);
}
username = username + append;
//add new user to the database
firebaseMethods.addNewUser(email, username, "", "", "");
Toast.makeText(mContext, "Signup successful. Sending
verification email.", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled: Cancelled ");
Log.e(TAG, "Failed to read
value.",databaseError.toException());
}
});
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
}
I use another file where i store all my FirebaseMethod logic
package com.example.searchapp.Utils;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.example.searchapp.R;
import com.example.searchapp.login.RegisterActivity;
import com.example.searchapp.models.User;
import com.example.searchapp.models.UserAccountSettings;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.EmailAuthCredential;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class FirebaseMethods {
private static final String TAG = "FirebaseMethods";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private String userID;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private Context mContext;
public FirebaseMethods(Context context){
mAuth = FirebaseAuth.getInstance();
mContext = context;
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
if(mAuth.getCurrentUser() != null){
userID = mAuth.getCurrentUser().getUid();
}
}
public boolean checkIfUsernameExists(String username, DataSnapshot dataSnapshot){
Log.d(TAG, "checkIfUsernameExists: check if " + username + "already exists");
User user = new User();
//dataSnapshot.child(userID).getChildren()
for(DataSnapshot ds: dataSnapshot.child(mContext.getString(R.string.dbname_users)).getChildren()){
Log.d(TAG, "checkIfUsernameExists: datasnapshot " + ds);
user.setUsername(ds.getValue(User.class).getUsername());
Log.d(TAG, "checkIfUsernameExists: username "+user.getUsername());
if(StringManipulation.expandUsername(user.getUsername()).equals(username)){
Log.d(TAG, "checkIfUsernameExists: FOUND A MATCH "+ user.getUsername());
return true;
}
}
return false;
}
public void registerNewEmail(final String email, String password, final String username){
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "onComplete: createUserWithEmail " + task.isSuccessful());
if(!task.isSuccessful()){
Toast.makeText(mContext, R.string.auth_failed,Toast.LENGTH_SHORT).show();
}
else if(task.isSuccessful())
{
userID = mAuth.getCurrentUser().getUid();
Log.d(TAG, "onComplete: Authstate changed " +userID );
}
}
});
}
public void addNewUser(String email, String username,String description,String website,String profile_photo){
User user = new User(userID,1,email,StringManipulation.condenseUsername(username));
myRef.child(mContext.getString(R.string.dbname_users))
.child(userID)
.setValue(user);
UserAccountSettings settings = new UserAccountSettings(
description,
username,
0,
0,
0,
profile_photo,
username,
website
);
myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.child(userID)
.setValue(settings);
}
}
Strings file
<resources>
<string name="app_name">SearchApp</string>
<string name="edit_profile">Edit Profile</string>
<string name="sign_out">Sign Out</string>
<string name="auth_failed">Failed to Authenticate</string>
<string name="auth_success">Authenticate Success</string>
<string name="dbname_users">users</string>
<string name="dbname_user_account_settings">user_account_settings</string>
</resources>
Upvotes: 0
Views: 519
Reputation: 598728
You're not attaching mAuthListener
to Firebase anywhere, which means that Firebase never calls it.
You'll need to call addAuthStateListener
to make Firebase aware of your listener, with something like:
FirebaseAuth.getInstance().addAuthStateListener(mAuthListener);
Upvotes: 1