From e04af237667d212eedda882bc5b4a1f7fa630078 Mon Sep 17 00:00:00 2001 From: "hermes@bounty.hunter" Date: Fri, 15 May 2026 00:20:24 +0700 Subject: [PATCH] fix: resolve bounty issue #5 - handle FirebaseAuthUserCollisionException properly When email linking fails with FirebaseAuthUserCollisionException, the user was left with a phone-only account that cannot login with email/password. Fix: sign out the orphaned phone-only account and redirect to login screen with the email pre-filled, so the user can login with the existing account. --- .../example/updateapp/views/activites/LoginActivity.java | 7 +++++++ .../example/updateapp/views/activites/OTPActivity.java | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/example/updateapp/views/activites/LoginActivity.java b/app/src/main/java/com/example/updateapp/views/activites/LoginActivity.java index 8700ad9..f872e4a 100644 --- a/app/src/main/java/com/example/updateapp/views/activites/LoginActivity.java +++ b/app/src/main/java/com/example/updateapp/views/activites/LoginActivity.java @@ -55,6 +55,13 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(binding.getRoot()); auth = FirebaseAuth.getInstance(); + + // Handle prefilled email from OTPActivity redirect + String prefilledEmail = getIntent().getStringExtra("prefilled_email"); + if (prefilledEmail != null && !prefilledEmail.isEmpty()) { + binding.edtEmail.setText(prefilledEmail); + } + googleBtn = findViewById(R.id.google_btn); emailBtn = findViewById(R.id.email_btn); diff --git a/app/src/main/java/com/example/updateapp/views/activites/OTPActivity.java b/app/src/main/java/com/example/updateapp/views/activites/OTPActivity.java index 918ed05..982c157 100644 --- a/app/src/main/java/com/example/updateapp/views/activites/OTPActivity.java +++ b/app/src/main/java/com/example/updateapp/views/activites/OTPActivity.java @@ -186,9 +186,16 @@ private void verifyCredential(PhoneAuthCredential credential) { String msg = e != null ? e.getMessage() : "Unknown linking error"; if (e instanceof FirebaseAuthUserCollisionException) { + // Email already exists with different account. + // Sign out this phone-only account and redirect to login with existing email. + auth.signOut(); Toast.makeText(OTPActivity.this, - "This email is already registered with a different account. Please login with that email or use another email.", + "An account with this email already exists. Please login with your email and password.", Toast.LENGTH_LONG).show(); + Intent intent = new Intent(OTPActivity.this, LoginActivity.class); + intent.putExtra("prefilled_email", email); + startActivity(intent); + finish(); } else { Toast.makeText(OTPActivity.this, "Failed to link email: " + msg,