Skip to content

Commit 6218752

Browse files
committed
Fix refresh behavior in doesSessionExist
1 parent 3c2ced0 commit 6218752

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

app/src/main/java/com/supertokens/session/SuperTokens.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ public static boolean doesSessionExist(Context context) {
123123
long accessTokenExpiry = frontToken.getLong("ate");
124124

125125
if (accessTokenExpiry < System.currentTimeMillis()) {
126-
Utils.LocalSessionState localSessionState = Utils.getLocalSessionState(context);
127-
Utils.Unauthorised response = SuperTokensHttpURLConnection.onUnauthorisedResponse(localSessionState, context);
126+
Utils.LocalSessionState preRequestLocalSessionState = Utils.getLocalSessionState(context);
127+
Utils.Unauthorised response = SuperTokensHttpURLConnection.onUnauthorisedResponse(preRequestLocalSessionState, context);
128+
129+
// Here we dont throw the error and instead return false, because
130+
// otherwise users would have to use a try catch just to call doesSessionExist
131+
if (response.status == Utils.Unauthorised.UnauthorisedStatus.API_ERROR) {
132+
return false;
133+
}
134+
128135
return response.status == Utils.Unauthorised.UnauthorisedStatus.RETRY;
129136
}
130137
} catch (JSONException e) {

testHelpers/testapp/app/src/test/java/com/example/example/SuperTokensHttpURLConnectionTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,4 +1219,56 @@ public void doAction(HttpURLConnection con) throws IOException {
12191219

12201220
userInfoRequestConnection.disconnect();
12211221
}
1222+
1223+
// Test that doesSessionExist returns true after access token expiry (with refresh)
1224+
@Test
1225+
public void httpUrlConnection_testThatDoesSessionExistReturnsTrueAfterAccessTokenExpiry() throws Exception {
1226+
com.example.TestUtils.startST(2);
1227+
new SuperTokens.Builder(context, Constants.apiDomain).build();
1228+
1229+
HttpURLConnection loginRequestConnection = SuperTokensHttpURLConnection.newRequest(new URL(loginAPIURL), new SuperTokensHttpURLConnection.PreConnectCallback() {
1230+
@Override
1231+
public void doAction(HttpURLConnection con) throws IOException {
1232+
con.setDoOutput(true);
1233+
con.setRequestMethod("POST");
1234+
con.setRequestProperty("Accept", "application/json");
1235+
con.setRequestProperty("Content-Type", "application/json");
1236+
1237+
JsonObject bodyJson = new JsonObject();
1238+
bodyJson.addProperty("userId", Constants.userId);
1239+
1240+
OutputStream outputStream = con.getOutputStream();
1241+
outputStream.write(bodyJson.toString().getBytes(StandardCharsets.UTF_8));
1242+
outputStream.close();
1243+
}
1244+
});
1245+
1246+
if (loginRequestConnection.getResponseCode() != 200) {
1247+
throw new Exception("Login request failed");
1248+
}
1249+
loginRequestConnection.disconnect();
1250+
1251+
if (!SuperTokens.doesSessionExist(context)) {
1252+
throw new Exception("Session should exist immediately after login");
1253+
}
1254+
1255+
Thread.sleep(3000);
1256+
1257+
// Call doesSessionExist after access token expiry
1258+
// This should trigger a refresh and still return true
1259+
boolean sessionExists = SuperTokens.doesSessionExist(context);
1260+
1261+
if (!sessionExists) {
1262+
throw new Exception("doesSessionExist should return true after access token expiry with successful refresh");
1263+
}
1264+
1265+
int refreshCount = com.example.TestUtils.getRefreshTokenCounter();
1266+
if (refreshCount != 1) {
1267+
throw new Exception("Expected refresh to be called 1 time but it was called " + refreshCount + " times");
1268+
}
1269+
1270+
if (!SuperTokens.doesSessionExist(context)) {
1271+
throw new Exception("Session should still exist after refresh");
1272+
}
1273+
}
12221274
}

0 commit comments

Comments
 (0)