I know there's a few question like this but they either are not what I wanted or I'm just somehow not able do it. What I want is the user, during registration, are required to write username, email and password. Then when user sign in, user can use username/email to login. I found this post:
Firebase login and signup with username
But just like this guy, I don't know how others link to my code. I did try the method in the answer but failed. Do I need to store the username in the database? If yes, how? Really hoping you guys can help me, still a rookie here.
SignUpActivity:
public class SignUpActivity extends AppCompatActivity implements View.OnClickListener {
@InjectView(R.id.fab)
FloatingActionButton fab;
@InjectView(R.id.cv_add)
CardView cvAdd;
private EditText editTextEmail;
private EditText editTextPassword;
private EditText editTextUsername;
private ProgressDialog progressDialog;
private Button bt_signup;
private FirebaseAuth firebaseAuth;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
ButterKnife.inject(this);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() != null){
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextUsername = (EditText) findViewById(R.id.editTextUsername);
bt_signup = (Button) findViewById(R.id.bt_signup);
progressDialog = new ProgressDialog(this);
bt_signup.setOnClickListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ShowEnterAnimation();
}
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateRevealClose();
}
});
}
private void registerUser(){
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
final String username = editTextUsername.getText().toString().trim();
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(username)){
Toast.makeText(this,"Please enter username",Toast.LENGTH_LONG).show();
return;
}
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
User newUser = new User(firebaseAuth.getCurrentUser().getEmail(),username);
mDatabase.child(firebaseAuth.getCurrentUser().getUid()).setValue(newUser);
finish();
startActivity(new Intent(getApplicationContext(), SignInActivity.class));
}else{
Toast.makeText(SignUpActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
private void ShowEnterAnimation() {
Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
getWindow().setSharedElementEnterTransition(transition);
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {
cvAdd.setVisibility(View.GONE);
}
@Override
public void onTransitionEnd(Transition transition) {
transition.removeListener(this);
animateRevealShow();
}
@Override
public void onTransitionCancel(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
}
});
}
public void animateRevealShow() {
Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth()/2,0, fab.getWidth() / 2, cvAdd.getHeight());
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
@Override
public void onAnimationStart(Animator animation) {
cvAdd.setVisibility(View.VISIBLE);
super.onAnimationStart(animation);
}
});
mAnimator.start();
}
public void animateRevealClose() {
Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd,cvAdd.getWidth()/2,0, cvAdd.getHeight(), fab.getWidth() / 2);
mAnimator.setDuration(500);
mAnimator.setInterpolator(new AccelerateInterpolator());
mAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
cvAdd.setVisibility(View.INVISIBLE);
super.onAnimationEnd(animation);
fab.setImageResource(R.drawable.plus);
SignUpActivity.super.onBackPressed();
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
mAnimator.start();
}
@Override
public void onBackPressed() {
animateRevealClose();
}
@Override
public void onClick(View view) {
if(view == bt_signup){
registerUser();
}
}
}
User.Java
import com.google.firebase.database.IgnoreExtraProperties;
@IgnoreExtraProperties
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password)
{
this.username = username;
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
My database Data stored from my other activity.
I turned some of the code into comments, those are the code when I attempt to store the username in the database, but failed even though my other activity use the same method. I've spent the whole morning just trying to allow users to login with username, please help.
Edited: Added mDatabase.
Edited(2): Update SignUpActivity, added database and rules.