Android中Firebase Auth非角色认证下按邮箱跳转页面问题求助
Solution: Route to Admin/Profile Activity Based on Email Without Role-Based Auth
Got it, let's fix this issue step by step. The root problem here is that your current login flow isn't properly checking the logged-in user's email to redirect them to the correct activity. Below is a complete, implementation-ready solution that meets your requirements:
Core Logic Overview
- Validate user input (email/password)
- Verify if the user is registered by attempting a login
- On successful login, check if the user's email matches the admin address (
admin@admin.com) - Redirect to
AdminActivityif it's the admin email, otherwise redirect toProfileActivity
Complete Code Example (Android with Firebase Auth)
Assuming you're using Firebase Authentication (adjust logic for your backend if needed), here's how to implement this in your LoginActivity:
import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; public class LoginActivity extends AppCompatActivity { private FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mAuth = FirebaseAuth.getInstance(); EditText emailInput = findViewById(R.id.et_email); EditText passwordInput = findViewById(R.id.et_password); Button loginButton = findViewById(R.id.btn_login); loginButton.setOnClickListener(v -> { String email = emailInput.getText().toString().trim(); String password = passwordInput.getText().toString().trim(); // Basic input validation if (TextUtils.isEmpty(email)) { emailInput.setError("Email is required"); return; } if (TextUtils.isEmpty(password)) { passwordInput.setError("Password is required"); return; } // Attempt login to verify if user is registered mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, task -> { if (task.isSuccessful()) { // Login success: Fetch current user's email FirebaseUser user = mAuth.getCurrentUser(); if (user != null && user.getEmail() != null) { String userEmail = user.getEmail(); // Check if it's the admin email (case-insensitive match) if ("admin@admin.com".equalsIgnoreCase(userEmail)) { // Redirect to Admin Activity startActivity(new Intent(LoginActivity.this, AdminActivity.class)); } else { // Redirect to Profile Activity startActivity(new Intent(LoginActivity.this, ProfileActivity.class)); } // Close login activity to prevent going back finish(); } } else { // Login failed: User is not registered or password is wrong Toast.makeText(LoginActivity.this, "Invalid credentials or user not registered", Toast.LENGTH_SHORT).show(); } }); }); } }
Key Notes to Avoid Issues
- Activity Registration: Ensure both
AdminActivityandProfileActivityare added to yourAndroidManifest.xml(otherwise the app will crash on redirect). - Case Insensitivity: Using
equalsIgnoreCase()allows case variations (e.g.,Admin@Admin.com) to still redirect to the admin page—remove this if you need an exact case match. - Finish Login Activity: Calling
finish()after redirecting ensures users can't press the back button to return to the login screen after successful authentication. - Non-Firebase Backends: If you're using a custom backend, the logic stays the same:
- Call your login API with email/password
- On success, retrieve the user's email from the API response
- Compare the email to the admin address and redirect accordingly
Troubleshooting Your Original Issue
If you were previously being redirected to ProfileActivity even with the admin email, check these common mistakes:
- You had the redirect logic reversed (e.g., checking for non-admin first)
- Typo in the admin email string (e.g.,
admin@admin.cminstead of.com) - You didn't properly fetch the logged-in user's email (e.g., using a hardcoded value instead of
user.getEmail())
内容的提问来源于stack exchange,提问作者JimC
相关产品推荐
相关产品推荐

