@@ -234,12 +234,25 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
234234 // now we know that the recipe user ID is not a primary user, so we can focus on it's one
235235 // login method
236236 assert (recipeUser .loginMethods .length == 1 );
237- LoginMethod recipeUserIdLM = recipeUser .loginMethods [0 ];
238-
237+
239238 Set <String > tenantIds = new HashSet <>();
240239 tenantIds .addAll (recipeUser .tenantIds );
241240 tenantIds .addAll (primaryUser .tenantIds );
242241
242+ checkIfLoginMethodCanBeLinkedOnTenant (con , appIdentifier , authRecipeStorage , tenantIds , recipeUser .loginMethods [0 ], primaryUser );
243+
244+ for (LoginMethod currLoginMethod : primaryUser .loginMethods ) {
245+ checkIfLoginMethodCanBeLinkedOnTenant (con , appIdentifier , authRecipeStorage , tenantIds , currLoginMethod , primaryUser );
246+ }
247+
248+ return new CanLinkAccountsResult (recipeUser .getSupertokensUserId (), primaryUser .getSupertokensUserId (), false );
249+ }
250+
251+ private static void checkIfLoginMethodCanBeLinkedOnTenant (TransactionConnection con , AppIdentifier appIdentifier ,
252+ AuthRecipeSQLStorage authRecipeStorage ,
253+ Set <String > tenantIds , LoginMethod currLoginMethod ,
254+ AuthRecipeUserInfo primaryUser )
255+ throws StorageQueryException , AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdException {
243256 // we loop through the union of both the user's tenantIds and check that the criteria for
244257 // linking accounts is not violated in any of them. We do a union and not an intersection
245258 // cause if we did an intersection, and that yields that account linking is allowed, it could
@@ -251,17 +264,14 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
251264 // intersection, we will get an empty set, but if we do a union, we will get both the tenants and
252265 // do the checks in both.
253266 for (String tenantId : tenantIds ) {
254- TenantIdentifier tenantIdentifier = new TenantIdentifier (
255- appIdentifier .getConnectionUriDomain (), appIdentifier .getAppId (),
256- tenantId );
257267 // we do not bother with getting the storage for each tenant here because
258268 // we get the tenants from the user itself, and the user can only be shared across
259269 // tenants of the same storage - therefore, the storage will be the same.
260270
261- if (recipeUserIdLM .email != null ) {
271+ if (currLoginMethod .email != null ) {
262272 AuthRecipeUserInfo [] usersWithSameEmail = authRecipeStorage
263273 .listPrimaryUsersByEmail_Transaction (appIdentifier , con ,
264- recipeUserIdLM .email );
274+ currLoginMethod .email );
265275 for (AuthRecipeUserInfo user : usersWithSameEmail ) {
266276 if (!user .tenantIds .contains (tenantId )) {
267277 continue ;
@@ -274,10 +284,10 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
274284 }
275285 }
276286
277- if (recipeUserIdLM .phoneNumber != null ) {
287+ if (currLoginMethod .phoneNumber != null ) {
278288 AuthRecipeUserInfo [] usersWithSamePhoneNumber = authRecipeStorage
279289 .listPrimaryUsersByPhoneNumber_Transaction (appIdentifier , con ,
280- recipeUserIdLM .phoneNumber );
290+ currLoginMethod .phoneNumber );
281291 for (AuthRecipeUserInfo user : usersWithSamePhoneNumber ) {
282292 if (!user .tenantIds .contains (tenantId )) {
283293 continue ;
@@ -291,10 +301,10 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
291301 }
292302 }
293303
294- if (recipeUserIdLM .thirdParty != null ) {
304+ if (currLoginMethod .thirdParty != null ) {
295305 AuthRecipeUserInfo [] usersWithSameThirdParty = authRecipeStorage
296306 .listPrimaryUsersByThirdPartyInfo_Transaction (appIdentifier , con ,
297- recipeUserIdLM .thirdParty .id , recipeUserIdLM .thirdParty .userId );
307+ currLoginMethod .thirdParty .id , currLoginMethod .thirdParty .userId );
298308 for (AuthRecipeUserInfo userWithSameThirdParty : usersWithSameThirdParty ) {
299309 if (!userWithSameThirdParty .tenantIds .contains (tenantId )) {
300310 continue ;
@@ -310,8 +320,6 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
310320
311321 }
312322 }
313-
314- return new CanLinkAccountsResult (recipeUser .getSupertokensUserId (), primaryUser .getSupertokensUserId (), false );
315323 }
316324
317325 @ TestOnly
0 commit comments