android - Firebase 代码不创建新用户

标签 android firebase firebase-authentication

我的登录/注册类的代码是:

 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;
    }

}

每当我运行这部分代码时,它最终都会显示“注册失败”的 toast 消息,我不确定为什么。我已经测试了电子邮件、密码的值,并使用 toast 消息进行了验证,以确保它们被正确传递。我也检查了 firebase 建议的代码以对 android 上的用户进行身份验证。

我的 gradle 构建文件是:

 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'

我也在模拟器上运行它。

如果需要编辑任何内容以便更好地解决问题,请告诉我。

更新:我注册的用户帐户显示在我的 firebase 中,但该应用程序仍在向我发送错误消息。

最佳答案

如果您想知道为什么创建用户失败,您应该显示 Firebase 身份验证给您的原因:

if(!task.isSuccessful()){
    FirebaseAuthException e = (FirebaseAuthException )task.getException();
    Toast.makeText(LoginActivity.this, "Failed Registration: "+e.getMessage(), Toast.LENGTH_SHORT).show();
    message.hide();
    return;
}

我强烈建议不要使用 toasts 来显示此类信息,而是(或另外)将其记录下来,以便您在开发时拥有永久记录:

Log.e("LoginActivity", "Failed Registration", e);

关于android - Firebase 代码不创建新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40572316/

相关文章:

java - 如何创建一个随机整数数组生成器

javascript - React Native 在不使用 native 组件的情况下从 http 播放 mp3 声音

android - 使用游标适配器和加载器时动态编辑 ListView 项

firebase - BigQuery 会向我收取从 Firebase Analytic 导出数据的费用吗

android - Firebase limitToLast(查询)不解析响应

firebase - 如何检查 Firebase 数据库中是否存在项目? - react native 火力基地

Android-如何将 Firebase Uid 保存到 SharedPreferences?

android - 建立项目时Android Studio错误

java - Firebase Auth有点dubbio

cordova - 无法谷歌登录 : auth/redirect-cancelled-by-user