Firebase电子邮件和密码验证与Android - 用户注册 [英] Firebase Email and Password Authentication with android - User sign up
问题描述
最近为我的应用程序创建了一个简单的注册页面,该页面采用了电子邮件和密码的两个值,检查它们是否有效,然后将它们传递给firebase函数以创建用户。值正在正确传递,但是,我总是收到消息注册失败。 firebase连接处于活动状态,我允许用户使用电子邮件和密码验证进行注册。
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.
我认为这是我的 build.gradle
文件的问题,可能有错误的依赖关系,但是,我我是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.
下面是我的Register表单,build.gradle文件和错误的控制台日志。任何帮助将不胜感激。
Below is my Register form, build.gradle files and a console log of the error. Any help would be greatly appreciated.
注册表
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(项目)
buildscript {
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'
控制台日志
如果我更换了,这是我收到的错误消息在else循环中登录失败文本。
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.
推荐答案
与OP交谈后,错误是:
After speaking with OP, the error was:
我/错误是:发生内部错误。 [访问未配置。之前或已禁用Google Identity Toolkit API尚未在项目248549959996中使用。访问console.developers.google.com/...启用它,然后重试。如果您最近启用了此API,请等待几分钟,以便将操作传播到我们的系统并重试。 ]
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. ]
要解决这个问题,他必须启用google identity toolkit api。
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();
或将其添加到日志
:
Log.i("error is :", task.getException().getMessage());
这篇关于Firebase电子邮件和密码验证与Android - 用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!