Skip to content

Commit 3234b87

Browse files
committed
fix: signup
1 parent 0125d22 commit 3234b87

File tree

7 files changed

+194
-46
lines changed

7 files changed

+194
-46
lines changed

src/main/java/io/supertokens/storage/postgresql/Start.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,8 @@ public AuthRecipeUserInfo signUp(TenantIdentifier tenantIdentifier, String id, S
11131113

11141114
if (isUniqueConstraintError(serverMessage, config.getEmailPasswordUserToTenantTable(), "email")) {
11151115
throw new DuplicateEmailException();
1116+
} else if (isPrimaryKeyError(serverMessage, config.getRecipeUserTenantsTable())) {
1117+
throw new DuplicateEmailException();
11161118
} else if (isPrimaryKeyError(serverMessage, config.getEmailPasswordUsersTable())
11171119
|| isPrimaryKeyError(serverMessage, config.getUsersTable())
11181120
|| isPrimaryKeyError(serverMessage, config.getEmailPasswordUserToTenantTable())
@@ -1566,6 +1568,9 @@ public AuthRecipeUserInfo signUp(
15661568
"third_party_user_id")) {
15671569
throw new DuplicateThirdPartyUserException();
15681570

1571+
} else if (isPrimaryKeyError(serverMessage, config.getRecipeUserTenantsTable())) {
1572+
throw new DuplicateThirdPartyUserException();
1573+
15691574
} else if (isPrimaryKeyError(serverMessage, config.getThirdPartyUsersTable())
15701575
|| isPrimaryKeyError(serverMessage, config.getUsersTable())
15711576
|| isPrimaryKeyError(serverMessage, config.getThirdPartyUserToTenantTable())
@@ -2160,6 +2165,16 @@ public AuthRecipeUserInfo createUser(TenantIdentifier tenantIdentifier,
21602165
PostgreSQLConfig config = Config.getConfig(this);
21612166
ServerErrorMessage serverMessage = ((PSQLException) actualException).getServerErrorMessage();
21622167

2168+
if (isPrimaryKeyError(serverMessage, config.getRecipeUserTenantsTable())) {
2169+
// For passwordless, recipe_user_tenants primary key error means duplicate email or phone number
2170+
// Determine which one based on what was provided
2171+
if (email != null) {
2172+
throw new DuplicateEmailException();
2173+
} else {
2174+
throw new DuplicatePhoneNumberException();
2175+
}
2176+
}
2177+
21632178
if (isPrimaryKeyError(serverMessage, config.getPasswordlessUsersTable())
21642179
|| isPrimaryKeyError(serverMessage, config.getUsersTable())
21652180
|| isPrimaryKeyError(serverMessage, config.getPasswordlessUserToTenantTable())
@@ -4215,6 +4230,8 @@ public AuthRecipeUserInfo signUpWithCredentialsRegister_Transaction(TenantIdenti
42154230
Logging.error(this, errorMessage.getMessage(), true);
42164231
Logging.error(this, email, true);
42174232
throw new DuplicateUserEmailException();
4233+
} else if (isPrimaryKeyError(errorMessage, config.getRecipeUserTenantsTable())) {
4234+
throw new DuplicateUserEmailException();
42184235
} else if (isPrimaryKeyError(errorMessage, config.getWebAuthNUsersTable())
42194236
|| isPrimaryKeyError(errorMessage, config.getUsersTable())
42204237
|| isPrimaryKeyError(errorMessage, config.getWebAuthNUserToTenantTable())
@@ -4254,6 +4271,8 @@ public AuthRecipeUserInfo signUp_Transaction(TenantIdentifier tenantIdentifier,
42544271

42554272
if (isUniqueConstraintError(errorMessage, config.getWebAuthNUserToTenantTable(),"email")) {
42564273
throw new DuplicateUserEmailException();
4274+
} else if (isPrimaryKeyError(errorMessage, config.getRecipeUserTenantsTable())) {
4275+
throw new DuplicateUserEmailException();
42574276
} else if (isPrimaryKeyError(errorMessage, config.getWebAuthNUsersTable())
42584277
|| isPrimaryKeyError(errorMessage, config.getUsersTable())
42594278
|| isPrimaryKeyError(errorMessage, config.getWebAuthNUserToTenantTable())

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
package io.supertokens.storage.postgresql.queries;
1818

19+
import static java.lang.System.currentTimeMillis;
20+
import java.sql.Connection;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.Collections;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.Set;
30+
import java.util.stream.Collectors;
31+
32+
import io.supertokens.pluginInterface.ACCOUNT_INFO_TYPE;
33+
import static io.supertokens.pluginInterface.RECIPE_ID.EMAIL_PASSWORD;
1934
import io.supertokens.pluginInterface.RowMapper;
2035
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
2136
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
@@ -27,20 +42,13 @@
2742
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
2843
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
2944
import io.supertokens.storage.postgresql.PreparedStatementValueSetter;
45+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
46+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.executeBatch;
47+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
3048
import io.supertokens.storage.postgresql.Start;
3149
import io.supertokens.storage.postgresql.config.Config;
32-
import io.supertokens.storage.postgresql.utils.Utils;
33-
34-
import java.sql.Connection;
35-
import java.sql.ResultSet;
36-
import java.sql.SQLException;
37-
import java.util.*;
38-
import java.util.stream.Collectors;
39-
40-
import static io.supertokens.pluginInterface.RECIPE_ID.EMAIL_PASSWORD;
41-
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.*;
4250
import static io.supertokens.storage.postgresql.config.Config.getConfig;
43-
import static java.lang.System.currentTimeMillis;
51+
import io.supertokens.storage.postgresql.utils.Utils;
4452

4553
public class EmailPasswordQueries {
4654
static String getQueryToCreateUsersTable(Start start) {
@@ -334,6 +342,23 @@ public static AuthRecipeUserInfo signUp(Start start, TenantIdentifier tenantIden
334342
});
335343
}
336344

345+
{ // recipe_user_tenants
346+
String QUERY = "INSERT INTO " + getConfig(start).getRecipeUserTenantsTable()
347+
+ "(app_id, recipe_user_id, tenant_id, recipe_id, account_info_type, third_party_id, third_party_user_id, account_info_value)"
348+
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
349+
350+
update(sqlCon, QUERY, pst -> {
351+
pst.setString(1, tenantIdentifier.getAppId());
352+
pst.setString(2, userId);
353+
pst.setString(3, tenantIdentifier.getTenantId());
354+
pst.setString(4, EMAIL_PASSWORD.toString());
355+
pst.setString(5, ACCOUNT_INFO_TYPE.EMAIL.toString());
356+
pst.setString(6, "");
357+
pst.setString(7, "");
358+
pst.setString(8, email);
359+
});
360+
}
361+
337362
UserInfoPartial userInfo = new UserInfoPartial(userId, email, passwordHash, timeJoined);
338363
fillUserInfoWithTenantIds_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);
339364
fillUserInfoWithVerified_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,10 @@ static String getQueryToCreateRecipeUserTenantsTable(Start start) {
299299
+ "recipe_id VARCHAR(128) NOT NULL,"
300300
+ "account_info_type VARCHAR(8) NOT NULL,"
301301
+ "account_info_value TEXT NOT NULL,"
302+
+ "third_party_id VARCHAR(28),"
303+
+ "third_party_user_id VARCHAR(256),"
302304
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, null, "pkey")
303-
+ " PRIMARY KEY (app_id, recipe_user_id, tenant_id),"
305+
+ " PRIMARY KEY (app_id, tenant_id, recipe_id, account_info_type, third_party_id, third_party_user_id, account_info_value),"
304306
+ "CONSTRAINT " + Utils.getConstraintName(schema, tableName, "tenant_id", "fkey")
305307
+ " FOREIGN KEY(app_id, tenant_id)"
306308
+ " REFERENCES " + Config.getConfig(start).getTenantsTable() + " (app_id, tenant_id) ON DELETE CASCADE"
@@ -331,7 +333,8 @@ static String getQueryToCreateTenantIndexForRecipeUserTenantsTable(Start start)
331333

332334
static String getQueryToCreateAccountInfoIndexForRecipeUserTenantsTable(Start start) {
333335
return "CREATE INDEX IF NOT EXISTS idx_recipe_user_tenants_account_info ON "
334-
+ Config.getConfig(start).getRecipeUserTenantsTable() + "(app_id, tenant_id, account_info_type, account_info_value);";
336+
+ Config.getConfig(start).getRecipeUserTenantsTable()
337+
+ "(app_id, tenant_id, account_info_type, third_party_id, account_info_value);";
335338
}
336339

337340
static String getQueryToCreatePrimaryUserIndexForPrimaryUserTenantsTable(Start start) {
@@ -847,7 +850,9 @@ public static void deleteAllTables(Start start) throws SQLException, StorageQuer
847850
+ getConfig(start).getKeyValueTable() + ","
848851
+ getConfig(start).getAppIdToUserIdTable() + ","
849852
+ getConfig(start).getUserIdMappingTable() + ","
853+
+ getConfig(start).getRecipeUserTenantsTable() + ","
850854
+ getConfig(start).getUsersTable() + ","
855+
+ getConfig(start).getPrimaryUserTenantsTable() + ","
851856
+ getConfig(start).getAccessTokenSigningKeysTable() + ","
852857
+ getConfig(start).getTenantFirstFactorsTable() + ","
853858
+ getConfig(start).getTenantRequiredSecondaryFactorsTable() + ","

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

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616

1717
package io.supertokens.storage.postgresql.queries;
1818

19+
import java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.stream.Collectors;
30+
31+
import javax.annotation.Nonnull;
32+
import javax.annotation.Nullable;
33+
34+
import io.supertokens.pluginInterface.ACCOUNT_INFO_TYPE;
35+
import static io.supertokens.pluginInterface.RECIPE_ID.PASSWORDLESS;
1936
import io.supertokens.pluginInterface.RowMapper;
2037
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
2138
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
@@ -30,21 +47,13 @@
3047
import io.supertokens.pluginInterface.sqlStorage.SQLStorage.TransactionIsolationLevel;
3148
import io.supertokens.storage.postgresql.ConnectionPool;
3249
import io.supertokens.storage.postgresql.PreparedStatementValueSetter;
50+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
51+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.executeBatch;
52+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
3353
import io.supertokens.storage.postgresql.Start;
3454
import io.supertokens.storage.postgresql.config.Config;
35-
import io.supertokens.storage.postgresql.utils.Utils;
36-
37-
import javax.annotation.Nonnull;
38-
import javax.annotation.Nullable;
39-
import java.sql.Connection;
40-
import java.sql.ResultSet;
41-
import java.sql.SQLException;
42-
import java.util.*;
43-
import java.util.stream.Collectors;
44-
45-
import static io.supertokens.pluginInterface.RECIPE_ID.PASSWORDLESS;
46-
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.*;
4755
import static io.supertokens.storage.postgresql.config.Config.getConfig;
56+
import io.supertokens.storage.postgresql.utils.Utils;
4857

4958
public class PasswordlessQueries {
5059
public static String getQueryToCreateUsersTable(Start start) {
@@ -468,6 +477,36 @@ public static AuthRecipeUserInfo createUser(Start start, TenantIdentifier tenant
468477
pst.setString(5, phoneNumber);
469478
});
470479
}
480+
481+
{ // recipe_user_tenants
482+
String accountInfoType;
483+
String accountInfoValue;
484+
485+
if (email != null) {
486+
accountInfoType = ACCOUNT_INFO_TYPE.EMAIL.toString();
487+
accountInfoValue = email;
488+
} else if (phoneNumber != null) {
489+
accountInfoType = ACCOUNT_INFO_TYPE.PHONE_NUMBER.toString();
490+
accountInfoValue = phoneNumber;
491+
} else {
492+
throw new IllegalArgumentException("Either email or phoneNumber must be provided");
493+
}
494+
495+
String QUERY = "INSERT INTO " + getConfig(start).getRecipeUserTenantsTable()
496+
+ "(app_id, recipe_user_id, tenant_id, recipe_id, account_info_type, third_party_id, third_party_user_id, account_info_value)"
497+
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
498+
499+
update(sqlCon, QUERY, pst -> {
500+
pst.setString(1, tenantIdentifier.getAppId());
501+
pst.setString(2, id);
502+
pst.setString(3, tenantIdentifier.getTenantId());
503+
pst.setString(4, PASSWORDLESS.toString());
504+
pst.setString(5, accountInfoType);
505+
pst.setString(6, "");
506+
pst.setString(7, "");
507+
pst.setString(8, accountInfoValue);
508+
});
509+
}
471510
UserInfoPartial userInfo = new UserInfoPartial(id, email, phoneNumber, timeJoined);
472511
fillUserInfoWithTenantIds_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);
473512
fillUserInfoWithVerified_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
package io.supertokens.storage.postgresql.queries;
1818

19+
import java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.stream.Collectors;
30+
31+
import io.supertokens.pluginInterface.ACCOUNT_INFO_TYPE;
32+
import static io.supertokens.pluginInterface.RECIPE_ID.THIRD_PARTY;
1933
import io.supertokens.pluginInterface.RowMapper;
2034
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
2135
import io.supertokens.pluginInterface.authRecipe.LoginMethod;
@@ -27,19 +41,13 @@
2741
import io.supertokens.pluginInterface.thirdparty.ThirdPartyImportUser;
2842
import io.supertokens.storage.postgresql.ConnectionPool;
2943
import io.supertokens.storage.postgresql.PreparedStatementValueSetter;
44+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.execute;
45+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.executeBatch;
46+
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.update;
3047
import io.supertokens.storage.postgresql.Start;
3148
import io.supertokens.storage.postgresql.config.Config;
32-
import io.supertokens.storage.postgresql.utils.Utils;
33-
34-
import java.sql.Connection;
35-
import java.sql.ResultSet;
36-
import java.sql.SQLException;
37-
import java.util.*;
38-
import java.util.stream.Collectors;
39-
40-
import static io.supertokens.pluginInterface.RECIPE_ID.THIRD_PARTY;
41-
import static io.supertokens.storage.postgresql.QueryExecutorTemplate.*;
4249
import static io.supertokens.storage.postgresql.config.Config.getConfig;
50+
import io.supertokens.storage.postgresql.utils.Utils;
4351

4452
public class ThirdPartyQueries {
4553

@@ -163,6 +171,35 @@ public static AuthRecipeUserInfo signUp(Start start, TenantIdentifier tenantIden
163171
});
164172
}
165173

174+
{ // recipe_user_tenants
175+
// Insert row for email
176+
String QUERY = "INSERT INTO " + getConfig(start).getRecipeUserTenantsTable()
177+
+ "(app_id, recipe_user_id, tenant_id, recipe_id, account_info_type, third_party_id, third_party_user_id, account_info_value)"
178+
+ " VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
179+
update(sqlCon, QUERY, pst -> {
180+
pst.setString(1, tenantIdentifier.getAppId());
181+
pst.setString(2, id);
182+
pst.setString(3, tenantIdentifier.getTenantId());
183+
pst.setString(4, THIRD_PARTY.toString());
184+
pst.setString(5, ACCOUNT_INFO_TYPE.EMAIL.toString());
185+
pst.setString(6, thirdParty.id);
186+
pst.setString(7, thirdParty.userId);
187+
pst.setString(8, email);
188+
});
189+
190+
// Insert row for third party id
191+
update(sqlCon, QUERY, pst -> {
192+
pst.setString(1, tenantIdentifier.getAppId());
193+
pst.setString(2, id);
194+
pst.setString(3, tenantIdentifier.getTenantId());
195+
pst.setString(4, THIRD_PARTY.toString());
196+
pst.setString(5, ACCOUNT_INFO_TYPE.THIRD_PARTY.toString());
197+
pst.setString(6, thirdParty.id);
198+
pst.setString(7, thirdParty.userId);
199+
pst.setString(8, thirdParty.userId);
200+
});
201+
}
202+
166203
UserInfoPartial userInfo = new UserInfoPartial(id, email, thirdParty, timeJoined);
167204
fillUserInfoWithTenantIds_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);
168205
fillUserInfoWithVerified_transaction(start, sqlCon, tenantIdentifier.toAppIdentifier(), userInfo);

0 commit comments

Comments
 (0)