Skip to content

Commit 04a2a9f

Browse files
committed
fix: session queries
1 parent 22bd869 commit 04a2a9f

File tree

1 file changed

+44
-54
lines changed

1 file changed

+44
-54
lines changed

src/main/java/io/supertokens/inmemorydb/queries/SessionQueries.java

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -128,62 +128,52 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
128128
return null;
129129
}
130130

131-
// update userId of session info to reflect primary user id
132-
// 1. find supertokens user id for recipe user id (if using a userIdMapping)
133-
// 2. find the primary_or_recipe_user_id for the supertokens user id
134-
// 3. map primary_or_recipe_user_id to external user (if using a userIdMapping)
135-
136-
137-
{ // Step 1
138-
QUERY = "SELECT supertokens_user_id FROM " + getConfig(start).getUserIdMappingTable()
139-
+ " WHERE app_id = ? AND external_user_id = ?";
140-
141-
String stUserId = execute(con, QUERY, pst -> {
142-
pst.setString(1, tenantIdentifier.getAppId());
143-
pst.setString(2, sessionInfo.recipeUserId);
144-
}, result -> {
145-
if (result.next()) {
146-
return result.getString("supertokens_user_id");
147-
}
148-
return null;
149-
});
150-
if (stUserId != null) {
151-
sessionInfo.userId = stUserId;
152-
}
153-
}
154-
155-
{ // Step 2
156-
QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable()
157-
+ " WHERE app_id = ? AND user_id = ?";
158-
String primaryUserId = execute(con, QUERY, pst -> {
159-
pst.setString(1, tenantIdentifier.getAppId());
160-
pst.setString(2, sessionInfo.userId);
161-
}, result -> {
162-
if (result.next()) {
163-
return result.getString("primary_or_recipe_user_id");
164-
}
165-
return null;
166-
});
167-
if (primaryUserId != null) {
168-
sessionInfo.userId = primaryUserId;
131+
QUERY = "SELECT external_user_id " +
132+
"FROM " + getConfig(start).getUserIdMappingTable() + " um2 " +
133+
"WHERE um2.app_id = ? AND um2.supertokens_user_id IN (" +
134+
"SELECT primary_or_recipe_user_id " +
135+
"FROM " + getConfig(start).getUsersTable() + " " +
136+
"WHERE app_id = ? AND user_id IN (" +
137+
"SELECT um1.supertokens_user_id as user_id " +
138+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
139+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
140+
"UNION ALL " +
141+
"SELECT ? " +
142+
"LIMIT 1" +
143+
")" +
144+
") " +
145+
"UNION ALL " +
146+
"SELECT primary_or_recipe_user_id " +
147+
"FROM " + getConfig(start).getUsersTable() + " " +
148+
"WHERE app_id = ? AND user_id IN (" +
149+
"SELECT um1.supertokens_user_id as user_id " +
150+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
151+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
152+
"UNION ALL " +
153+
"SELECT ? " +
154+
"LIMIT 1" +
155+
") " +
156+
"LIMIT 1";
157+
158+
String finalUserId = execute(con, QUERY, pst -> {
159+
pst.setString(1, tenantIdentifier.getAppId());
160+
pst.setString(2, tenantIdentifier.getAppId());
161+
pst.setString(3, tenantIdentifier.getAppId());
162+
pst.setString(4, sessionInfo.recipeUserId);
163+
pst.setString(5, sessionInfo.recipeUserId);
164+
pst.setString(6, tenantIdentifier.getAppId());
165+
pst.setString(7, tenantIdentifier.getAppId());
166+
pst.setString(8, sessionInfo.recipeUserId);
167+
pst.setString(9, sessionInfo.recipeUserId);
168+
}, result -> {
169+
if (result.next()) {
170+
return result.getString(1);
169171
}
170-
}
172+
return sessionInfo.recipeUserId;
173+
});
171174

172-
{ // Step 3
173-
QUERY = "SELECT external_user_id FROM " + getConfig(start).getUserIdMappingTable()
174-
+ " WHERE app_id = ? AND supertokens_user_id = ?";
175-
String externalUserId = execute(con, QUERY, pst -> {
176-
pst.setString(1, tenantIdentifier.getAppId());
177-
pst.setString(2, sessionInfo.userId);
178-
}, result -> {
179-
if (result.next()) {
180-
return result.getString("external_user_id");
181-
}
182-
return null;
183-
});
184-
if (externalUserId != null) {
185-
sessionInfo.userId = externalUserId;
186-
}
175+
if (finalUserId != null) {
176+
sessionInfo.userId = finalUserId;
187177
}
188178

189179
return sessionInfo;

0 commit comments

Comments
 (0)