You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Validate user input (email/password)
  2. Verify if the user is registered by attempting a login
  3. On successful login, check if the user's email matches the admin address (admin@admin.com)
  4. Redirect to AdminActivity if it's the admin email, otherwise redirect to ProfileActivity

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 AdminActivity and ProfileActivity are added to your AndroidManifest.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:
    1. Call your login API with email/password
    2. On success, retrieve the user's email from the API response
    3. 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.cm instead 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 06:17:40