Android – firebase code cannot create a new user
The code of my login / registration 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 part of the code, it will eventually display my toast message of "registration failed". I don't know why. I have tested the e-mail, password value and use toast message for authentication to ensure that they are delivered correctly. I have checked the code recommended by firebase to verify the user's identity on Android
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 also run it on the simulator
Please let me know if there is anything that needs to be edited to better solve the problem
Update: I registered my user account in my firebase, but the application is still sending me error messages
resolvent:
If you want to know the reason why the user creation failed, you should display the reason that firebase authentication provides for 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 strongly recommend that you do not use toasts to display such information, but (or otherwise) record it to obtain a permanent record at development time:
Log.e("LoginActivity", "Failed Registration", e);