firebase访问令牌在4到5小时后到期

时间:2017-08-16 11:15:03

标签: android firebase firebase-authentication quickblox quickblox-android

我正在制作聊天应用并使用firebase手机授权和quickblox chat 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();
        }
}

但问题是我的令牌在4到5个小时后过期。当我清除缓存或重新安装应用程序时,它再次工作5个小时。

2 个答案:

答案 0 :(得分:3)

您可以使用QBSessionListener监听QBSession状态。因此,在回调onProviderSessionExpired(字符串提供程序)中,您可以获取当前的firebase令牌并续订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();
                        }
                    }
                });
}

答案 1 :(得分:1)

它可以过期

根据我的理解, onVerificationCompleted 回调将在两种情况下触发:

  1. 首次登录(通过短信确认)
  2. 令牌续订
  3. https://firebase.google.com/docs/auth/android/phone-auth#verification-callbacks

    所以你必须一直听它并且如果它正在更新 - 那么再次调用QuickBlox登录