How to keep the same userId when linking anonymous and social accounts?

Hello.

I opened this issue earlier: https://github.com/better-auth/better-auth/issues/4180

I’d like to confirm the recommended approach for handling anonymous → social account linking without changing the userId:

If I set disableDeleteAnonymousUser: true, is it valid to:
  1. Let Better Auth create the new
    user
    record when the social provider is linked
  2. In onLinkAccount, reassign the new Account.userId to the existing anonymous user
  3. Then delete the newly created
    user
    record, so that only the anonymous user remains as the main user
Would this be the correct and supported way to ensure the anonymous user ID is preserved, instead of attempting to swap user IDs?

Thanks in advance!
GitHub
Hello, I’m currently working on handling data migration when linking an anonymous user account to a social login with onLinkAccount. My question: Is it possible to keep using the old anonymous user...
Was this page helpful?