Skip to content

Commit

Permalink
add sample flow refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
Arief Nur Putranto committed May 8, 2024
1 parent fdf6e9f commit 9b607de
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions app/src/main/java/com/qiscus/dragonfly/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

Expand All @@ -59,6 +60,7 @@ public class MainActivity extends AppCompatActivity {
private TextView mVersion;
private boolean publishCustomEvent = false;
private static final int UNAUTHORIZED = 403;
private static final int NEEDLOGIN = 401;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -210,23 +212,37 @@ public void buildRoom() {
}

public void showError(Throwable throwable) {
/* if (isTokenExpired(throwable)) {
if (isTokenExpired(throwable)) {
callRefreshToken();
return;
} */
}

if (isNeedRelogin(throwable)){
logoutUser();
mLoginButton.setText("Login");
return;
}

String errorMessage = QiscusErrorLogger.getMessage(throwable);
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}

// disable manual refresh token
/* private boolean isTokenExpired(Throwable throwable) {
private boolean isTokenExpired(Throwable throwable) {
if(throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
return httpException.code() == UNAUTHORIZED;
}
return false;
}*/
}

private boolean isNeedRelogin(Throwable throwable) {
if(throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
return httpException.code() == NEEDLOGIN;
}
return false;
}

private void callRefreshToken() {
QiscusCore.refreshToken(new QiscusCore.SetRefreshTokenListener() {
Expand All @@ -252,6 +268,32 @@ private void logoutUser() {
}
}

private void reLoginUser(){
if (QiscusCore.hasSetupUser()) {
Qiscus.clearUser();
}

showLoading();
/* Qiscus.setUser("arief92", "arief92")
.withUsername("arief92")*/
Qiscus.setUser("arief92", "arief92")
.withUsername("arief92")
.save()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(qiscusAccount -> {
Log.i("MainActivity", "Login with account: " + qiscusAccount);
mLoginButton.setText("Logout");
dismissLoading();

FirebaseUtil.sendCurrentToken();
}, throwable -> {
QiscusErrorLogger.print(throwable);
showError(throwable);
dismissLoading();
});
}

public void showLoading() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
Expand Down Expand Up @@ -308,12 +350,14 @@ public void onRoomChanged(QiscusChatRoomEvent event) {
QiscusLogger.print(event.toString());
}

//implement this to all your activity
@Subscribe
public void onRefreshToken(QiscusRefreshTokenEvent event) {
if (event.isTokenExpired()) {
callRefreshToken();
} else if (event.isUnauthorized()) {
logoutUser();
//relogin or re init sdk qiscus
reLoginUser();
} else {
// do somethings
}
Expand Down

0 comments on commit 9b607de

Please sign in to comment.