android - firebase 访问 token 将在 4 到 5 小时后过期

标签 android firebase firebase-authentication quickblox quickblox-android

我正在制作一个聊天应用程序并使用 firebase 电话授权和 quickblox 聊天 api。

我的代码是:

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d("test", "signInWithCredential:success");
                        FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();

                        mUser.getToken(false).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                            @Override
                            public void onComplete(@NonNull Task<GetTokenResult> task) {
                                if(task.isSuccessful()){
                                    String m=task.getResult().getToken();
                                    signIn(m);
                                }
                            }
                        });


 public void signIn(String token){
       QBUsers.signInUsingFirebase(projectId, token).performAsync( new QBEntityCallback<QBUser>() {
        @Override
        public void onSuccess(QBUser user, Bundle args) {

            messenger.com.nowchat.helper.DataHolder.getInstance().setSignInQbUser(user);
            Intent intent = new Intent(Registration.this, WelcomeProfile.class);
            startActivity(intent);
             finish();
        }
}

但问题是我的 token 会在 4 到 5 小时后过期。当我清除缓存或重新安装应用程序时,它会再次运行 5 小时。

最佳答案

您可以使用 QBSessionListener 来监听 QBSession 状态。因此,在回调 onProviderSessionExpired(String provider) 中,您可以获得当前的 firebase token 并更新 QBSession。在代码中它看起来像这样:

@Override
public void onProviderSessionExpired(String provider) {
    if (!QBProvider.FIREBASE_PHONE.equals(provider)){
        return;
    }

    final String projectId = "your.project.id";

    FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
        mUser.getToken(true)
                .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                    public void onComplete(@NonNull Task<GetTokenResult> task) {
                        if (task.isSuccessful()) {
                            String authToken = task.getResult().getToken();
                            QBUsers.signInUsingFirebase(projectId, authToken).performAsync(new QBEntityCallback<QBUser>() {
                                @Override
                                public void onSuccess(QBUser qbUser, Bundle bundle) {
                                    qbUser.setPassword(QBSessionManager.getInstance().getToken());
                                    QBChatService.getInstance().login(qbUser, new QBEntityCallback() {
                                        @Override
                                        public void onSuccess(Object o, Bundle bundle) {
                                            //your actions after success login to the chat
                                        }

                                        @Override
                                        public void onError(QBResponseException e) {

                                        }
                                    });
                                }

                                @Override
                                public void onError(QBResponseException e) {

                                }
                            });
                        } else {
                            // Handle error -> task.getException();
                        }
                    }
                });
}

关于android - firebase 访问 token 将在 4 到 5 小时后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45712054/

相关文章:

android - 我想为商店中的 android 和 ios 应用程序生成相同的 URL

android - 更新到 Android studio 3.5 后出现以下错误 - FAILURE : Build failed with an exception

javascript - Firebase电子邮件/密码身份验证未注册用户

javascript - Firebase 登录并使用 google 弹出窗口加载时间过长

javascript - 如何修复 firebase.auth().signInWithEmailAndPassword() 方法中的错误 'Undefined and error message: this.f is not a constructor'?

android - RecyclerView getMenuInfo 始终为空

java - android.view.InflateException : Binary XML file line #29: Error inflating class android. widget.Button

android - 在自定义警报对话框中获取单选按钮的值

javascript - 如何将firebase返回的数据保存到变量

ios - 使用 Firebase 计算相同的操作 (swift)