在以下代码中,我尝试打开“登录”页面。用户将填写电子邮件和密码。当用户单击登录按钮时,将调用onComplete
方法。
我理解在package com.awani.pocketblog;
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;
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 LoginActivity extends AppCompatActivity {
private EditText mLoginEmailField;
private EditText mLoginPasswordField;
private Button mLoginButton;
private Button mNewAccountButton;
private FirebaseAuth mAuth;
private ProgressDialog mProgress;
private DatabaseReference mDatabaseUsers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mDatabaseUsers.keepSynced(true);
mLoginEmailField = (EditText) findViewById(R.id.loginEmailField);
mLoginPasswordField = (EditText) findViewById(R.id.loginPaswordField);
mLoginButton = (Button) findViewById(R.id.loginButton);
mNewAccountButton = (Button) findViewById(R.id.newAccountButton);
mProgress = new ProgressDialog(this);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkLogin();
}
});
}
private void checkLogin() {
//retrieve the data from database to check if user is logged in correctly
String email = mLoginEmailField.getText().toString().trim();
String password = mLoginPasswordField.getText().toString().trim();
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mProgress.setMessage("Checking Login...");
mProgress.show();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener < AuthResult > () {
@Override
public void onComplete(@NonNull Task < AuthResult > task) {
//the following if block is never executed....WHY?
if (task.isSuccessful()) {
// Toast.makeText(LoginActivity.this,"hi",Toast.LENGTH_LONG).show();
checkUserExist();
} else {
mProgress.dismiss();
Toast.makeText(LoginActivity.this, "Error Login", Toast.LENGTH_LONG).show();
}
}
});
}
}
private void checkUserExist() {
//retrieving UID
final String user_id = mAuth.getCurrentUser().getUid();
//check if the user with thi UID already exists
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(user_id)) {
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
} else {
Intent setUpIntent = new Intent(LoginActivity.this, SetUpActivity.class);
setUpIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(setUpIntent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
方法中,不会调用第1个if块检查成功(填写电子邮件,密码和单击登录)。我总是收到Toast消息“Error login”(else块)。
has_many :users association
答案 0 :(得分:0)
答案 1 :(得分:0)
要使用电子邮件/密码进行身份验证,必须先使用createUserWithEmailAndPassword()创建用户:
尝试使用给定的电子邮件地址创建新的用户帐户 密码。如果成功,它还会将用户签入应用程序
此示例在guide for password-based authentication:
的第4步中提供mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});