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.
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;
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:
If you think it looks good, I'll make a PR