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

Open
opened 2026-04-15 22:39:16 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8552
Author: @afn
Created: 3/11/2026
Status: 🔄 Open

Base: mainHead: additional-account-fields


📝 Commits (4)

  • 691789e feat: allow provider config to add extra fields to linked accounts
  • bace714 Fix linter errors
  • 7c6b632 fix: update additional account fields on sign-in
  • 33d9800 Fix linter errors

📊 Changes

6 files changed (+268 additions, -10 deletions)

View changed files

📝 packages/better-auth/src/api/routes/account.test.ts (+222 -0)
📝 packages/better-auth/src/api/routes/callback.ts (+8 -0)
📝 packages/better-auth/src/api/routes/sign-in.ts (+4 -1)
📝 packages/better-auth/src/oauth2/link-account.ts (+22 -9)
📝 packages/core/src/oauth2/oauth-provider.ts (+11 -0)
📝 pnpm-workspace.yaml (+1 -0)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8552 **Author:** [@afn](https://github.com/afn) **Created:** 3/11/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `additional-account-fields` --- ### 📝 Commits (4) - [`691789e`](https://github.com/better-auth/better-auth/commit/691789e92c8fb33def17bcff384a7deb43e01a9b) feat: allow provider config to add extra fields to linked accounts - [`bace714`](https://github.com/better-auth/better-auth/commit/bace714f0bcddaa52eab9eefc6a9f00d3a5c7a14) Fix linter errors - [`7c6b632`](https://github.com/better-auth/better-auth/commit/7c6b6328d3f129aaa63c7a24c88f998a5a55d78c) fix: update additional account fields on sign-in - [`33d9800`](https://github.com/better-auth/better-auth/commit/33d9800e4a8cd16d417d4ca72a4791a672ff8ca2) Fix linter errors ### 📊 Changes **6 files changed** (+268 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/account.test.ts` (+222 -0) 📝 `packages/better-auth/src/api/routes/callback.ts` (+8 -0) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+4 -1) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+22 -9) 📝 `packages/core/src/oauth2/oauth-provider.ts` (+11 -0) 📝 `pnpm-workspace.yaml` (+1 -0) </details> ### 📄 Description 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. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:39:16 -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#24957