[英]FirebaseAuth is not creating user
當我嘗試注冊時,它說注冊失敗。 經過一些調整后,這是代碼的更新版本。 我試圖分析代碼沒有運氣。
打包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);
}
}
我希望該程序可以注冊我,並且確實可以。 在此先感謝您的幫助。
您正在從onCreate()
調用RegisterUser()
方法,這就是在插入任何信息之前調用此方法的原因。
因此,僅當用戶單擊“注冊”按鈕時才從onCreate()
刪除RegisterUser()
調用。
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'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.