API 调用后如何与 Firebase 用户交互?
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) {
// 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();
}
});
如何正确检索FirebaseUser
?
谢谢。
如果您使用 Firebase Auth REST API 登录用户,则不能同时使用 Firebase Auth SDK 来管理该 sign-in。您将必须坚持使用 REST API 进行所有管理。这意味着您必须跟踪从 API 获得的 ID 令牌,在令牌过期前每小时刷新一次令牌,并保持您自己对用户“登录”意味着什么的感觉.
我强烈建议只使用提供的 SDK,因为它会自动为您完成所有这些工作。
我使用 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) {
// 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();
}
});
如何正确检索FirebaseUser
?
谢谢。
如果您使用 Firebase Auth REST API 登录用户,则不能同时使用 Firebase Auth SDK 来管理该 sign-in。您将必须坚持使用 REST API 进行所有管理。这意味着您必须跟踪从 API 获得的 ID 令牌,在令牌过期前每小时刷新一次令牌,并保持您自己对用户“登录”意味着什么的感觉.
我强烈建议只使用提供的 SDK,因为它会自动为您完成所有这些工作。