Recently created a simple sign up page for my application, the page takes the two values of email and password, checks they are valid then passes them into a firebase function to create the user. The values are being passed correctly, however, I always get the message saying "Registration Failed". The firebase connection is active and I have allowed for users to sign up using email and password authenication.
I feel it is an issue with my build.gradle
files, maybe having the wrong dependencies, however, I am new to Firebase and can't figure this out.
Below is my Register form, build.gradle files and a console log of the error. Any help would be greatly appreciated.
Register Form
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
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 RegisterPage extends AppCompatActivity {
private EditText userEmail,userPassword;
private Button regButton;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_page);
setUpUIViews();
firebaseAuth = FirebaseAuth.getInstance();
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (validate()){
//Upload Data to the database
String user_email = userEmail.getText().toString().trim();
String user_password =
userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email,user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegisterPage.this, "Registration Complete", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterPage.this,MainActivity.class));
}
else {
// FirebaseAuthException e = (FirebaseAuthException)task.getException();
// Toast.makeText(RegisterPage.this,"Failed Registation: "+e.getMessage(),Toast.LENGTH_SHORT).show();
Toast.makeText(RegisterPage.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
private void setUpUIViews(){
userEmail = (EditText)findViewById(R.id.Email_Register);
userPassword = (EditText)findViewById(R.id.Password_Register);
regButton = (Button)findViewById(R.id.Register_btn);
}
private Boolean validate(){
Boolean result = false;
String email = userEmail.getText().toString();
String password = userPassword.getText().toString();
if (email.isEmpty() && password.isEmpty()){
Toast.makeText(this,"Please enter all your details correctly", Toast.LENGTH_SHORT).show();
}
else {
result = true;
}
return result;
}
}
Build.Gradle (project) buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.1.0'
}
}
allprojects {
repositories {
google()
jcenter()
//added maven
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.Gradle (app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.bradwaterhouse.discovernottingham"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-database:11.0.4'
implementation 'com.google.firebase:firebase-auth:11.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//added in
compile 'com.google.firebase:firebase-auth:11.0.4'
}
apply plugin: 'com.google.gms.google-services'
Console log
This is the error message I get if i replace the "login failed" text in the else loop.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bradwaterhouse.discovernottingham, PID: 4473
java.lang.ClassCastException: com.google.firebase.FirebaseException cannot be cast to com.google.firebase.auth.FirebaseAuthException
at com.example.bradwaterhouse.discovernottingham.RegisterPage$1$1.onComplete(RegisterPage.java:59)
at com.google.android.gms.tasks.zzf.run(Unknown Source:23)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.
After speaking with OP, the error was:
I/error is: An internal error has occurred. [ Access Not Configured. Google Identity Toolkit API has not been used in project 248549959996 before or it is disabled. Enable it by visiting console.developers.google.com/… then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry. ]
To solve it he had to enable the google identity toolkit api.
Also it is better to add the below to be able to know what exactly the problem is:
Add this in your else, you will get the error more clearer:
Toast.makeText(RegisterPage.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
or add it in Log
:
Log.i("error is :", task.getException().getMessage());
You can avoid exception like below :-
if (ex instanceof FirebaseAuthException) {
FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) ex);
// TODO
} else {
// TODO
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.