为什么我没有使用FirebaseAuth注册用户?

时间:2017-11-26 08:54:25

标签: android firebase firebase-authentication

我正在学习使用firebase开发应用程序。但不知怎的,我无法注册我的用户。我仔细研究了一切,不知道错误是什么。它一直促使我"未能注册"但我不确定是什么错误。我已确保已启用电子邮件/密码登录方法。我已经确保google play版本高于firebase(' com.google.firebase:firebase-auth:11.0.4')。有人可以帮我这个。谢谢

更新:我收到错误消息 enter image description here

摇篮:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "co.xtvt.xtvt"
        minSdkVersion 19
        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-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'
}



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

xml:

<LinearLayout
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_margin="15dp"
        android:id="@+id/editTextEmailRegister"
        android:hint="Enter your email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_margin="15dp"
        android:id="@+id/editTextPasswordRegister"
        android:hint="Enter your password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_margin="15dp"
        android:id="@+id/buttonRegister"
        android:text="Register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textViewLogin"
        android:textAlignment="center"
        android:text="Already registered? Login here"
        android:layout_margin="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

java:

public class Register extends AppCompatActivity implements View.OnClickListener{

    private EditText editTextEmailRegister;
    private EditText editTextPasswordRegister;
    private TextView textViewLogin;
    private FirebaseAuth mAuth;

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

        editTextEmailRegister = (EditText) findViewById(R.id.editTextEmailRegister);
        editTextPasswordRegister = (EditText) findViewById(R.id.editTextPasswordRegister);

        findViewById(R.id.textViewLogin).setOnClickListener(this);
        findViewById(R.id.buttonRegister).setOnClickListener(this);

        mAuth = FirebaseAuth.getInstance();
    }

    public void RegisterUser(){
        String email = editTextEmailRegister.getText().toString().trim();
        String password = editTextPasswordRegister.getText().toString().trim();

        if (email.isEmpty()){
            editTextEmailRegister.setError("Email is required");
            editTextEmailRegister.requestFocus();
            return;
        }

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            editTextEmailRegister.setError("Email is invalid");
            editTextEmailRegister.requestFocus();
            return;
        }

        if (password.isEmpty()){
            editTextPasswordRegister.setError("Password is required");
            editTextPasswordRegister.requestFocus();
            return;
        }

        if (password.length() < 6){
            editTextPasswordRegister.setError("Minimum length is 6");
            editTextPasswordRegister.requestFocus();
            return;
        }

        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    Toast.makeText(Register.this, "Registered succesfully", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(Register.this, "Failed to register", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textViewLogin:
                startActivity(new Intent(this, Login.class));
                break;
            case R.id.buttonRegister:
                RegisterUser();
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要在android studio中使用firebase工具创建。在firebase控制台中手动创建,现在可以正常工作。