简体   繁体   English

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

[英]How to interact with Firebase user after API call?

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, ie refresh some data or check if user logged in on next time application starts (I'm getting a correct Json object).我使用 Retrofit2 执行 API 调用以使用电子邮件和密码通过 Firebase 身份验证登录用户,然后需要以某种方式与用户对象进行交互,即刷新一些数据或检查用户是否在下次应用程序启动时登录(我得到了正确的 Json目的)。

But after API call checking FirebaseAuth object mAuth.getCurrentUser() gives null and have no idea how to save login state inside my app.但是在 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();
        }
    });

How to retrieve FirebaseUser correctly?如何正确检索FirebaseUser

Thanks.谢谢。

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.如果您使用 Firebase Auth REST API 登录用户,则不能同时使用 Firebase Auth SDK 管理该登录。 You will have to stick to using the REST API for all management.您必须坚持使用 REST API 进行所有管理。 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".这意味着您必须跟踪从 API 获得的 ID 令牌,在令牌过期前每小时刷新一次,并保持自己对用户“登录”意味着什么的认识。

I highly suggest just using the provided SDK, as it will do all of this for you automatically.我强烈建议只使用提供的 SDK,因为它会自动为您完成所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从用户 0 作为用户 -1 未经许可调用 INTERACT_ACROSS_USERS 或 INTERACT_ACROSS_USERS_FULL 不允许 - Call from user 0 as user -1 without permission INTERACT_ACROSS_USERS or INTERACT_ACROSS_USERS_FULL not allowed 如何在 api 调用中使用令牌对用户进行身份验证? - How to authenticate an user with token in an api call? Java API与JVM到底如何交互? - How exactly does Java API interact with JVM? 如何使用Selenide API聚焦弹出窗口并与之交互 - How to focus and interact with popup using selenide api 如何使用用户输入更改API调用 - How alter an API call with user input 如何使API与我的应用程序交互 - How to make an API to interact with my app 如何通过 Id 获取用户并从 firebase auth 数据库中删除它 - How to get a user by Id and delete it after from firebase auth database 如何使应用程序与Asterisk服务器交互以进行呼叫? - How to make an application interact with Asterisk server to make a call? 如何使Servlet与网站的API交互并存储XML响应? - How to make a servlet interact with the API of a website and also store the XML response? Java 标准 API 中的类如何与操作系统交互? - How do classes in the Java standard API interact with the OS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM