Firebase安卓:用户注册无法正常工作

huangapple go评论77阅读模式
英文:

FireBase android: User Registration is not working

问题

我已经在Android中创建了一个登录注册应用程序,并使用Firebase来存储数据。因此,在提供电子邮件和密码后,当我点击“注册”按钮时,它会显示“注册失败”通知。我应该怎么做呢?

以下是代码部分:

注册活动(Registration Activity)

package com.shankhadeep.firebasedemo;

// ...(省略导入部分)

public class Registration extends AppCompatActivity {

    // ...(省略变量声明部分)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        // ...(省略控件初始化部分)

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = edt_email_reg.getText().toString().trim();
                String password = edt_password_reg.getText().toString().trim();

                if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
                    Toast.makeText(Registration.this, "Empty Credentials", Toast.LENGTH_LONG).show();
                } else if (password.length() < 6) {
                    Toast.makeText(Registration.this, "Password too short! Minimum 8 characters required", Toast.LENGTH_LONG).show();
                } else {
                    registerUser(email, password);
                }
            }
        });
    }

    private void registerUser(String email, String password) {
        auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(Registration.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(Registration.this, "Registration successful", Toast.LENGTH_LONG).show();
                    FirebaseUser user = auth.getCurrentUser();
                } else {
                    Toast.makeText(Registration.this, "Registration failed", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

Build.gradle(应用级别)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    // ...(省略Android配置部分)
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-analytics:17.5.0'
    // ...(省略其他依赖)
}

Build.gradle(项目级别)

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
        classpath 'com.google.gms:google-services:4.3.3'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我收到的通知:

(插入图片)

在Logcat中显示:

(插入图片)

希望以上信息对您有所帮助。

英文:

I have created one Login Registration app in android and using Firebase for storing the data. So, after giving email and password, when I'm clicking on the Register button, it's showing Registration Failed Notification. What should I do?

The codes are given Below:

Registration Activity

package com.shankhadeep.firebasedemo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
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;
import com.google.firebase.auth.FirebaseUser;
public class Registration extends AppCompatActivity {
EditText edt_email_reg, edt_password_reg;
Button btn_register;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
edt_email_reg = findViewById(R.id.edt_email_reg);
edt_password_reg = findViewById(R.id.edt_password_reg);
btn_register = findViewById(R.id.btn_register);
auth = FirebaseAuth.getInstance();
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email= edt_email_reg.getText().toString().trim();
String password = edt_password_reg.getText().toString().trim();
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)){
Toast.makeText(Registration.this,&quot;Empty Credentials&quot;,Toast.LENGTH_LONG).show();
}
else if(password.length()&lt;6){
Toast.makeText(Registration.this,&quot;Password too short! Minimum 8 characters required&quot;,Toast.LENGTH_LONG).show();
}
else{
registerUser(email,password);
}
}
});
}
private void registerUser(String email, String password) {
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(Registration.this, new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (task.isSuccessful()){
Toast.makeText(Registration.this,&quot;Registartion failed&quot;, Toast.LENGTH_LONG).show();
FirebaseUser user = auth.getCurrentUser();
}
else {
Toast.makeText(Registration.this,&quot;Registartion failed&quot;, Toast.LENGTH_LONG).show();
}
}
});
}
}

Build.gradle(app level)

apply plugin: &#39;com.android.application&#39;
apply plugin: &#39;com.google.gms.google-services&#39;
android {
compileSdkVersion 29
buildToolsVersion &quot;29.0.3&quot;
defaultConfig {
applicationId &quot;com.shankhadeep.firebasedemo&quot;
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName &quot;1.0&quot;
testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
}
}
}
dependencies {
implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])
implementation &#39;androidx.appcompat:appcompat:1.2.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:2.0.1&#39;
implementation &#39;com.google.firebase:firebase-database:17.0.0&#39;
implementation &#39;com.google.firebase:firebase-auth:17.0.0&#39;
implementation &#39;com.google.firebase:firebase-analytics:17.5.0&#39;
testImplementation &#39;junit:junit:4.12&#39;
androidTestImplementation &#39;androidx.test.ext:junit:1.1.2&#39;
androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.3.0&#39;
}

Build.gradle(project level)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath &#39;com.android.tools.build:gradle:3.5.3&#39;
classpath &#39;com.google.gms:google-services:4.3.3&#39;
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

**Here is the notification I'm getting: **

Firebase安卓:用户注册无法正常工作

In the Log-cat it's showing

Log-cat:

Any suggestion would be fine for me.

答案1

得分: 0

请在您的Firebase账户中检查此功能的启用状态,您可以在那里启用通过电子邮件登录的选项。

打开此菜单:
Firebase安卓:用户注册无法正常工作

英文:

Please, check the enable status this functionality into your Firebase account, where you can enable the option to login via email.

Open this menu:
Firebase安卓:用户注册无法正常工作

答案2

得分: 0

尝试这个

btn_register.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String email = edt_email_reg.getText().toString().trim();
        String password = edt_password_reg.getText().toString().trim();

        if (!validate(email, password)) {
            return;
        }

        registerUser(email, password);
    }
});

private boolean validate(String email, String password) {
    if (email.isEmpty() || password.isEmpty()) {
        Toast.makeText(Registration.this, "填写所有字段", Toast.LENGTH_LONG).show();
        return false;
    }

    if (password.length() < 8) {
        Toast.makeText(Registration.this, "密码太短!需要至少8个字符", Toast.LENGTH_LONG).show();
        return false;
    }

    return true;
}

private void registerUser(String email, String password) {
    auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(Registration.this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(Registration.this, "注册成功", Toast.LENGTH_LONG).show();

                FirebaseUser user = auth.getCurrentUser();

            } else {
                Toast.makeText(Registration.this, "注册失败", Toast.LENGTH_LONG).show();
            }
        }
    });
}
英文:

TRY THIS :

btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email= edt_email_reg.getText().toString().trim();
String password = edt_password_reg.getText().toString().trim();
if(!validate(email,password))
{
return;
}
registerUser(email,password);
}
});
private boolean validate(String email,String password)
{
if(email.isEmpty()||password.isEmpty())
{
Toast.makeText(Registration.this,&quot;Fill all the Fields&quot;, Toast.LENGTH_LONG).show();
return false;
}
if(password.length()&lt;8)
{
Toast.makeText(Registration.this,&quot;Password too short! Minimum 8 characters required&quot;,Toast.LENGTH_LONG).show();
return false;
}
return true;
}
private void registerUser(String email, String password)
{
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(Registration.this, new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (task.isSuccessful()){
Toast.makeText(Registration.this,&quot;Registartion Successful&quot;, Toast.LENGTH_LONG).show();
FirebaseUser user = auth.getCurrentUser();
}
else {
Toast.makeText(Registration.this,&quot;Registartion failed&quot;, Toast.LENGTH_LONG).show();
}
}
});
}

huangapple
  • 本文由 发表于 2020年9月14日 14:47:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63879309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定