我正在制作一个应用程序,它使用firebase注册用户并使用电子邮件和密码登录。我已经成功地实现了注册部分,但我有一个轻微的打嗝实现登录。我想找出用户何时输入了错误的密码或无效的电子邮件,并分别处理每个异常。我知道当我使用电子邮件和密码登录时有一个getException()方法,但我不知道如何使用它。如果您能帮助我优雅地处理这些异常,我将不胜感激。
5us2dqdw1#
对于注册用户,请参阅以下其他部分:
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(); } } });
有关登录部分,请参阅以下内容:
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(); }
1条答案
按热度按时间5us2dqdw1#
对于注册用户,请参阅以下其他部分:
有关登录部分,请参阅以下内容: