Firebase身份验证注册发生错误

时间:2017-11-17 01:15:34

标签: java android firebase firebase-authentication

我尝试使用Google Firebase身份验证选项在我的Android应用中注册用户,但我收到错误,我的Toast在此代码行中处理了我的错误Toast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();

我认为我的SHA-1和firebase连接是正确的。 Firebase控制台图片附在下面 firebase console

我在这里添加了我的源代码和xml文件。

package com.example.anu.activityone;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 RegistrationActivity extends AppCompatActivity {

private EditText txtEmail, txtPassword;
private Button btnRegister;

private FirebaseAuth mAuth;
/*
    public abstract class FirebaseAuth extends Object
    The entry point of the Firebase Authentication SDK.
 */
private FirebaseAuth.AuthStateListener FirebaseAuthStateListener;
/*  FirebaseAuth.AuthStateListener  is a interface,
    that Listener called when there is a change in the authentication state.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);

    // method called when user logged in or logged out
    mAuth = FirebaseAuth.getInstance();
    FirebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user != null){
                Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                return;
            }
        }
    };

    txtEmail = (EditText)findViewById(R.id.email);
    txtPassword = (EditText)findViewById(R.id.password);
    btnRegister = (Button)findViewById(R.id.register);

    // Register a new user
    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = txtEmail.getText().toString();
            final String password = txtPassword.getText().toString();
            /*
                Support User Registration
                With password-based authentication, new users must register themselves by providing
                a unique email address and a password. To add this functionality to your app,
                you can use the createUserWithEmailAndPassword() method of the FirebaseAuth class.
                As its name suggests, the method expects an email address and a password as its arguments.
             */
            mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                /*
                    public interface OnCompleteListener
                    Listener called when a Task completes.
                 */
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful()){
                        Toast.makeText(RegistrationActivity.this, "Sign Up Error!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            /*
                To be able to determine the result of the createUserWithEmailAndPassword() method,
                you must add an OnCompleteListener to it using the addOnCompleteListener() method.
             */
        }
    });
}
// To start Firebase authentication
@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(FirebaseAuthStateListener);
}
// To stop Firebase authentication when app pause
@Override
protected void onStop() {
    super.onStop();
    mAuth.removeAuthStateListener(FirebaseAuthStateListener);
}
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anu.activityone.RegistrationActivity"
android:orientation="vertical">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="email"
    android:id="@+id/email"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="password"
    android:id="@+id/password"/>

<Button
    android:id="@+id/register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Registration" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

试试这段代码:

Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();

获取登录失败的完整信息,然后使用“错误发生”更新您的问题

答案 1 :(得分:0)

我发现错误,我也使用Toast通过使用此代码行打印它

Toast.makeText(LoginActivity.this, "User Authentication Failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
相关问题