Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: session queries #245

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ public static void createNewSession(Start start, TenantIdentifier tenantIdentifi
public static SessionInfo getSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
String sessionHandle)
throws SQLException, StorageQueryException {
// we do this as two separate queries and not one query with left join cause psql does not
// support left join with for update if the right table returns null.

String QUERY =
"SELECT session_handle, user_id, refresh_token_hash_2, session_data, " +
"expires_at, created_at_time, jwt_user_payload, use_static_key FROM " +
Expand All @@ -149,21 +146,55 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
return null;
}

QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable()
+ " WHERE app_id = ? AND user_id = ?";

return execute(con, QUERY, pst -> {
QUERY = "SELECT external_user_id " +
"FROM " + getConfig(start).getUserIdMappingTable() + " um2 " +
"WHERE um2.app_id = ? AND um2.supertokens_user_id IN (" +
"SELECT primary_or_recipe_user_id " +
"FROM " + getConfig(start).getUsersTable() + " " +
"WHERE app_id = ? AND user_id IN (" +
"SELECT um1.supertokens_user_id as user_id " +
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
"UNION ALL " +
"SELECT ? " +
"LIMIT 1" +
")" +
") " +
"UNION ALL " +
"SELECT primary_or_recipe_user_id " +
"FROM " + getConfig(start).getUsersTable() + " " +
"WHERE app_id = ? AND user_id IN (" +
"SELECT um1.supertokens_user_id as user_id " +
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
"UNION ALL " +
"SELECT ? " +
"LIMIT 1" +
") " +
"LIMIT 1";

String finalUserId = execute(con, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, sessionInfo.recipeUserId);
pst.setString(2, tenantIdentifier.getAppId());
pst.setString(3, tenantIdentifier.getAppId());
pst.setString(4, sessionInfo.recipeUserId);
pst.setString(5, sessionInfo.recipeUserId);
pst.setString(6, tenantIdentifier.getAppId());
pst.setString(7, tenantIdentifier.getAppId());
pst.setString(8, sessionInfo.recipeUserId);
pst.setString(9, sessionInfo.recipeUserId);
}, result -> {
if (result.next()) {
String primaryUserId = result.getString("primary_or_recipe_user_id");
if (primaryUserId != null) {
sessionInfo.userId = primaryUserId;
}
return result.getString(1);
}
return sessionInfo;
return sessionInfo.recipeUserId;
});

if (finalUserId != null) {
sessionInfo.userId = finalUserId;
}

return sessionInfo;
}

public static void updateSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
Expand Down
Loading