{"record":{"id":"03b4c51053b4076e","repo":"medusajs/medusa","slug":"invalid-data-03b4c5","errorCode":"INVALID_DATA","errorMessage":"The user is already registered and cannot create a new account.","messagePattern":"The user is already registered and cannot create a new account\\.","errorType":"exception","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/medusa/src/api/auth/[auth_provider]/user/route.ts","lineNumber":25,"sourceCode":"  MedusaResponse,\n} from \"@medusajs/framework/http\"\nimport {\n  ContainerRegistrationKeys,\n  MedusaError,\n} from \"@medusajs/framework/utils\"\n\nconst ACTOR_TYPE = \"user\"\n\nexport const POST = async (\n  req: AuthenticatedMedusaRequest,\n  res: MedusaResponse\n) => {\n  const { auth_provider: authProvider } = req.params\n  const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)\n\n  // If an actor is already linked to this auth identity, reject.\n  if (req.auth_context.actor_id) {\n    throw new MedusaError(\n      MedusaError.Types.INVALID_DATA,\n      \"The user is already registered and cannot create a new account.\"\n    )\n  }\n\n  if (!req.auth_context.user_metadata?.email) {\n    throw new MedusaError(\n      MedusaError.Types.INVALID_DATA,\n      \"Email is required to create a user account.\"\n    )\n  }\n\n  // Check that the auth identity was created by the provider named in the route.\n  const providerIdentities = await query\n    .graph({\n      entity: \"auth_identity\",\n      fields: [\"id\", \"provider_identities.provider\"],\n      filters: {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/medusa/src/api/auth/[auth_provider]/user/route.ts#L7-L43","documentation":"Thrown by POST /auth/:auth_provider/user when the JWT attached to the request already carries an actor_id, meaning the auth identity is already linked to a user. The endpoint is only for first-time user creation after registering an auth identity; subsequent logins should use the token endpoints instead. It maps to HTTP 400 (INVALID_DATA).","triggerScenarios":"Calling POST /auth/emailpass/user (or any provider's user route) with an Authorization: Bearer <jwt> whose auth_context already has actor_id set, i.e. after the user was already created for that identity.","commonSituations":"Client keeps calling the register-user endpoint on every login flow; front-end persists the JWT and re-runs signup; testing reuse of the same token after successful user creation.","solutions":["Do not call /auth/:provider/user when already registered — use /auth/user/:actor_type or refresh the token instead","Clear the stored JWT/token before retrying first-user creation","Check req.auth_context.actor_id in your client flow to branch login vs signup","If a new user is genuinely needed, authenticate with a different identity first"],"exampleFix":"// before\nawait sdk.auth.registerUser('emailpass', 'user', token) // token already has actor_id\n\n// after\nif (!token?.actor_id) {\n  await sdk.auth.registerUser('emailpass', 'user', token)\n} else {\n  // already registered, just refresh\n  await sdk.auth.refresh()\n}","handlingStrategy":"validation","validationCode":"// Decode stored JWT and check actor_id before calling register-user\nfunction parseJwt(t) { return JSON.parse(atob(t.split('.')[1])) }\nconst claims = parseJwt(storedToken)\nif (!claims.actor_id) {\n  await fetch(`/auth/${provider}/user`, { method: 'POST', headers: { Authorization: `Bearer ${storedToken}` } })\n}","typeGuard":"function needsUserRegistration(claims: { actor_id?: string }): boolean {\n  return !claims.actor_id\n}","tryCatchPattern":"catch (e) { if (e.type === 'invalid_data' && /already registered/.test(e.message)) { /* proceed to login/refresh */ } else throw e }","preventionTips":["Only call /auth/:provider/user on first signup","Inspect the JWT's actor_id claim before registering a user","Treat 'already registered' as success and switch to login flow"],"tags":["auth","user-registration","already-exists"],"backgroundTag":"user-already-registered","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}