Skip to content

Commit

Permalink
don't error if identity already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-town committed May 6, 2024
1 parent ffb6e9a commit bed48fe
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions api/server/handlers/user/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bed48fe

Please sign in to comment.