From 4ae21a18f027bc7cb6dac19738b01a138df747d5 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 21 Jan 2021 14:37:23 +0800 Subject: Handle no new refresh token. --- .../infinityforreddit/AccessTokenAuthenticator.java | 10 +++++++--- .../infinityforreddit/PullNotificationWorker.java | 8 ++++++-- .../docilealligator/infinityforreddit/account/AccountDao.java | 3 +++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 3c912221..529f5ad2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -10,8 +10,8 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import okhttp3.Authenticator; import okhttp3.Headers; @@ -71,8 +71,12 @@ class AccessTokenAuthenticator implements Authenticator { if (response.isSuccessful() && response.body() != null) { JSONObject jsonObject = new JSONObject(response.body()); String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); - String newRefreshToken = jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY); - mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken); + String newRefreshToken = jsonObject.has(APIUtils.REFRESH_TOKEN_KEY) ? jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY) : null; + if (newRefreshToken == null) { + mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getUsername(), newAccessToken); + } else { + mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken); + } return newAccessToken; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 561f393b..773c96c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -249,8 +249,12 @@ public class PullNotificationWorker extends Worker { if (response.isSuccessful() && response.body() != null) { JSONObject jsonObject = new JSONObject(response.body()); String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); - String newRefreshToken = jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY); - mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken); + String newRefreshToken = jsonObject.has(APIUtils.REFRESH_TOKEN_KEY) ? jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY) : null; + if (newRefreshToken == null) { + mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getUsername(), newAccessToken); + } else { + mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken); + } return newAccessToken; } return ""; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java index 0f214f85..0bcb8d4e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -52,4 +52,7 @@ public interface AccountDao { @Query("UPDATE accounts SET access_token = :accessToken, refresh_token = :refreshToken WHERE username = :username") void updateAccessTokenAndRefreshToken(String username, String accessToken, String refreshToken); + + @Query("UPDATE accounts SET access_token = :accessToken WHERE username = :username") + void updateAccessToken(String username, String accessToken); } -- cgit v1.2.3