gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:35:32 27 4
gpt4 key购买 nike

根据我当前的登录 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com