5

The code for my login/register class is:

 package com.example.joshpc.bluetoothattendee;

 import android.app.ProgressDialog;
 import android.content.Intent;
 import android.support.annotation.NonNull;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 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;

 public class LoginActivity extends AppCompatActivity {

private EditText etEmail;
private EditText etPassword;
private EditText etRegPW;
private FirebaseAuth firebaseAuth;
private Button loginBut;
private Button regBut;
private ProgressDialog message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    etEmail = (EditText) findViewById(R.id.etEmail);
    etPassword = (EditText) findViewById(R.id.etPassword);
    etRegPW = (EditText) findViewById(R.id.etRegPW);

    firebaseAuth = FirebaseAuth.getInstance();
    loginBut = (Button) findViewById(R.id.bLogin);
    regBut = (Button) findViewById(R.id.bRegister);
    message = new ProgressDialog(this);



    regBut.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            userRegister();
        }
    });

    loginBut.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            userLogin();
        }
    });

}

private void userRegister(){
    String email = etEmail.getText().toString().trim();
    String password = etPassword.getText().toString().trim();
    String verify = etRegPW.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
        return;
    }
    if(TextUtils.isEmpty(password)){
        Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
        return;
    }
    Toast.makeText(this, email, Toast.LENGTH_SHORT).show();

    if(TextUtils.equals(password, verify)){
        message.setMessage("Registering User...");
        message.show();
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(LoginActivity.this, "Successful Registration", Toast.LENGTH_SHORT).show();
                            message.hide();
                            sendData();
                        }
                        if(!task.isSuccessful()){
                            Toast.makeText(LoginActivity.this, "Failed Registration", Toast.LENGTH_SHORT).show();
                            message.hide();
                            return;
                        }
                    }
                });
    }

    else {
        Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT).show();
        return;
    }

}

Whenever I run this portion of code, it ends up showing my toast message of "failed registration" and I'm not sure why. I have tested the values of email, password, and verify with toast messages to make sure they are being passed in correctly. I have checked the firebase suggested code to authenticate users on android as well.

my gradle build file is:

 apply plugin: 'com.android.application'

 android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
useLibrary 'org.apache.http.legacy'

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/NOTICE.txt'
}

defaultConfig {
    applicationId "com.example.joshpc.bluetoothattendee"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled true
        debug{debuggable = true}
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',           {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.google.android.gms:play-services-gcm:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

I am running this on an emulator as well.

if there is anything that needs to be edited in for better trouble shooting please let me know.

update: the user account i registered with IS showing up in my firebase, but the app is still kicking the error message out at me.

ThePeskyWabbit
  • 417
  • 1
  • 7
  • 23
  • Have you set up your project in the Firebase console and added the google-services.json file to your /app directory ? – rhari Nov 13 '16 at 09:42
  • just updated the post. The account i registered with is actually showing in my firebase console but the app is still showing me my error message – ThePeskyWabbit Nov 13 '16 at 09:44
  • Can you check your Logcat when you press the register button ? Firebase usually gives some sort of error message. – rhari Nov 13 '16 at 09:57
  • all its telling me is `W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.` which based on my research, is an error that should not effect functionality according to firebase dev – ThePeskyWabbit Nov 13 '16 at 10:01
  • Modify your completion listener code to log or toast the exception message when the task is not successful. Example code at this answer: http://stackoverflow.com/a/39427322/4815718 – Bob Snyder Nov 13 '16 at 13:42
  • @ThePpeskyWabbit did you solve the issue? I am facing the same problem except the user is not registered in firebase user section too. –  Jan 07 '18 at 13:57
  • @monu suri I did resolve it but its been so long now I cant remember what I even did! – ThePeskyWabbit Jan 25 '18 at 15:03

8 Answers8

9

If you want to know why creating the user fails, you should display the reason that Firebase Authentication gives you:

if(!task.isSuccessful()){
    FirebaseAuthException e = (FirebaseAuthException )task.getException();
    Toast.makeText(LoginActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();
    message.hide();
    return;
}

I highly recommend not using toasts to display this type of information, but instead (or additionally) also log it, so that you have a permanent record while developing:

Log.e("LoginActivity", "Failed Registration", e);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
6

try typing a longer password when you register your new user. Firebase require a password with >6 chars.

https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthWeakPasswordException

5

Make sure you enable Email in Firebase Authentication first. Otherwise, you could get this error.

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204
1

It sounds like you are confusing createUserWithEmailAndPassword() with signInWithEmailAndPassword(). You only need to create the user once. The call to create the user also signs him/her in. The user remains signed-in on the device until a sign-out. If another sign-in is needed, use signInWithEmailAndPassword(), not createUserWithEmailAndPassword().

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
0

To add onto Frank's answer you can also display the error message along with the general "Failed registration" in the toast using the method in task. task.getException() which returns a String.

    String s = "Sign up Failed" + task.getException();
    Toast.makeText(SignInActivity.this, s,
    Toast.LENGTH_SHORT).show();

*Note I separated the message displayed as a String for clarity.

This can display the error in the app itself.

0

Have you enabled your Sign-in providers using email method in your Firebase project?

In my case, I forgot to enable it so my exception is be like this:

D/AUTH: The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section.

Aji Imawan
  • 11
  • 3
0

I just wanted to add to make sure you check for google play services and if they are updated for your specific apk. Today I tried testing an app on an older phone that worked fine on a newer phone. I couldn't figure out why the "on failure listener" wasn't working, and by luck I stumbled on the google play services not being updated, which fixed the problem..

You have to check for that in code:

https://stackoverflow.com/a/35476962/11348687

wutamidoin
  • 61
  • 1
  • 6
0

check whether you have enabled email/password in firebase. go to firebase site, click on go to console, click on your project, then click on build and under build click on authentication. Then enable the email/password. Get back to android studio and run your project. This worked for me android studio 4.1.2.

Fahyma
  • 1
  • 2