Id Prefixing #876

Closed
opened 2026-03-13 08:08:30 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @aidansunbury on GitHub (Mar 19, 2025).

Is this suited for github?

  • Yes, this is suited for github

A common pattern used by apis is to prefix the Id of an entity. Companies like stripe use this pattern.

org_asdfasdf
user_asdfasdf

I would love to be able to configure this behavior with better auth.

Describe the solution you'd like

There are several possible approaches:

  1. Allow database hooks for all entities, including plugins (discussed in #615)
  2. Allow for passing a global prefix function to the auth config, which would let you assign custom prefixes for all Ids. This could also let you use
const prefix = (entityType: string, entityId: string) => {
  if (entitiyType === "organization"){
    return `org_${entitityId}`
  }
// handle other cases
}

  1. Allow for passing of Ids manually in api handlers when called on the server
const newOrg = await auth.api.createOrganization({
  body: {
    name: input.name,
     slug: input.name,
     userId: ctx.user.id,
     id: createId("org") // your custom handling
   },
});

Describe alternatives you've considered

The alternative is just to implement the functionality of the API handlers manually, or edit the Id of the entity after creation. Neither of which are ideal approaches.

Additional context

No response

Originally created by @aidansunbury on GitHub (Mar 19, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. A common pattern used by apis is to prefix the Id of an entity. Companies like [stripe](https://docs.stripe.com/api) use this pattern. org_asdfasdf user_asdfasdf I would love to be able to configure this behavior with better auth. ### Describe the solution you'd like There are several possible approaches: 1. Allow database hooks for all entities, including plugins (discussed in #615) 2. Allow for passing a global `prefix` function to the auth config, which would let you assign custom prefixes for all Ids. This could also let you use ```ts const prefix = (entityType: string, entityId: string) => { if (entitiyType === "organization"){ return `org_${entitityId}` } // handle other cases } ``` 3. Allow for passing of Ids manually in api handlers when called on the server ```ts const newOrg = await auth.api.createOrganization({ body: { name: input.name, slug: input.name, userId: ctx.user.id, id: createId("org") // your custom handling }, }); ``` ### Describe alternatives you've considered The alternative is just to implement the functionality of the API handlers manually, or edit the Id of the entity after creation. Neither of which are ideal approaches. ### Additional context _No response_
Author
Owner

@Bekacru commented on GitHub (Mar 21, 2025):

You can do this through advanced.generateId the callback receives the model name

const auth = betterAuth({
    advanced: {
        generateId: (options)=>createId(options.model)
    }
})
@Bekacru commented on GitHub (Mar 21, 2025): You can do this through `advanced.generateId` the callback receives the model name ```ts const auth = betterAuth({ advanced: { generateId: (options)=>createId(options.model) } }) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#876