[GH-ISSUE #1479] Generic OAuth Plugin doesn't work when the service doesn't provide the email #8779

Closed
opened 2026-04-13 03:59:05 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @alessandrojean on GitHub (Feb 17, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1479

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a config for a generic provider that does not provide the user email (similar to Twitter/X).
  2. Try returning { email: null } or { email: user.username } as a workaround, like Twitter/X provider does.
  3. It always throws a email_is_missing or email_doesn't_match error, even with allowDifferentEmails set to true (as suggested in the manually linking docs).

Current vs. Expected behavior

It should allow the custom provider to return nullable emails or some other value (such as the username as a workaround), like the built-in OAuth providers does (refer to Twitter/X).

What version of Better Auth are you using?

1.1.18

Provide environment information

- OS: Ubuntu 24.10
- Browser: Firefox 135.0
- Framework: Nuxt

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
import type { User } from "better-auth"
import { genericOAuth } from "better-auth/plugins"

export const auth = betterAuth({
  account: {
    accountLinking: {
      enabled: true,
      allowDifferentEmails: true,
    },
  },
  plugins: [
    genericOAuth({
      config: [
        {
          providerId: 'trakt',
          clientId: process.env.TRAKT_CLIENT_ID as string,
          clientSecret: process.env.TRAKT_CLIENT_SECRET as string,
          authorizationUrl: 'https://trakt.tv/oauth/authorize',
          tokenUrl: 'https://api.trakt.tv/oauth/token',
          redirectURI: `${config.public.app.url}/api/v1/auth/oauth2/callback/trakt`,
          getUserInfo: async (tokens) => {
            const user = await $fetch<TraktUser>('https://api.trakt.tv/users/me', {
              query: { extended: 'full' },
              headers: {
                'Authorization': `Bearer ${tokens.accessToken}`,
                'trakt-api-version': '2',
                'trakt-api-key': process.env.TRAKT_CLIENT_ID as string,
              },
            })

            return {
              id: user.ids.slug,
              // Trakt does not provide user emails,
              email: user.username,
              emailVerified: false,
              name: user.name,
              image: user.images.avatar.full,
            } as User
          },
        }
      ],
    }),
  ],
});

interface TraktUser {
  username: string
  name: string
  ids: {
    slug: string
  }
  about: string
  images: {
    avatar: {
      full: string
    }
  }
}

Additional context

I was trying to add a Trakt integration with my project, and they do not provide the user emails (stated on their API repo). I was also planning to add a MyAnimeList integration, and it's the same case, they do not provide the user email.

Their authentication follows the OAuth2 protocol, and its reference is available here.

Originally created by @alessandrojean on GitHub (Feb 17, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1479 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a config for a generic provider that does not provide the user email (similar to Twitter/X). 2. Try returning `{ email: null }` or `{ email: user.username }` as a workaround, like Twitter/X provider does. 3. It always throws a `email_is_missing` or `email_doesn't_match` error, even with `allowDifferentEmails` set to `true` (as suggested in the [manually linking docs](https://www.better-auth.com/docs/concepts/users-accounts#manually-linking-accounts)). ### Current vs. Expected behavior It should allow the custom provider to return nullable emails or some other value (such as the username as a workaround), like the built-in OAuth providers does (refer to Twitter/X). ### What version of Better Auth are you using? 1.1.18 ### Provide environment information ```bash - OS: Ubuntu 24.10 - Browser: Firefox 135.0 - Framework: Nuxt ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" import type { User } from "better-auth" import { genericOAuth } from "better-auth/plugins" export const auth = betterAuth({ account: { accountLinking: { enabled: true, allowDifferentEmails: true, }, }, plugins: [ genericOAuth({ config: [ { providerId: 'trakt', clientId: process.env.TRAKT_CLIENT_ID as string, clientSecret: process.env.TRAKT_CLIENT_SECRET as string, authorizationUrl: 'https://trakt.tv/oauth/authorize', tokenUrl: 'https://api.trakt.tv/oauth/token', redirectURI: `${config.public.app.url}/api/v1/auth/oauth2/callback/trakt`, getUserInfo: async (tokens) => { const user = await $fetch<TraktUser>('https://api.trakt.tv/users/me', { query: { extended: 'full' }, headers: { 'Authorization': `Bearer ${tokens.accessToken}`, 'trakt-api-version': '2', 'trakt-api-key': process.env.TRAKT_CLIENT_ID as string, }, }) return { id: user.ids.slug, // Trakt does not provide user emails, email: user.username, emailVerified: false, name: user.name, image: user.images.avatar.full, } as User }, } ], }), ], }); interface TraktUser { username: string name: string ids: { slug: string } about: string images: { avatar: { full: string } } } ``` ### Additional context I was trying to add a Trakt integration with my project, and they do not provide the user emails (stated on [their API repo](https://github.com/trakt/api-help/issues/59)). I was also planning to add a MyAnimeList integration, and it's the same case, they do not provide the user email. Their authentication follows the OAuth2 protocol, and its reference is available [here](https://trakt.docs.apiary.io/#reference/authentication-oauth/authorize/authorize-application).
GiteaMirror added the lockedbug labels 2026-04-13 03:59:05 -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#8779