From bed48fe3a8bdb41d77c59dc2f9a6fae6a75d3a31 Mon Sep 17 00:00:00 2001 From: dgtown Date: Mon, 6 May 2024 15:17:34 -0400 Subject: [PATCH] don't error if identity already exists --- api/server/handlers/user/migrate.go | 38 +++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/api/server/handlers/user/migrate.go b/api/server/handlers/user/migrate.go index 9a4f01c10a..70cd533529 100644 --- a/api/server/handlers/user/migrate.go +++ b/api/server/handlers/user/migrate.go @@ -128,15 +128,39 @@ func (u *MigrateUsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) continue } - createdIdentity, _, err := u.Config().Ory.IdentityAPI.CreateIdentity(u.Config().OryApiKeyContextWrapper(ctx)).CreateIdentityBody(createIdentityBody).Execute() + createdIdentity, resp, err := u.Config().Ory.IdentityAPI.CreateIdentity(u.Config().OryApiKeyContextWrapper(ctx)).CreateIdentityBody(createIdentityBody).Execute() if err != nil { - errString := fmt.Sprintf("error creating identity: %s", err.Error()) - if len(migrationErrors[err.Error()]) == 0 { - migrationErrors[errString] = []uint{} + switch resp.StatusCode { + // identity already exists, so we need to list the identities and find the one that matches + case 409: + identities, _, err := u.Config().Ory.IdentityAPI.ListIdentities(u.Config().OryApiKeyContextWrapper(ctx)).CredentialsIdentifier(user.Email).Execute() + if err != nil { + errString := fmt.Sprintf("error calling list identities``: %v\n", err) + if len(migrationErrors[err.Error()]) == 0 { + migrationErrors[errString] = []uint{} + } + migrationErrors[errString] = append(migrationErrors[errString], user.ID) + continue + } + + if len(identities) != 1 { + errString := fmt.Sprintf("expected 1 identity, got %d", len(identities)) + if len(migrationErrors[err.Error()]) == 0 { + migrationErrors[errString] = []uint{} + } + migrationErrors[errString] = append(migrationErrors[errString], user.ID) + continue + } + + createdIdentity = &identities[0] + default: + errString := fmt.Sprintf("error creating identity: %s", err.Error()) + if len(migrationErrors[err.Error()]) == 0 { + migrationErrors[errString] = []uint{} + } + migrationErrors[errString] = append(migrationErrors[errString], user.ID) + continue } - migrationErrors[errString] = append(migrationErrors[errString], user.ID) - - continue } user.AuthProvider = models.AuthProvider_Ory