{"record":{"id":"66377b195e463fb5","repo":"hcengineering/platform","slug":"accountalreadyexists","errorCode":"AccountAlreadyExists","errorMessage":"AccountAlreadyExists","messagePattern":"AccountAlreadyExists","errorType":"error_code","errorClass":"PlatformError","httpStatus":409,"severity":"error","filePath":"server/account/src/utils.ts","lineNumber":696,"sourceCode":"  email: string,\n  password: string | null,\n  firstName: string,\n  lastName: string,\n  confirmed = false,\n  automatic = false\n): Promise<{ account: AccountUuid, socialId: PersonId }> {\n  const normalizedEmail = cleanEmail(email)\n\n  const emailSocialId = await getEmailSocialId(db, normalizedEmail)\n  let account: AccountUuid\n  let socialId: PersonId\n\n  if (emailSocialId !== null) {\n    const existingAccount = await db.account.findOne({ uuid: emailSocialId.personUuid as AccountUuid })\n\n    if (existingAccount !== null) {\n      ctx.error('An account with the provided email already exists', { email })\n      throw new PlatformError(new Status(Severity.ERROR, platform.status.AccountAlreadyExists, {}))\n    }\n\n    account = emailSocialId.personUuid as AccountUuid\n    socialId = emailSocialId._id\n    // Person exists, but may have different name, need to update with what's been provided\n    await db.person.update({ uuid: account }, { firstName, lastName })\n  } else {\n    // There's no person we can link to this email, so we need to create a new one\n    account = await db.person.insertOne({ firstName, lastName })\n    socialId = await db.socialId.insertOne({\n      type: SocialIdType.EMAIL,\n      value: normalizedEmail,\n      personUuid: account,\n      ...(confirmed ? { verifiedOn: Date.now() } : {})\n    })\n  }\n\n  await createAccount(db, account, confirmed, automatic)","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/server/account/src/utils.ts#L678-L714","documentation":"AccountAlreadyExists is thrown during social sign-up/account creation when an account for the email's social ID already exists in the database. The lookup by emailSocialId.personUuid finds a non-null account, so creating a new one would duplicate it.","triggerScenarios":"Signing up with a social grant (e.g. Google/GitHub) whose email is already linked to an existing account (server/account/src/utils.ts:696).","commonSituations":"A user signs up twice with the same email via social login; email change on the provider caused a re-link attempt; migrating users from another identity provider creates duplicate provisioning calls.","solutions":["Detect the existing account and sign the user in (link the social ID) instead of creating a new account","Handle AccountAlreadyExists in the signup flow by fetching the existing account by the social key","Deduplicate at the caller level: check for an existing person by social key before invoking createAccount"],"exampleFix":"// before\nconst account = await createAccountWithSocialId(ctx, db, identity, email, firstName, lastName)\n// after\nlet account = await findPersonBySocialKey(ctx, token, { socialString: email })\nif (account == null) account = await createAccountWithSocialId(ctx, db, identity, email, firstName, lastName)","handlingStrategy":"try-catch","validationCode":"const existing = await findPersonBySocialKey(ctx, token, { socialString: email })\nif (existing != null) return existing // sign in instead of sign up","typeGuard":"function accountExists(a: unknown): a is { uuid: AccountUuid } { return a != null && typeof (a as any).uuid === 'string' }","tryCatchPattern":"try { account = await createAccountWithSocialId(ctx, db, identity, email, firstName, lastName) } catch (err) { if (isStatus(err, platform.status.AccountAlreadyExists)) { account = await findPersonBySocialKey(ctx, token, { socialString: email }); } else throw err }","preventionTips":["Always attempt social-key lookup before account creation","Make signup flows idempotent for repeated grants with the same email","Handle provider-side email changes by re-linking, not re-creating"],"tags":["duplicate-account","signup","conflict"],"backgroundTag":"account-already-exists","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}