Handle no new refresh token.

This commit is contained in:
Alex Ning 2021-01-21 14:37:23 +08:00
parent e19dd8c31f
commit 4ae21a18f0
3 changed files with 16 additions and 5 deletions

View File

@ -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;
}

View File

@ -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 "";

View File

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