Sar*_*rni 3 android firebase firebase-authentication firebase-storage
我正在开发Android应用程序,在其中使用Firebase身份验证通过提供电子邮件和密码来进行用户登录。我使用了Firebase提供的基本教程,但是在输入用户ID和密码后,我发现登录或创建帐户按钮均无效。
当我调试它时,我发现控件不会执行以下操作:
public void onComplete(@NonNull Task<AuthResult> task)
Run Code Online (Sandbox Code Playgroud)
在:
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
Run Code Online (Sandbox Code Playgroud)
要么:
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
Run Code Online (Sandbox Code Playgroud)
登录和创建帐户的代码为:
private void createAccount(String email, String password)
{
Log.d(TAG, "createAccount:" + email);
if (!validateForm())
{
return;
}
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (task.isSuccessful())
{
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = fAuth.getCurrentUser();
updateUI(user);
}
else
{
Log.w(TAG,"createUserWithEmail:failure",task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
private void signIn(String email, String password)
{
Log.d(TAG, "signIn:" + email);
if (!validateForm())
{
return;
}
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (task.isSuccessful())
{
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = fAuth.getCurrentUser();
updateUI(user);
}
else
{
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
updateUI(null);
}
if (!task.isSuccessful())
{
mStatusTextView.setText(R.string.auth_failed);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
它只是跳过了这两种方法的内部,什么也没有返回。我按照与教程中完全相同的方式进行操作,但是我不了解确切的问题。有人可以帮我解决我在哪里出问题。
在代码中进行以下更改
声明AuthListener:
// [START declare_auth_listener]
private FirebaseAuth.AuthStateListener mAuthListener;
// [END declare_auth_listener]
// [START declare_auth]
private FirebaseAuth mAuth;
// [END declare_auth]
Run Code Online (Sandbox Code Playgroud)
在onStart()和onStop()中添加列表器:
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Activity的onCreate()中添加AuthStateListner方法,它将在每次创建Activity时检查Auth状态:
// [START auth_state_listener] ,this method execute as soon as there is a change in Auth status , such as user sign in or sign out.
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
//redirect
updateUI(user);
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
updateUI(null);
}
}
};
// [END auth_state_listener]
Run Code Online (Sandbox Code Playgroud)
对于使用电子邮件密码登录,请执行此操作。
private void signIn(String email, String password)
{
Log.d(TAG, "signIn:" + email);
if (!validateForm())
{
return;
}
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d(LOG_TAG, " Verification : signIn With Email:onComplete:" + task.isSuccessful());
// If sign in succeeds i.e if task.isSuccessful(); returns true then the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
// If sign in fails, display a message to the user.
if (!task.isSuccessful()) {
try {
throw task.getException();
} catch (FirebaseAuthInvalidUserException e) {
mStatusTextView.setError("Invalid Emaild Id");
mStatusTextView.requestFocus();
} catch (FirebaseAuthInvalidCredentialsException e) {
Log.d(LOG_TAG , "email :" + email);
mStatusTextView.setError("Invalid Password");
mStatusTextView.requestFocus();
} catch (FirebaseNetworkException e) {
showErrorToast("error_message_failed_sign_in_no_network");
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
Log.w(LOG_TAG, "signInWithEmail:failed", task.getException());
Toast.makeText(LoginActivity.this, R.string.login_error,
Toast.LENGTH_SHORT).show();
updateUI(null);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8356 次 |
| 最近记录: |