From c3a8ff6dc55cbfbec3b83ae1ea97bb6b861297e5 Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Mon, 24 Feb 2025 11:25:06 -0500 Subject: [PATCH 1/4] chore(core): add upgrade to phone schema script --- core/api/src/debug/upgrade-to-phone-schema.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 core/api/src/debug/upgrade-to-phone-schema.ts diff --git a/core/api/src/debug/upgrade-to-phone-schema.ts b/core/api/src/debug/upgrade-to-phone-schema.ts new file mode 100644 index 0000000000..610faab9e8 --- /dev/null +++ b/core/api/src/debug/upgrade-to-phone-schema.ts @@ -0,0 +1,68 @@ +/** + * how to run: + * + * pnpm tsx src/debug/upgrade-to-phone-schema.ts + * + * : phone number to upgrade to + * : kratos user ID + */ + +import { IdentifierNotFoundError } from "@/domain/authentication/errors" + +import { + AuthWithUsernamePasswordDeviceIdService, + IdentityRepository, +} from "@/services/kratos" +import { UsersRepository } from "@/services/mongoose" +import { setupMongoConnection } from "@/services/mongodb" + +const upgradeToPhoneSchema = async ({ + phone, + userId, +}: { + phone: PhoneNumber + userId: UserId +}) => { + const userUpdated = await UsersRepository().findById(userId) + if (userUpdated instanceof Error) return userUpdated + + const identities = IdentityRepository() + const result = await identities.getUserIdFromIdentifier(phone) + + // phone account must not exist + if (result instanceof IdentifierNotFoundError) { + return await AuthWithUsernamePasswordDeviceIdService().upgradeToPhoneSchema({ + phone, + userId, + }) + } + + if (result instanceof Error) return result + + return new Error(`Schema already upgraded for user ${result}`) +} + +const main = async () => { + const args = process.argv.slice(-2) + const params = { + phone: args[0] as PhoneNumber, + userId: args[1] as UserId, + } + + const result = await upgradeToPhoneSchema(params) + if (result instanceof Error) { + console.error("Error:", result) + return + } + + console.log( + `Successfully upgraded user ${params.userId} to phone schema with phone ${params.phone}`, + ) +} + +setupMongoConnection() + .then(async (mongoose) => { + await main() + if (mongoose) await mongoose.connection.close() + }) + .catch((err) => console.log(err)) From c8186eaaaa39200b4bc9d6de6015d07fb6744c6b Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Mon, 24 Feb 2025 11:58:53 -0500 Subject: [PATCH 2/4] fix: add additional validations --- core/api/src/debug/upgrade-to-phone-schema.ts | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/api/src/debug/upgrade-to-phone-schema.ts b/core/api/src/debug/upgrade-to-phone-schema.ts index 610faab9e8..f8a8fdea6e 100644 --- a/core/api/src/debug/upgrade-to-phone-schema.ts +++ b/core/api/src/debug/upgrade-to-phone-schema.ts @@ -23,23 +23,33 @@ const upgradeToPhoneSchema = async ({ phone: PhoneNumber userId: UserId }) => { - const userUpdated = await UsersRepository().findById(userId) - if (userUpdated instanceof Error) return userUpdated + const user = await UsersRepository().findById(userId) + if (user instanceof Error) return user const identities = IdentityRepository() - const result = await identities.getUserIdFromIdentifier(phone) + const kratosUserId = await identities.getUserIdFromIdentifier(phone) // phone account must not exist - if (result instanceof IdentifierNotFoundError) { + if (kratosUserId instanceof IdentifierNotFoundError) { + if (kratosUserId instanceof Error) return kratosUserId + if (kratosUserId !== user.id) + return new Error( + `User ids do not match. kratosUserId: ${kratosUserId} - UserId: ${user.id}`, + ) return await AuthWithUsernamePasswordDeviceIdService().upgradeToPhoneSchema({ phone, userId, }) } - if (result instanceof Error) return result + if (kratosUserId instanceof Error) return kratosUserId - return new Error(`Schema already upgraded for user ${result}`) + if (kratosUserId !== user.id) + return new Error( + `User ids do not match. kratosUserId: ${kratosUserId} - UserId: ${user.id}`, + ) + + return new Error(`Schema already upgraded for user ${kratosUserId}`) } const main = async () => { From c9acdbd2d826eb5eb3d35e37c8ea320494bbb781 Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Mon, 24 Feb 2025 12:01:14 -0500 Subject: [PATCH 3/4] fix: update validations --- core/api/src/debug/upgrade-to-phone-schema.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/api/src/debug/upgrade-to-phone-schema.ts b/core/api/src/debug/upgrade-to-phone-schema.ts index f8a8fdea6e..8aab074565 100644 --- a/core/api/src/debug/upgrade-to-phone-schema.ts +++ b/core/api/src/debug/upgrade-to-phone-schema.ts @@ -31,11 +31,6 @@ const upgradeToPhoneSchema = async ({ // phone account must not exist if (kratosUserId instanceof IdentifierNotFoundError) { - if (kratosUserId instanceof Error) return kratosUserId - if (kratosUserId !== user.id) - return new Error( - `User ids do not match. kratosUserId: ${kratosUserId} - UserId: ${user.id}`, - ) return await AuthWithUsernamePasswordDeviceIdService().upgradeToPhoneSchema({ phone, userId, From cbc5044ae342c5aff59b0cafd778d6820acf6b62 Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Mon, 24 Feb 2025 12:14:01 -0500 Subject: [PATCH 4/4] fix: lint issues --- core/api/src/debug/upgrade-to-phone-schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/api/src/debug/upgrade-to-phone-schema.ts b/core/api/src/debug/upgrade-to-phone-schema.ts index 8aab074565..929e2fc792 100644 --- a/core/api/src/debug/upgrade-to-phone-schema.ts +++ b/core/api/src/debug/upgrade-to-phone-schema.ts @@ -31,7 +31,7 @@ const upgradeToPhoneSchema = async ({ // phone account must not exist if (kratosUserId instanceof IdentifierNotFoundError) { - return await AuthWithUsernamePasswordDeviceIdService().upgradeToPhoneSchema({ + return AuthWithUsernamePasswordDeviceIdService().upgradeToPhoneSchema({ phone, userId, })