如何从Firebase数据库获取密码并与edittext匹配?

时间:2019-10-15 04:13:37

标签: java android firebase

我也是android studio和java的新手

我是在firebasedatabase代码上创建的,我想按如下所示进行编码

Class aclass {
String codeX= someedittext.toString();
String codeY= coderetrievedfromfirebase.toString();
if (codex=codey){do something};
}

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码使用 FirebaseAuth 对电子邮件和密码进行身份验证:-

private FirebaseAuth mAuth = FirebaseAuth.getInstance();

 mAuth.signInWithEmailAndPassword(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, "signInWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithEmail:failure", task.getException());
                        Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
                    }

                    // ...
                }
            });

updateUI:

private void updateUI(FirebaseUser user){
if (user != null) {
    // Name, email address, and profile photo Url
    String name = user.getDisplayName();
    String email = user.getEmail();
    Uri photoUrl = user.getPhotoUrl();

    // Check if user's email is verified
    boolean emailVerified = user.isEmailVerified();

    // The user's ID, unique to the Firebase project. Do NOT use this value to
    // authenticate with your backend server, if you have one. Use
    // FirebaseUser.getIdToken() instead.
    String uid = user.getUid();
}
}

有关更多信息,您可以通过Google Firebase Authentication