Firebase Signinwithemailandpassword And Createuserwithemailandpassword Not Working In Android
I'm developing Android application where I'm using Firebase Authentication for user login by giving email and password. I have used the basic tutorial provided by Firebase for it b
Solution 1:
do following changes in your code
declare AuthListener :
// [START declare_auth_listener]private FirebaseAuth.AuthStateListener mAuthListener;
// [END declare_auth_listener]// [START declare_auth]private FirebaseAuth mAuth;
// [END declare_auth]
add listner in onStart() and onStop():
@OverridepublicvoidonStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@OverridepublicvoidonStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
then add AuthStateListner method in onCreate() of your Activity,It will check for Auth status every time your activity is created:
// [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 = newFirebaseAuth.AuthStateListener() {
@OverridepublicvoidonAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUseruser= 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]
for Sign in with email password do this..
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);
}
});
}
Post a Comment for "Firebase Signinwithemailandpassword And Createuserwithemailandpassword Not Working In Android"