[英]Firebase Email and Password Authentication with android - User sign up
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.最近为我的应用程序创建了一个简单的注册页面,该页面采用电子邮件和密码这两个值,检查它们是否有效,然后将它们传递给 firebase 函数以创建用户。 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. firebase 连接处于活动状态,我允许用户使用电子邮件和密码身份验证进行注册。
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.我觉得这是我的build.gradle
文件的问题,可能有错误的依赖项,但是,我是 Firebase 的新手,无法弄清楚这一点。
Below is my Register form, build.gradle files and a console log of the error.下面是我的注册表、build.gradle 文件和错误的控制台日志。 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 { Build.Gradle(项目) 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) Build.Gradle(应用程序)
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.这是我在 else 循环中替换“登录失败”文本时得到的错误消息。
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:与OP交谈后,错误是:
I/error is: An internal error has occurred. I/error 是:发生内部错误。 [ Access Not Configured. [未配置访问。 Google Identity Toolkit API has not been used in project 248549959996 before or it is disabled. Google Identity Toolkit API 之前未在项目 248549959996 中使用或已禁用。 Enable it by visiting console.developers.google.com/… then retry.通过访问 console.developers.google.com/... 启用它,然后重试。 If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.如果您最近启用了此 API,请等待几分钟,让操作传播到我们的系统并重试。 ] ]
To solve it he had to enable the google identity toolkit api.为了解决这个问题,他必须启用谷歌身份工具包 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:将此添加到您的 else 中,您将更清楚地看到错误:
Toast.makeText(RegisterPage.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
or add it in Log
:或将其添加到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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.