使用通行证github2的SailsJS v1.0 Github登录不起作用

时间:2018-07-25 05:40:35

标签: sails.js passport.js passport-github2

我们有一个Sails.JS v0.12应用程序,该应用程序最近已更新为Sails.JS v1.0。以前,我们将护照用于本地登录策略(电子邮件),将护照-github2用于GitHub身份验证npm软件包。更新Sails.JS后,护照-github2不再起作用,但是我们的护照本地策略仍按预期工作。

到目前为止,我已经确定了一个可疑的GitHub回调函数:

passport.authenticate('github',
  function (req, res) {
    res.redirect('/home');
  })(req, res, next);

function(req,res)不再获取res对象,而是获取github用户。我不确定为什么会这样。

我将代码粘贴在这里,希望有人识别出问题:

config.http.js:

module.exports.http = {

  middleware: {

    passportInit    : require('passport').initialize(),
    passportSession : require('passport').session(), 

  order: [
    'cookieParser',
    'session',
    'passportInit',
    'passportSession',
    'bodyParser',
    'compress',
    'poweredBy',
    'router',
    'www',
    'favicon',
  ],

};

AuthController:(仅粘贴github函数)

'github': function (req, res, next) {
    passport.authenticate('github', {
      scope: ['user:email', 'repo'],
      failureRedirect: '/signin',
    },
      function (err, user) {
        sails.log.debug(user);
        req.logIn(user, function (err) {
          if (!err) {
            req.session.authenticated = true;
            req.session.user = user;

            //check user role type
            var userRoles = user.userRoles;
            if (userRoles) {
              var isSuperAdmin = false;
              for(var x =0;x < userRoles.length; x++){                
                if (userRoles[x].toUpperCase() == roles.SUPER_ADMIN.toUpperCase()) {
                  isSuperAdmin = true;
                  break;           
                }                 
              }
              if(isSuperAdmin){
                req.session.isSuperAdmin = true;                    
                res.redirect('/admin');
              }
              else{
                res.redirect('/home');                  
              }
            }
            else {
              res.redirect('/home');              
            }
          }
        });
      })(req, res, next);
  },

  'github/callback': function (req, res, next) {
    passport.authenticate('github',
      function (req, res) {

        // when this callback occurs, the req is empty and res contains the github user instead of an actual response object

        res.redirect('/home');
      })(req, res, next);
  }
};

config.passport.js:

var passport = require('passport'),
GitHubStrategy = require('passport-github2').Strategy,
LocalStrategy = require('passport-local').Strategy,
roles = require('../api/constants/Roles.js');

    var githubVerifyHandler = function (token, tokenSecret, profile, done) {
      process.nextTick(function () {
        console.log('github authentication');
        User.findOne({
          uid: profile.id
        }, function (err, user) {
          if (user) {        
            if (user.token !== token) {
              User.update({ uid: profile.id }, { token: token }, function () {

                // runs till here successfully, user is being returned as authenticated 


                return done(null, user);
              });
            } else {
              return done(null, user);
            }
          } else {

            var data = {
              provider: profile.provider,
              uid: profile.id,
              name: profile.displayName,
              token,
              tokenSecret          
            };

            if (profile.emails && profile.emails[0] && profile.emails[0].value) {
              data.email = profile.emails[0].value;
            }
            if (profile.name && profile.name.givenName) {
              //data.firstname = profile.name.givenName;
            }      

            User.create(data).fetch().exec(function (err, user) {
              return done(err, user);
            });
          }
        });
      });
    };



    passport.serializeUser(function (user, done) {
      done(null, user.id);
    });

    passport.deserializeUser(function (id, done) {
      User.findOne({
        id: id
      }, function (err, user) {
        done(err, user);
      });
    });   



    passport.use(new GitHubStrategy({      
      clientID: CLIENT_ID,  // Use your github client id
      clientSecret: CLIENT_SECRET, // Use your github client secret
      callbackURL: 'http://localhost:1337/auth/github/callback'
    }, githubVerifyHandler));

0 个答案:

没有答案