4

I am making an chat app and use firebase phone authorization and quickblox chat api.

My code is:

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();
        }
}

But problem is that my token expires after 4 to 5 hours. When I clear the cache or reinstall the app then it works again for 5 hours.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
prem jangir
  • 300
  • 2
  • 13
  • It's okay that it expires. What issues do you have with this? You have to renew it every start app or so – Rubycon Aug 22 '17 at 10:10

2 Answers2

3

You can use QBSessionListener for listening QBSession states. So in callback onProviderSessionExpired(String provider) you can get current firebase token and renew QBSession. In code it will be looks like this:

@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();
                        }
                    }
                });
}
  • Thanks ....one thing more when logintochat for first time it works properly but after few hours its shows the error that authentication failed user id or password is wrong ...well I am using qbsession token as password ...I know qbsession token also expire after few hours ...so my question will callback it fire on obsession token expire – prem jangir Aug 23 '17 at 06:14
  • @premjangir I updated my answer for showing how to login to the chat after renew QBsession – Valentyn Tereshchenko Aug 23 '17 at 12:02
  • I also wrote same code but after few hours the qbsession token expires ...and only login fire an error.....how to handle that – prem jangir Aug 23 '17 at 12:08
  • Did you get error even after renew session in callback onProviderSessionExpired? You can organize your code for login to the chat from two points: 1- after success login to REST, 2 - in callback onProviderSessionExpired. In both ways you can get current token as QBSessionManager.getInstance().getToken(). – Valentyn Tereshchenko Aug 23 '17 at 12:17
  • No...now onProvideSessionExpired works properly... when the user has already sign in to app and start app again after few hours then the login to chat fire an error that user id or password id is wrong... – prem jangir Aug 23 '17 at 12:53
1

It's okay that it expires

From my understanding, the onVerificationCompleted callback will fire in 2 situations:

  1. First login (via SMS confirmation)
  2. Token renew

https://firebase.google.com/docs/auth/android/phone-auth#verification-callbacks

So you have to listen for it all the time and if it's renew - then call QuickBlox login again

Rubycon
  • 18,156
  • 10
  • 49
  • 70