diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-01-21 06:37:23 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-01-21 06:37:23 +0000 |
commit | 4ae21a18f027bc7cb6dac19738b01a138df747d5 (patch) | |
tree | d70f32ef9e4911e93018f486c5a143f1e5982ce9 /app | |
parent | e19dd8c31f2128b77ee08c8475919829b89d5e4f (diff) | |
download | infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar.gz infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar.bz2 infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar.lz infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar.xz infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.tar.zst infinity-for-reddit-4ae21a18f027bc7cb6dac19738b01a138df747d5.zip |
Handle no new refresh token.
Diffstat (limited to '')
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); } |