Skip to content
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

Email #15

Closed
Sparo opened this issue Nov 4, 2013 · 25 comments
Closed

Email #15

Sparo opened this issue Nov 4, 2013 · 25 comments

Comments

@Sparo
Copy link

Sparo commented Nov 4, 2013

I get this:
emails: [ { value: undefined } ],
in response... so is it valid? because I need email info...

this is request:
app.get('/auth/github', passport.authenticate('github', { scope : "user:email"}));
and this is callback:
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function (req, res) { res.redirect('/'); });

nachiket-synerzip pushed a commit to amitdotpatel/knowledgestoreapi that referenced this issue Nov 22, 2013
Password is not yet available in the profile response received.
Existing issue with passport-github: jaredhanson/passport-github#15
@stash
Copy link

stash commented Jan 28, 2014

http://developer.github.com/v3/users/emails/ is a good source of real emails

@bendecoste
Copy link

To add, the email that is currently fetched is the one that displays on a users profile page, which doesn't have to exist. Using the /emails endpoint should always have something defined.

@createproblem
Copy link

I run exactly into the same problem. If you request the profile: https://developer.github.com/v3/users/#get-a-single-user (this is what passport-github do for your, You only will see the _public_ email which can be set here https://github.com/settings/profile. stash is right you've to request the email endpoint.

@iliakan
Copy link

iliakan commented Aug 2, 2014

@createproblem you mean that passport-github should be patched to get email? How did you solve the problem?

@createproblem
Copy link

@iliakan No, in my eyes this is not an issue what passport-github have to solve. passports-github job is the authentication. If you want the email you have to implement the github api for your own and make an additional request to the email endpoint (https://developer.github.com/v3/users/emails/) as I described above.

@cfsghost
Copy link

Author of Passport-Github do not maintain module for a long time, and features(e.g., email issue) in this module doesn't work since Github upgrades their API to version 3.0, so that we fork its project and re-publish it to NPM with a new name passport-Github2.

Here is the git repo of the new module:
https://github.com/cfsghost/passport-github

@iliakan
Copy link

iliakan commented Dec 11, 2014

Actually, not a problem for me. I implemented everything, works well. In open source btw.

@createproblem
Copy link

👍

@iliakan
Copy link

iliakan commented Dec 13, 2014

The code is like:

module.exports = new GithubStrategy({
    clientID:     config.authProviders.github.appId,
    clientSecret: config.authProviders.github.appSecret,
    callbackURL:  config.server.siteHost + "/auth/callback/github",
    passReqToCallback: true
  },
  function(req, accessToken, refreshToken, profile, done) {

    // this may be a default avatar, or a real user pic, can't be sure
    /* jshint -W106 */
    profile.photos = [
      {
        value: profile._json.avatar_url
      }
    ];

    var options = {
      headers: {
        'User-Agent':    'JavaScript.ru',
        'Authorization': 'token ' + accessToken
      },
      json:    true,
      url:     'https://api.github.com/user/emails'
    };

    // get emails using oauth token
    request(options, function(error, response, body) {
      if (error || response.statusCode != 200) {
        req.log.error(error, body);
        done(null, false, {message: "Ошибка связи с сервером github."});
        return;
      }

//      [ { email: 'iliakan@gmail.com', primary: true, verified: true } ],

      var emails = body.filter(function(email) {
        return email.verified;
      });

      if (!emails.length) {
        return done(null, false, {message: "Почта на github должна быть подтверждена."});
      }

      profile.emails = [
        {value: emails[0].email }
      ];

      authenticateByProfile(req, profile, done);
    });


  }
);

@kunal-mandalia
Copy link

Thanks iliakan, I was having the same problem (email: null) I used your code to get the email address successfully.

@sunjay
Copy link

sunjay commented Nov 22, 2016

For those who see this in the future, the latest way to accomplish this is:

  new GitHubStrategy(
    {
      clientID: ...,
      clientSecret: ...,
      callbackURL: ...,
      scope: 'user:email',
    },
    ...
  );

An extra API call will automatically be made to fetch the email.

Would be great if the documentation could be updated.

@gapsong
Copy link

gapsong commented Nov 26, 2016

@sunjay it still doesnt work for me...
(updted)
The "passport-github2" didnt work. I installed the "passport-github" and it worked. Thanks mate

@oliviertassinari
Copy link

oliviertassinari commented Jan 21, 2017

I have tried passport-github2 with no luck.
I have tried @sunjay solution and it's working thanks to the line he linked and https://github.com/jaredhanson/passport-oauth2/blob/master/lib/strategy.js#L95 .
This issue can be closed once documented.

@animeshsinghweb
Copy link

animeshsinghweb commented Mar 20, 2017

@gapsong @oliviertassinari It was not working, because at passport-github2 strategy, scope should be passed as an array.

Using this for passport-github2 strategy worked fine for me:
If you are using passport-github2 strategy, you just need to scope: ['user:email'] to the options.

passport.use(new GitHubStrategy({
   clientID: process.env.GITHUB_CLIENT_ID,
   clientSecret: process.env.GITHUB_CLIENT_SECRET,
   scope: ['user:email'],
   callbackURL: process.env.GITHUB_CALLBACK_URL
},
...
);

which would be returning emails to the json file return, similar to

emails: [ { value: 'user@xyz,com' } ]`

depending upon the emails attached to their profile.

Alternatively, you can console.log(profile) to see the output returned.

@proshoumma
Copy link

@sunjay thank you man, it really helps 👍

@sreerag-nair
Copy link

You are a life saver, @sunjay ... much thanks!

@timmywheels
Copy link

Thank you @sunjay! Saved me a lot of headaches.

@niftylettuce
Copy link

niftylettuce commented Jan 13, 2020

To anyone reading this - don't waste your time - just use passport-github2 - the core logic of this package's (passport-github's) user profile lookup in this is written very poorly.

Ref: https://github.com/jaredhanson/passport-github/blob/master/lib/strategy.js#L159-L169 vs. https://github.com/cfsghost/passport-github/blob/master/lib/strategy.js#L145-L150

@itswadesh
Copy link

itswadesh commented May 30, 2020

For me passport-github is working much better thatn passport-github2.

@niftylettuce
Copy link

@itswadesh please back up your statement with information as how it is working much better?

@itswadesh
Copy link

@niftylettuce

  • I tried to retrieve user email info. passport-github2 retrieved only public email where as passport-github did both.

  • Could not get typescript working with passport-github2 (may be because of wrong typings file)

@niftylettuce
Copy link

You just need to set scopes to get that information. This is not related to the package as far as I know. TypeScript is completely outside the scope of the package, and not many authors I know on npm support it. Furthermore I highly recommend you do not use it, as it is bad for open-source and it is unnecessary convolution. Look at all the time wasted trying to get TypeScript working..

@itswadesh
Copy link

@niftylettuce I tried
new GithubStrategy( { clientID: GITHUB_CLIENT_ID, clientSecret: GITHUB_CLIENT_SECRET, callbackURL: WWW_URL + '/auth/github/callback' scope: 'user:email', } )

without success

@niftylettuce
Copy link

@itswadesh see https://github.com/ladjs/passport/blob/master/index.js for working implementation.

This is used on https://forwardemail.net and https://lad.sh with success.

@Sparo Sparo closed this as completed Aug 12, 2020
@thepranaygupta
Copy link

@sunjay Thanks a ton! 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests