Reputation: 3
I'm new to firebase. My app gets crashes whenever I click on Sign up button. I just tried to create new user authentication and a email verification for that user and finally uploading the user data to firebase. Successfully I get authentication and email verification, but not uploading my data.. I checked logcat, it shows I get error in uploading details.. firebaseAuth.getUid() returns null present in sendUserData() function..what to do if it returns null or how to stop returning null. Help me to update this data to firebase. I dont know how to solve.. please help me out.. Check out the last function sendUserData()
public class RegisterActivity extends AppCompatActivity {
private EditText name,password,email,age;
private Button sign_up;
private TextView login;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
private ImageView profilePic;
public String Username,UserPass,UserAge,UserEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
setUp();
firebaseAuth=FirebaseAuth.getInstance();
progressDialog=new ProgressDialog(this);
sign_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressDialog.setMessage("Signing up");
progressDialog.show();
if(validate()){
String user_email=email.getText().toString().trim();
String user_name=name.getText().toString().trim();
String user_password=password.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
progressDialog.dismiss();
sendEmail();
firebaseAuth.signOut();
}
else{
progressDialog.dismiss();
Toast.makeText(RegisterActivity.this,"Registration failed",Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(RegisterActivity.this,MainActivity.class));
}
});
}
private void setUp(){
name=(EditText)findViewById(R.id.etName);
password=(EditText)findViewById(R.id.etPassword);
email =(EditText)findViewById(R.id.etEmail);
sign_up=(Button)findViewById(R.id.btSignUp);
login=(TextView)findViewById(R.id.tvlogin);
age=(EditText)findViewById(R.id.etAge);
profilePic=(ImageView)findViewById(R.id.ivProfile);
}
private Boolean validate(){
Boolean result=false;
Username=name.getText().toString();
UserPass=password.getText().toString();
UserEmail=email.getText().toString();
UserAge=age.getText().toString();
if(Username.isEmpty() || UserPass.isEmpty() || UserEmail.isEmpty() || UserAge.isEmpty()){
Toast.makeText(this,"Please enter all details",Toast.LENGTH_SHORT).show();
}
else{
result=true;
}
return result;
}
private void sendEmail(){
FirebaseUser firebaseUser=firebaseAuth.getCurrentUser();
if(firebaseUser!=null){
firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
sendUserData();
Toast.makeText(RegisterActivity.this,"Email sent",Toast.LENGTH_LONG).show();
firebaseAuth.signOut();
finish();
}
else{
Toast.makeText(RegisterActivity.this,"Email not sent, Try again later",Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void sendUserData(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());
UserProfile userProfile = new UserProfile(UserAge, UserEmail, Username);
databaseReference.setValue(userProfile);
}
}
I created a class UserProfile and passed these 3 parameter to the constructor which I created in UserProfile class..And I also tried it with if condition matching firebaseAuth.getUid()==null and else part with Toast message.. It always prints toast message, not uploading my data to firebase.. I'm new to this...So please post what I need to with explanation..Hope you get my question.. Thank you
Upvotes: 0
Views: 232
Reputation: 317352
You're calling firebaseAuth.signOut()
before sendEmail()
has a chance to fully complete. In other words, the UID is null because you signed out the user before calling firebaseAuth.getUid()
. You probably don't want to sign them out at all automatically - that typically only happens when the user chooses to do so.
Upvotes: 1