FirebaseAuth未创建用户

时间:2019-06-26 09:03:54

标签: java android firebase firebase-authentication

当我尝试注册时,它说注册失败。 经过一些调整后,这是代码的更新版本。 我试图分析代码没有运气。

package com.brandshopping.brandshopping;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class RegisterActivity extends AppCompatActivity {

private EditText Input_email;
private EditText Input_password;
private EditText Input_confirm_password;
private EditText Input_first_name;
private EditText Input_last_name;
private EditText Input_phonenumber;
private Button SignUpBtn;
private TextView I_Have_An_Account_Btn;
private ProgressDialog LoadingBar;

private FirebaseAuth firebaseAuth;

public class RegisterActivity extends AppCompatActivity {

private EditText Input_email;
private EditText Input_password;
private EditText Input_confirm_password;
private EditText Input_first_name;
private EditText Input_last_name;
private EditText Input_phonenumber;
private Button SignUpBtn;
private TextView I_Have_An_Account_Btn;
private ProgressDialog LoadingBar;

private FirebaseAuth firebaseAuth;

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


    LoadInGui(); //Loads in GUI


    I_Have_An_Account_Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(RegisterActivity.this, MainActivity.class));
        }
    });

    SignUpBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final String email = Input_email.getText().toString().trim();
            final String password = Input_password.getText().toString().trim();

            validateCredentialls();
            doNormalLogin(email, password);

        }
    });


}

private void validateCredentialls(){
    if(TextUtils.isEmpty(Input_first_name.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're first name", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_last_name.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're last name", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_email.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're e-mail", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_phonenumber.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're phone number", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_password.toString())){
        Toast.makeText(RegisterActivity.this, "Please enter you're password", Toast.LENGTH_SHORT).show();
    }

    else if(TextUtils.isEmpty(Input_confirm_password.toString())){
        Toast.makeText(RegisterActivity.this, "Please write you're password again", Toast.LENGTH_SHORT).show();
    }
}

private void doNormalLogin(String email, String password){
    FirebaseAuth mAuth = FirebaseAuth.getInstance();
    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                Log.e("SignUpError :", task.getException().getMessage());
            }
        }
    });
}

private void LoadInGui()
{
    Input_email = (EditText) findViewById(R. id. input_email);
    Input_password = (EditText) findViewById(R. id. input_password);
    Input_confirm_password = (EditText) findViewById(R. id. input_confirm_password);
    Input_first_name = (EditText) findViewById(R. id. Input_First_Name);
    Input_last_name = (EditText) findViewById(R. id. Input_Last_Name);
    Input_phonenumber = (EditText) findViewById(R. id. input_phonenumber);

    SignUpBtn = (Button) findViewById(R. id. register_btn);
    I_Have_An_Account_Btn = (TextView) findViewById(R. id. I_Have_An_Account_Btn);

}

}

我希望程序可以注册我,并且确实存在。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您正在从RegisterUser()调用onCreate()方法,这就是为什么在插入任何信息之前调用此方法的原因。

因此,仅当用户单击“注册”按钮时才从RegisterUser()中删除onCreate()呼叫。

答案 1 :(得分:1)

    Remove  RegisterUser(); from onCreate

    and try this - 

        private void doNormalLogin(String email, String password){
      FirebaseAuth mAuth = FirebaseAuth.getInstance();
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                    Toast.makeText(RegisterActivity.this, "Registration successful", Toast.LENGTH_SHORT).show();
                    } else {
                    Toast.makeText(RegisterActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

Make sure you added this in your app gradle dependencies  and the bottom plugin also - 

  dependencies {
      implementation fileTree(include: ['*.jar'], dir: 'libs')
      implementation 'com.google.firebase:firebase-auth:11.2.2'
      implementation 'com.google.firebase:firebase-database:11.2.2'
  }
  apply plugin: 'com.google.gms.google-services'

  And in your Project gradle -

  dependencies {
       classpath 'com.android.tools.build:gradle:3.2.1'
       classpath 'com.google.gms:google-services:4.2.0'
   }