如何在 API 调用后与 Firebase 用户进行交互?

huangapple go评论75阅读模式
英文:

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&lt;Transaction.Result&gt;() {
        @Override
        public void onResponse(Call&lt;Transaction.Result&gt; call, Response&lt;Transaction.Result&gt; response) {

            if (response.code() == 200) {
                // Sign in success, update UI with the signed-in user&#39;s information
                Log.d(TAG, &quot;signInWithEmail:success&quot;);

                FirebaseUser user = mAuth.getCurrentUser();
                activity.updateUI(user);

                activity.startService();

            } else {
                Log.d(TAG, &quot;signInWithEmail:failure&quot;);
                activity.showToast(&quot;Authentication failed.&quot;);
                activity.updateUI(null);
            }

            activity.hideProgressBar();
        }

        @Override
        public void onFailure(Call&lt;Transaction.Result&gt; 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.

huangapple
  • 本文由 发表于 2020年8月27日 03:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63604841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定