英文:
How to interact with Firebase user after API call?
问题
使用Retrofit2进行API调用,通过电子邮件和密码使用Firebase身份验证登录用户,然后需要以某种方式与用户对象进行交互,例如刷新某些数据或在下次应用程序启动时检查用户是否已登录(我收到了正确的Json对象)。
但是,在API调用之后检查FirebaseAuth
对象的mAuth.getCurrentUser()
返回null
,并且不知道如何在我的应用程序内部保存登录状态。
NetworkService.getInstance().getJsonApi()
.loginWithEmail(email, password, true)
.enqueue(new Callback<Transaction.Result>() {
@Override
public void onResponse(Call<Transaction.Result> call, Response<Transaction.Result> response) {
if (response.code() == 200) {
// 登录成功,使用已登录用户的信息更新UI
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
activity.updateUI(user);
activity.startService();
} else {
Log.d(TAG, "signInWithEmail:failure");
activity.showToast("认证失败。");
activity.updateUI(null);
}
activity.hideProgressBar();
}
@Override
public void onFailure(Call<Transaction.Result> call, Throwable t) {
t.printStackTrace();
}
});
如何正确检索FirebaseUser
?
谢谢。
英文:
I perform API call with Retrofit2 to login user via Firebase Authentication with email and password and after that need to interact somehow with user object, i.e. refresh some data or check if user logged in on next time application starts (I'm getting a correct Json object).
But after API call checking FirebaseAuth
object mAuth.getCurrentUser()
gives null
and have no idea how to save login state inside my app.
NetworkService.getInstance().getJsonApi()
.loginWithEmail(email, password, true)
.enqueue(new Callback<Transaction.Result>() {
@Override
public void onResponse(Call<Transaction.Result> call, Response<Transaction.Result> response) {
if (response.code() == 200) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
activity.updateUI(user);
activity.startService();
} else {
Log.d(TAG, "signInWithEmail:failure");
activity.showToast("Authentication failed.");
activity.updateUI(null);
}
activity.hideProgressBar();
}
@Override
public void onFailure(Call<Transaction.Result> call, Throwable t) {
t.printStackTrace();
}
});
How to retrieve FirebaseUser
correctly?
Thanks.
答案1
得分: 1
如果您正在使用Firebase Auth REST API来登录用户,您不能同时使用Firebase Auth SDK来管理该登录。您将需要坚持使用REST API来进行所有管理。这意味着您将不得不跟踪从API获取的ID令牌,在其到期之前每小时刷新令牌,并保持自己对于用户“已登录”含义的理解。
我强烈建议您只是使用提供的SDK,因为它会自动为您处理所有这些事情。
英文:
If you're using the Firebase Auth REST API to sign in a user, you can't also use the Firebase Auth SDK to manage that sign-in. You will have to stick to using the REST API for all management. This means that you'll have to track the ID token that you get from the API, refresh the token every hour before it expires, and maintain your own sense of what it means for the user to be "signed in".
I highly suggest just using the provided SDK, as it will do all of this for you automatically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论