Android-登录后如何从Firebase获取用户信息

标签 android firebase firebase-authentication

根据我当前的登录 Activity ,我可以正常登录,但是我需要在整个项目中获取 Uid。

这是我当前的登录 Activity :

   //authenticate user
        firebaseAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        progressbar.setVisibility(View.GONE);
                        if (!task.isSuccessful()) {
                            // there was an error
                            if (password.length() < 6) {
                                lPass.setError(getString(R.string.minimum_password));
                            } else {
                                Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Intent intent = new Intent(LoginActivity.this, MapsActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
    }
});

如何从登录中获取用户 Uid 并使其可供我的所有其他 Activity 访问? 谢谢

最佳答案

您可以从 FirebaseUser 对象获取用户信息。为了实现这一点,请使用以下代码:

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            String userId = firebaseUser.getUid();
            String userEmail = firebaseUser.getEmail();
        } 
    }
};

关于Android-登录后如何从Firebase获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389418/

相关文章:

Java LibGDX Texture 给出空指针异常

firebase - Flutter Phone认证报错原始例子

android - Firebase 身份验证限制

firebase - 如何在 Flutter 中初始化 firebase crashlytics?

android - 使用 Firebase 登录 Android - statusCode DEVELOPER_ERROR

android - PhoneGap 应用程序未在 Android 模拟器中运行

android - 如何在android中查找重复文件

java - react-native .58.1 无法构建 Android 版本

ios - 通过仅覆盖某些数据 Swift 3 在 Firebase 数据库中设置值

node.js - 无法从 Firebase 集合中删除文件