4

I am making an app which uses firebase to register user and sign in using email and password. I have successfully implemented the registration part but I have a slight hiccup implementing the sign in.

I want to find when and if the user enters wrong password or invalid email and handle each exception separately. I know there is a getException() method when I login using email and password but I cant exactly find how to use it.

I would appreciate it if you could help me handle these exceptions gracefully..

Sriram R
  • 2,109
  • 3
  • 23
  • 40

1 Answers1

0

For Register user refer below else part:

                mAuth.createUserWithEmailAndPassword(email, password)
                        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                progressBar.setVisibility(View.GONE);
                                if (task.isSuccessful()) {
                                    Toast.makeText(Register.this, "Account successfully created.",
                                            Toast.LENGTH_SHORT).show();
                                    UserDetails userDetails= new UserDetails(dob,medical_conditon,emergency_contact,emergency_contact_name);
                                    String uId=task.getResult().getUser().getUid();
                                    firebaseDatabase.getReference(uId).setValue(userDetails)
                                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                @Override
                                                public void onSuccess(Void unused) {
                                                    Intent intent=new Intent(getApplicationContext(), Login.class);
                                                    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);//finish current activity ie register,
                                                    //intent.putExtra("email",email);
                                                    startActivity(intent);
                                                    finish();
                                                }
                                            });
                                    // Sign in success, update UI with the signed-in user's information
                                    //FirebaseUser user = mAuth.getCurrentUser();

                                } else {
                                    // If sign in fails, display a message to the user.
                                    try{
                                        throw task.getException();
                                    }catch (FirebaseAuthUserCollisionException e){
                                        editTextEmail.setError("Email already registered");
                                        editTextEmail.requestFocus();
                                    }
                                    catch (FirebaseAuthInvalidCredentialsException e){
                                        editTextEmail.setError("Email is invalid or already in use");
                                        editTextEmail.requestFocus();
                                    }
                                    catch (Exception e)
                                    {
                                        Log.e(TAG, e.getMessage());
                                        Toast.makeText(Register.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                    }
                                    Toast.makeText(getApplicationContext(), "Authentication failed.",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

For Login part refer below:

else {
                                        // If sign in fails, display a message to the user.
                                        try{
                                            throw task.getException();
                                        }catch (FirebaseAuthInvalidCredentialsException e){
                                            editTextPassword.setError("Invalid Password");
                                            editTextPassword.requestFocus();
                                        }
                                        catch (FirebaseAuthInvalidUserException e) {
                                            editTextEmail.setError("Email does not exist!");
                                            editTextEmail.requestFocus();

                                        }
                                        catch (Exception e)
                                        {
                                            Log.e(TAG, e.getMessage());
                                            Toast.makeText(Login.this, e.getMessage(), Toast.LENGTH_LONG).show();
                                        }
                                        Toast.makeText(Login.this, "Authentication failed.",
                                                Toast.LENGTH_SHORT).show();

                                    }