Skip to content

Commit

Permalink
fix: adds recipe user id in session (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc authored Sep 13, 2023
1 parent 36098ff commit 57ce073
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,11 @@ public AuthRecipeUserInfo getPrimaryUserById(AppIdentifier appIdentifier, String
@Override
public String getPrimaryUserIdStrForUserId(AppIdentifier appIdentifier, String userId)
throws StorageQueryException {
// TODO:...
return null;
try {
return GeneralQueries.getPrimaryUserIdStrForUserId(this, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,21 @@ public static AuthRecipeUserInfo getPrimaryUserInfoForUserId_Transaction(Start s
return userIdToAuthRecipeUserInfo.get(pUserId);
}

public static String getPrimaryUserIdStrForUserId(Start start, AppIdentifier appIdentifier, String id)
throws SQLException, StorageQueryException {
String QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable() +
" WHERE user_id = ? AND app_id = ?";
return execute(start, QUERY, pst -> {
pst.setString(1, id);
pst.setString(2, appIdentifier.getAppId());
}, result -> {
if (result.next()) {
return result.getString("primary_or_recipe_user_id");
}
return null;
});
}

public static AuthRecipeUserInfo getPrimaryUserInfoForUserId(Start start, AppIdentifier appIdentifier, String id)
throws SQLException, StorageQueryException {
try (Connection con = ConnectionPool.getConnection(start)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
tenantIdentifier.getAppId() + "~" + tenantIdentifier.getTenantId() + "~" + sessionHandle +
Config.getConfig(start).getSessionInfoTable());

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 " + getConfig(start).getSessionInfoTable()
+ " WHERE app_id = ? AND tenant_id = ? AND session_handle = ?";
String QUERY =
"SELECT sess.session_handle, sess.user_id, sess.refresh_token_hash_2, sess.session_data, sess" +
".expires_at, "
+
"sess.created_at_time, sess.jwt_user_payload, sess.use_static_key, users" +
".primary_or_recipe_user_id FROM " +
getConfig(start).getSessionInfoTable()
+ " AS sess LEFT JOIN " + getConfig(start).getUsersTable() +
" as users ON sess.app_id = users.app_id AND sess.user_id = users.user_id WHERE sess.app_id =" +
" ? AND " +
"sess.tenant_id = ? AND sess.session_handle = ?";
return execute(con, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, tenantIdentifier.getTenantId());
Expand Down Expand Up @@ -306,9 +314,17 @@ public static int updateSession(Start start, TenantIdentifier tenantIdentifier,

public static SessionInfo getSession(Start start, TenantIdentifier tenantIdentifier, String sessionHandle)
throws SQLException, StorageQueryException {
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 " + getConfig(start).getSessionInfoTable()
+ " WHERE app_id = ? AND tenant_id = ? AND session_handle = ?";
String QUERY =
"SELECT sess.session_handle, sess.user_id, sess.refresh_token_hash_2, sess.session_data, sess" +
".expires_at, "
+
"sess.created_at_time, sess.jwt_user_payload, sess.use_static_key, users" +
".primary_or_recipe_user_id FROM " +
getConfig(start).getSessionInfoTable()
+ " AS sess LEFT JOIN " + getConfig(start).getUsersTable() +
" as users ON sess.app_id = users.app_id AND sess.user_id = users.user_id WHERE sess.app_id =" +
" ? AND " +
"sess.tenant_id = ? AND sess.session_handle = ?";
return execute(start, QUERY, pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, tenantIdentifier.getTenantId());
Expand Down Expand Up @@ -383,10 +399,14 @@ private static SessionInfoRowMapper getInstance() {
@Override
public SessionInfo map(ResultSet result) throws Exception {
JsonParser jp = new JsonParser();
return new SessionInfo(result.getString("session_handle"), result.getString("user_id"),
// if result.getString("primary_or_recipe_user_id") is null, it will be handled by SessionInfo
// constructor
return new SessionInfo(result.getString("session_handle"),
result.getString("primary_or_recipe_user_id"),
result.getString("user_id"),
result.getString("refresh_token_hash_2"),
jp.parse(result.getString("session_data")).getAsJsonObject(), result.getLong("expires_at"),
jp.parse(result.getString("session_data")).getAsJsonObject(),
result.getLong("expires_at"),
jp.parse(result.getString("jwt_user_payload")).getAsJsonObject(),
result.getLong("created_at_time"), result.getBoolean("use_static_key"));
}
Expand Down

0 comments on commit 57ce073

Please sign in to comment.