Skip to content

Commit

Permalink
fix: active users with account linking
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Mar 21, 2024
1 parent 8dfc12f commit 0b10553
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Fixes userIdMapping queries
- Fixes issue with session creation for users with userIdMapping and accounts linked
- Fixes active users tracking while linking accounts

## [8.0.1] - 2024-03-11

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/supertokens/ActiveUsers.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.supertokens;

import io.supertokens.pluginInterface.ActiveUsersStorage;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.StorageUtils;
import io.supertokens.pluginInterface.authRecipe.sqlStorage.AuthRecipeSQLStorage;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.exceptions.StorageTransactionLogicException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.sqlStorage.SQLStorage;
import io.supertokens.storageLayer.StorageLayer;
import org.jetbrains.annotations.TestOnly;

Expand Down Expand Up @@ -37,6 +39,20 @@ public static int countUsersActiveSince(Main main, AppIdentifier appIdentifier,
return StorageUtils.getActiveUsersStorage(storage).countUsersActiveSince(appIdentifier, time);
}

public static void updateLastActiveAfterLinking(Main main, AppIdentifier appIdentifier, String primaryUserId,
String recipeUserId)
throws StorageQueryException, TenantOrAppNotFoundException, StorageTransactionLogicException {
ActiveUsersStorage activeUsersStorage =
StorageUtils.getActiveUsersStorage(StorageLayer.getStorage(appIdentifier.getAsPublicTenantIdentifier(), main));

((SQLStorage) activeUsersStorage).startTransaction(con -> {
activeUsersStorage.deleteUserActive_Transaction(con, appIdentifier, recipeUserId);
return null;
});

updateLastActive(appIdentifier, main, primaryUserId);
}

@TestOnly
public static int countUsersActiveSince(Main main, long time)
throws StorageQueryException, TenantOrAppNotFoundException {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/supertokens/authRecipe/AuthRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ private static void deleteNonAuthRecipeUser(TransactionConnection con, AppIdenti
.deleteUserActive_Transaction(con, appIdentifier, userId);
StorageUtils.getTOTPStorage(storage)
.removeUser_Transaction(con, appIdentifier, userId);
StorageUtils.getActiveUsersStorage(storage)
.deleteUserActive_Transaction(con, appIdentifier, userId);
}

private static void deleteAuthRecipeUser(TransactionConnection con,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.supertokens.webserver.api.accountlinking;

import com.google.gson.JsonObject;
import io.supertokens.ActiveUsers;
import io.supertokens.Main;
import io.supertokens.StorageAndUserIdMapping;
import io.supertokens.authRecipe.AuthRecipe;
Expand Down Expand Up @@ -111,6 +112,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
response.addProperty("status", "OK");
response.addProperty("accountsAlreadyLinked", linkAccountsResult.wasAlreadyLinked);
response.add("user", linkAccountsResult.user.toJson());

if (!linkAccountsResult.wasAlreadyLinked) {
try {
ActiveUsers.updateLastActiveAfterLinking(
main, appIdentifier, primaryUserId, recipeUserId);
} catch (Exception e) {
// ignore
}
}

super.sendJsonResponse(200, response, resp);
} catch (StorageQueryException | TenantOrAppNotFoundException | FeatureNotEnabledException |
BadPermissionException e) {
Expand Down

0 comments on commit 0b10553

Please sign in to comment.