I know this has been discussed plenty of times, unfortunately I am a noob and didn`t find the answers useful enough. Basically, my app registers the users to Firebase using the email and pass registration method. I also need another 2 fields for the users, name and lastname. I understand these have to be saved in the Realtime Database. This is my code:
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
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.FirebaseAuth;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.database.FirebaseDatabase;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText editTextEmail;
EditText editTextPassword;
EditText editTextRePassword;
EditText editTextName;
EditText editTextLastName;
Button buttonSignup;
TextView textViewSignin;
ProgressDialog progressDialog;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing firebase auth object
firebaseAuth = FirebaseAuth.getInstance();
//if getCurrentUser does not returns null
if(firebaseAuth.getCurrentUser() != null){
//that means user is already logged in
//so close this activity
finish();
//and open profile activity
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
//initializing views
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextRePassword = (EditText) findViewById(R.id.editTextRePassword);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextLastName = (EditText) findViewById(R.id.editTextLastName);
textViewSignin = (TextView) findViewById(R.id.textViewSignin);
buttonSignup = (Button) findViewById(R.id.buttonSignup);
progressDialog = new ProgressDialog(this);
//attaching listener to button
buttonSignup.setOnClickListener(this);
textViewSignin.setOnClickListener(this);
}
private boolean isValidEmail(String email){
boolean isValidEmail = false;
String regExpn = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (email.matches(regExpn))
{
isValidEmail = true;
}
return isValidEmail;
}
private void registerUser(){
//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
String repass = editTextRePassword.getText().toString().trim();
String name = editTextName.getText().toString().trim();
String lastname = editTextLastName.getText().toString().trim();
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter the password",Toast.LENGTH_LONG).show();
return;
}
else if(TextUtils.isEmpty(email)){
Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show() ;
return;
}
else if (TextUtils.isEmpty(repass)){
Toast.makeText(this,"Please repeat the password",Toast.LENGTH_LONG).show();
return;
}
else if (!password.equals(repass)) {
Toast.makeText(this,"You must input the same password as in the previous field",Toast.LENGTH_LONG).show();
return;
}
else if(!isValidEmail(email.toString().trim()))
{
Toast.makeText(this,"Please use a valid email address",Toast.LENGTH_LONG).show();
return;
}
else if (password.length()<6) {
Toast.makeText(this,"Your password must be at least 6 characters long",Toast.LENGTH_LONG).show();
return;
}
else if(TextUtils.isEmpty(name)){
Toast.makeText(MainActivity.this, "Please enter your name", Toast.LENGTH_LONG).show() ;
return;
}
else if (TextUtils.isEmpty(lastname)){
Toast.makeText(this,"Please input your last name",Toast.LENGTH_LONG).show();
return;
}
progressDialog.setMessage("Registering, please wait...");
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}else{
//display some message here
Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
@Override
public void onClick(View view) {
if(view == buttonSignup){
registerUser();
}
if(view == textViewSignin){
//open login activity when user taps on the already registered textview
startActivity(new Intent(this, LoginActivity.class));
}
}
}
Any help appreciated.