Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase code not creating new user

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.

like image 751
ThePeskyWabbit Avatar asked Nov 29 '22 22:11

ThePeskyWabbit


2 Answers

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);
like image 152
Frank van Puffelen Avatar answered Dec 04 '22 11:12

Frank van Puffelen


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

like image 21
hampus millestu Avatar answered Dec 04 '22 13:12

hampus millestu