Add support for email retrieval on sign in for X/ Twitter Oauth 2.0 #12953
alfredsadjei
started this conversation in
Ideas
Replies: 1 comment
-
This works: providers: [
Google({ allowDangerousEmailAccountLinking: true }),
Twitter({
// Opt into OAuth 2.0 so we can request the newer scopes
allowDangerousEmailAccountLinking: true,
// Request the email-capable scope in addition to basic read + offline
authorization: 'https://x.com/i/oauth2/authorize?scope=users.read%20tweet.read%20offline.access%20users.email',
// Ask the v2 userinfo endpoint to include the confirmed email
userinfo: 'https://api.x.com/2/users/me?user.fields=confirmed_email,profile_image_url,verified',
// Map the provider profile to NextAuth's user shape, pulling email from confirmed_email when present
profile(profile) {
const data = profile.data as typeof profile.data & { confirmed_email: string | null }
return {
id: data.id,
name: data.name,
email: data.confirmed_email ?? null,
image: data.profile_image_url ?? null,
}
},
}),
] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
No response
Background
Currently Auth.js does not support email retrieval on sign in for Twitter/X OAuth 2.0. Given that this feature is now supported by X API V2, it would be great to have this supported by Auth.js as well.
Proposal
Given that the
profile
callback on theOAuth2Config<Profile>
interface accepts both the entire profile retrieved by the OAuth provider as well as the retrieved token set, by modifying the twitter provider's auth scope ;we are able to simply fetch the user email (as described by x devs) and attach the resulting email data to the returned user object.
packages/core/src/lib/utils/providers.ts
Yes, I would be willing to contribute with a PR.
Beta Was this translation helpful? Give feedback.
All reactions