subhendukundu / worker-auth-providers

worker-auth-providers is an open-source providers to make authentication easy with workers. Very lightweight script which doesn't need a lot of dependencies. Plug it with any framework or template of workers.
https://authc1.com
279 stars 29 forks source link

GitHub provider only returns public emails #11

Closed nathanclevenger closed 2 years ago

nathanclevenger commented 2 years ago

Currently, the GitHub provider only returns an email if the user has a public email address shared on their profile.

Even though the user:email is part of the scope, to get the user, there needs to be a subsequent call to get their email address. See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user for additional context.

Something like this in https://github.com/subhendukundu/worker-auth-providers/blob/main/src/providers/github/users.js:

    const getUserResponse = await fetch('https://api.github.com/user', {
      method: 'GET',
      headers,
    });
    const data = await getUserResponse.json();
    console.log('[provider user data]', data);
    if (!data.email) {
          // If the user does not have a public email, get another via the GitHub API
          // See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
          const res = await fetch("https://api.github.com/user/emails", {
                method: 'GET',
                headers,
          });
          const emails: GithubEmail[] = await res.json()
          console.log('[provider user emails]', emails);
          data.email = emails
    }
    return data;

If you think it looks good, I'll make a PR

subhendukundu commented 2 years ago

Yup, looks good to me. Please go ahead and make a PR. Thanks