[PR #8552] Allow provider config to add extra fields to linked accounts #16303

Open
opened 2026-04-13 10:29:18 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/8552

State: open
Merged: No


This allows users to do things like add an additional column to the accounts table with the email address of the linked account (see #2272).

Example usage

export const auth = betterAuth({
  socialProviders: {
    google: {
      getAccountFields: async (_token, userInfo) => {
        // Use `token` to make additional API requests if needed.
        // Otherwise, extract relevant data from userInfo.
        return { providerEmail: userInfo.email };
      },
    },
  },
  account: {
    accountLinking: {
      allowDifferentEmails: true,
    },
    additionalFields: {
      providerEmail: {
        type: "string",
      },
    },
  },
});

Summary by cubic

Adds a provider-level getAccountFields hook to add extra, provider-derived fields to OAuth accounts during linking and sign-in. Fields are merged on create and on updates; core account properties stay protected.

  • New Features

    • Added getAccountFields(tokens, userInfo) to provider options; returned fields are merged into the account during the OAuth callback and sign-in. Undefined values are ignored.
    • Existing accounts are updated with new fields on re-link and sign-in; fields cannot overwrite providerId, accountId, tokens, scope, or expirations.
    • Expanded tests cover linking new/existing accounts and OAuth sign-in for new/existing accounts.
  • Migration

    • Define each new field under account.additionalFields to persist it.
    • Implement getAccountFields in the provider (e.g., return { providerEmail: userInfo.email }).

Written for commit 33d9800e4a. Summary will update on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/8552 **State:** open **Merged:** No --- This allows users to do things like add an additional column to the accounts table with the email address of the linked account (see #2272). ## Example usage ```typescript export const auth = betterAuth({ socialProviders: { google: { getAccountFields: async (_token, userInfo) => { // Use `token` to make additional API requests if needed. // Otherwise, extract relevant data from userInfo. return { providerEmail: userInfo.email }; }, }, }, account: { accountLinking: { allowDifferentEmails: true, }, additionalFields: { providerEmail: { type: "string", }, }, }, }); ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a provider-level `getAccountFields` hook to add extra, provider-derived fields to OAuth accounts during linking and sign-in. Fields are merged on create and on updates; core account properties stay protected. - **New Features** - Added `getAccountFields(tokens, userInfo)` to provider options; returned fields are merged into the account during the OAuth callback and sign-in. Undefined values are ignored. - Existing accounts are updated with new fields on re-link and sign-in; fields cannot overwrite `providerId`, `accountId`, tokens, scope, or expirations. - Expanded tests cover linking new/existing accounts and OAuth sign-in for new/existing accounts. - **Migration** - Define each new field under `account.additionalFields` to persist it. - Implement `getAccountFields` in the provider (e.g., return `{ providerEmail: userInfo.email }`). <sup>Written for commit 33d9800e4a8cd16d417d4ca72a4791a672ff8ca2. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 10:29:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16303