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.

7
  • Have you set up your project in the Firebase console and added the google-services.json file to your /app directory ? Commented Nov 13, 2016 at 9: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 Commented Nov 13, 2016 at 9:44
  • Can you check your Logcat when you press the register button ? Firebase usually gives some sort of error message. Commented Nov 13, 2016 at 9: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 Commented Nov 13, 2016 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: stackoverflow.com/a/39427322/4815718 Commented Nov 13, 2016 at 13:42

8 Answers 8

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

5

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

enter image description here

Comments

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().

3 Comments

I also have a log in method but this is indeed first time creation of a user
So when the completion task is not successful, what is task.getException().getMessage()?
This answer should be on top
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.

Comments

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.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.