next-auth: GitHub user.email can be null
Describe the bug
GitHub user.email field does not always contains the email. It has the email, if the user decided to share the email to public.
So, session.user.email can be null in most of the cases.
To Reproduce
- Create a simple app to with the GitHub provider
- Disable sharing your GitHub email to public
- If you try to login then
session.user.emailwill be null
Expected behavior
We should expose the email in session.user
Additional context
In order to get the email, we need to add user.email scope to GitHub. We need to fetch emails using an API endpoint.
I read the GitHub provider file and I’m not sure we can apply login in the profile creating function. What’s the best way to implement the above logic?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (5 by maintainers)
@arunoda the default
userscope is sufficient as @iaincollins pointed out. You need to request the user’s emails in a customsignincallback as they are never included in the metadata returned by github:Since
fetchruns on the server, you have to use a Node.js implementation, for examplenode-fetch.Hope this helps
next-auth v4 will include
emailby default. In fact, we aim for returning exactly the same fields for ALL our providers by default. See #2524@aslakhellesoy Thank you so much for this. I was getting a
nullemail until I added your callback. I’m usinguser:email.Shouldn’t that be mentioned in the docs? If that’s always the case that seems like a pretty big deal that users created via GitHub by Next Auth never have an associated email by default.
Here is my slightly more compact version that is TypeScript-friendly, and checks for GitHub (Next Auth v3):
Hey, I faced this issue because I had a Github App instead of a Github OAuth App. See if that is the case for you too, hope it helps!
@aslakhellesoy can you please with the full example
I tried to use this code but nothing happenend. Can you please explain me what to do? Where should I put your code?
This is not an error it is mentioned in the documentation.
GitHub allows the user not to expose their email address to OAuth services if they have relevant privacy settings are enabled.
You can modify the
scopeproperty on any provider - though we are already requesting it implicitly via theuserproperty (see GitHub OAuth docs for details on scope options) so in this case will not make any difference.