-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GitHub user.email can be null #374
Comments
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 |
@iaincollins this is certainly is a bug. Let me clarify why?
This is not true. With the It's about using the correct scope and user will decide to give the email or not. |
@arunoda The GitHub documentation I linked to above disagrees. To clarify, you are not guaranteed to get an email address with all OAuth providers. If you want to require one, you can use a custom |
@arunoda the default callbacks: {
signin: async (profile, account, metadata) => {
// https://developer.github.com/v3/users/emails/#list-email-addresses-for-the-authenticated-user
const res = await fetch('https://api.github.com/user/emails', {
headers: {
'Authorization': `token ${account.accessToken}`
}
})
const emails = await res.json()
if (!emails || emails.length === 0) {
return
}
// Sort by primary email - the user may have several emails, but only one of them will be primary
const sortedEmails = emails.sort((a, b) => b.primary - a.primary)
profile.email = sortedEmails[0].email
},
}, Since Hope this helps |
Thanks @aslakhellesoy this callbacks is what I was looking for. |
@aslakhellesoy can you please with the full example import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
const options = {
providers: [
Providers.Google({
clientId: process.env.FRONT_GOOGLE_CLIENT_ID,
clientSecret: process.env.FRONT_GOOGLE_CLIENT_SECRET
}),
Providers.GitHub({
clientId: process.env.FRONT_GITHUB_CLIENT_ID,
clientSecret: process.env.FRONT_GITHUB_CLIENT_SECRET
}),
],
database: process.env.FRONT_DB_URL,
secret: process.env.FRONT_SESSION_SECRET,
session: {
jwt: true,
},
jwt: {
secret: process.env.FRONT_JWT_SECRET,
},
pages: {
},
callbacks: {
signin: async (profile, account, metadata) => {
console.info('we are here to see the callback\nP\nP');
console.log(profile, 'is the profile');
console.log(account, 'is the account');
console.log(metadata, 'is the metadata');
const res = await fetch('https://api.github.com/user/emails', {
headers: {
'Authorization': `token ${account.accessToken}`
}
})
const emails = await res.json()
if (!emails || emails.length === 0) {
return
}
const sortedEmails = emails.sort((a, b) => b.primary - a.primary)
profile.email = sortedEmails[0].email
},
},
events: {},
debug: process.env.NODE_ENV === 'development',
}
export default (req, res) => NextAuth(req, res, options) I tried to use this code but nothing happenend. |
I have fixed it: callbacks:{
signIn
} instead of callbacks:{
signin |
@aslakhellesoy Thank you so much for this. I was getting a
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): signIn: async (profile, account) => {
if (account.provider === 'github') {
const res = await fetch('https://api.github.com/user/emails', {
headers: { Authorization: `token ${account.accessToken}` },
})
const emails = await res.json()
if (emails?.length > 0) {
profile.email = emails.sort((a, b) => b.primary - a.primary)[0].email
}
return true
}
}, |
next-auth v4 will include |
In order to access the pbulic email addresses, the url
|
i follow along a tutorial and got this code. it is in my this is the index page
it keeps returning null as user email. here is the login page
please i will appreciate your help on how i can solve this |
I still face this issue even that it seems to be implemented and supported. But for users with an email address set to private I still face this issue |
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! |
Is there any support for GitHub Apps + NextAuth? Would love to handle the fine-grained repository scopes it has with this (and it also supports multiple callback URLs so you can develop localhost with same app). |
Agree with @IRules . It doesn't help that the next-auth docs link you to the Github Apps page rather than the oAuth apps |
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
session.user.email
will be nullExpected 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?
The text was updated successfully, but these errors were encountered: