-1

Here's my code in which I'm using Firebase to register a user and using username instead of email for login/register.

I searched a lot but wasn't able to find a solution for it. Any help regarding this method will be appreciated.

 reg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    {
                        String username=userName.getText().toString().trim();  //getting username
                        String email=memail.getText().toString().trim();
                        String pass=password.getText().toString().trim();
                        String cpass=confirmPass.getText().toString().trim();
    
                        if( TextUtils.isEmpty(email)){
                            memail.setError("Email is required");
                            return;
                        }
                        if(TextUtils.isEmpty(username)){
                            userName.setError("Username is required");
                            return;
                        }
                        if(TextUtils.isEmpty(pass)){
                           password.setError("Password is required");
                            return;
                        }
                        if(pass.length()<6){
                            password.setError("Password length should be greater than 6");
                            return;
                        }
                        if(TextUtils.isEmpty(cpass)){
                            confirmPass.setError("Password is required");
                            return;
                        }
                        if(TextUtils.equals(pass,cpass)==false){
                            confirmPass.setError("Passwords do not match");
                            return;
                        }
                        progBar.setVisibility(View.VISIBLE);
    
                        fauth.createUserWithEmailAndPassword(username,pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if(task.isSuccessful()){
                                    Toast.makeText(Signup.this,"Registeration Successfull",Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                                }
                                else{
                                    Toast.makeText(Signup.this,"Error !"+ task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
    
                    }
                }
            });`
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Any indication of what you've tried that didn't work would be extremely helpful – roflcopter1101 Jun 19 '22 at 14:55
  • Method `createUserWithEmailAndPassword` takes an email not a username. – Roddy of the Frozen Peas Jun 19 '22 at 14:55
  • I want the user to login using his username instead of email, what can I do for that ? – Hamza Butt Jun 19 '22 at 15:18
  • If you consider at some point in time implementing Firebase sign-in with Google, then I recommend you check this [resource](https://medium.com/firebase-tips-tricks/how-to-create-a-clean-firebase-authentication-using-mvvm-37f9b8eb7336). If you understand Kotlin, you can check [this](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) out. – Alex Mamo Jun 20 '22 at 07:12

1 Answers1

1

You're not validating the address against Patterns.EMAIL_ADDRESS, which equals no validation.

if (TextUtils.isEmpty(email)) {
    memail.setError("Email is required");
    return;
}

if (! Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
    memail.setError("Email is invalid");
    return;
}

And it's not being passed either: fauth.createUserWithEmailAndPassword(email, pass).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • the method fauth.createUserWithEmailAndPassword takes an email but I want the user to login using username and password – Hamza Butt Jun 19 '22 at 15:16
  • 1
    @HamzaButt "I want the user to login using username and password" That's not what `createUserWithEmailAndPassword` does though. As its name implies, it creates a user based on an email address and a password. If you're not passing in an email address, the error message would be expected. I recommend searching for [firebase authentication username and password](https://www.google.com/search?q=firebase+authentication+username+and+password) as this has been covered quite regularly before. – Frank van Puffelen Jun 19 '22 at 15:58