Skip to content

Commit d5c06aa

Browse files
authored
fix: session queries (#245)
1 parent 0ab9a98 commit d5c06aa

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/main/java/io/supertokens/storage/postgresql/queries/SessionQueries.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ public static void createNewSession(Start start, TenantIdentifier tenantIdentifi
126126
public static SessionInfo getSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
127127
String sessionHandle)
128128
throws SQLException, StorageQueryException {
129-
// we do this as two separate queries and not one query with left join cause psql does not
130-
// support left join with for update if the right table returns null.
131-
132129
String QUERY =
133130
"SELECT session_handle, user_id, refresh_token_hash_2, session_data, " +
134131
"expires_at, created_at_time, jwt_user_payload, use_static_key FROM " +
@@ -149,21 +146,55 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
149146
return null;
150147
}
151148

152-
QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable()
153-
+ " WHERE app_id = ? AND user_id = ?";
154-
155-
return execute(con, QUERY, pst -> {
149+
QUERY = "SELECT external_user_id " +
150+
"FROM " + getConfig(start).getUserIdMappingTable() + " um2 " +
151+
"WHERE um2.app_id = ? AND um2.supertokens_user_id IN (" +
152+
"SELECT primary_or_recipe_user_id " +
153+
"FROM " + getConfig(start).getUsersTable() + " " +
154+
"WHERE app_id = ? AND user_id IN (" +
155+
"SELECT um1.supertokens_user_id as user_id " +
156+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
157+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
158+
"UNION ALL " +
159+
"SELECT ? " +
160+
"LIMIT 1" +
161+
")" +
162+
") " +
163+
"UNION ALL " +
164+
"SELECT primary_or_recipe_user_id " +
165+
"FROM " + getConfig(start).getUsersTable() + " " +
166+
"WHERE app_id = ? AND user_id IN (" +
167+
"SELECT um1.supertokens_user_id as user_id " +
168+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
169+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
170+
"UNION ALL " +
171+
"SELECT ? " +
172+
"LIMIT 1" +
173+
") " +
174+
"LIMIT 1";
175+
176+
String finalUserId = execute(con, QUERY, pst -> {
156177
pst.setString(1, tenantIdentifier.getAppId());
157-
pst.setString(2, sessionInfo.recipeUserId);
178+
pst.setString(2, tenantIdentifier.getAppId());
179+
pst.setString(3, tenantIdentifier.getAppId());
180+
pst.setString(4, sessionInfo.recipeUserId);
181+
pst.setString(5, sessionInfo.recipeUserId);
182+
pst.setString(6, tenantIdentifier.getAppId());
183+
pst.setString(7, tenantIdentifier.getAppId());
184+
pst.setString(8, sessionInfo.recipeUserId);
185+
pst.setString(9, sessionInfo.recipeUserId);
158186
}, result -> {
159187
if (result.next()) {
160-
String primaryUserId = result.getString("primary_or_recipe_user_id");
161-
if (primaryUserId != null) {
162-
sessionInfo.userId = primaryUserId;
163-
}
188+
return result.getString(1);
164189
}
165-
return sessionInfo;
190+
return sessionInfo.recipeUserId;
166191
});
192+
193+
if (finalUserId != null) {
194+
sessionInfo.userId = finalUserId;
195+
}
196+
197+
return sessionInfo;
167198
}
168199

169200
public static void updateSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,

0 commit comments

Comments
 (0)