简体   繁体   English

Passport.js github策略回调总是返回“TypeError:无法设置未定义的属性'用户'”

[英]Passport.js github strategy callback always returns “TypeError: Cannot set property 'user' of undefined”

I am new to passport authentication generally, and am trying to create a simple Express app for using the github strategy passport-github . 我一般是护照身份验证的新手,我正在尝试使用github策略passport-github创建一个简单的Express应用程序。 I have had success using essentially this same code below for both google and twitter strategies, but every time I try to incorporate the github strategy I get a 500 response with this error whenever I hit the callback route: 我已经成功地使用下面相同的代码用于谷歌和推特策略,但每次我尝试合并github策略时,每当我点击回调路线时,我得到500响应此错误:

Cannot set property 'user' of undefined 无法设置undefined的属性'user'

TypeError: Cannot set property 'user' of undefined
    at /Users/dan/Repos/passport-github-portal/node_modules/passport-github/node_modules/passport-oauth/node_modules/passport/lib/passport/http/request.js:45:35
    at pass (/Users/dan/Repos/passport-github-portal/node_modules/passport/lib/authenticator.js:267:43)
    at serialized (/Users/dan/Repos/passport-github-portal/node_modules/passport/lib/authenticator.js:276:7)
    at /Users/dan/Repos/passport-github-portal/app.js:49:7
    at pass (/Users/dan/Repos/passport-github-portal/node_modules/passport/lib/authenticator.js:284:9)
    at Authenticator.serializeUser (/Users/dan/Repos/passport-github-portal/node_modules/passport/lib/authenticator.js:289:5)
    at IncomingMessage.req.login.req.logIn (/Users/dan/Repos/passport-github-portal/node_modules/passport-github/node_modules/passport-oauth/node_modules/passport/lib/passport/http/request.js:43:29)
    at Strategy.strategy.success (/Users/dan/Repos/passport-github-portal/node_modules/passport/lib/middleware/authenticate.js:228:13)
    at verified (/Users/dan/Repos/passport-github-portal/node_modules/passport-github/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth2.js:133:18)
    at /Users/dan/Repos/passport-github-portal/app.js:25:9

My app.js is as follows: 我的app.js如下:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var session = require('express-session');

var routes = require('./routes/index');
var users = require('./routes/users');
var auth = require('./routes/auth');


var app = express();

var GithubStrategy = require('passport-github').Strategy;

passport.use(new GithubStrategy({
  clientID: process.env.GITHUB_CLIENT_ID,
  clientSecret: process.env.GITHUB_CLIENT_SECRET,
  callbackURL: 'http://localhost:3000/auth/github/callback'
}, function(accessToken, refreshToken, profile, done) {
  process.nextTick(function() {
    done(null, profile);
  });
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
  secret: 'wordswordswords'
}));

app.use(passport.initialize());
app.use(passport.session());

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

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

app.use('/', routes);
app.use('/users', users);
app.use('/auth', auth);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

And my /routes/auth.js: 我的/routes/auth.js:

var express = require('express');
var passport = require('passport');
var router = express.Router();


router.route('/github/callback')
  .get(passport.authenticate('github', {
    failureRedirect: '/error'
}), function(req, res) {
  res.redirect('/users');
});

router.route('/github')
  .get(passport.authenticate('github'));

module.exports = router;

In a previous project I implemented both google and twitter strategies within the same app without trouble, but when I attempted to add passport-github, it broke all three strategies, giving me the exact same "Cannot set property 'user' of undefined" error whenever I hit any of the three callback routes. 在之前的项目中,我在同一个应用程序中同时实现了google和twitter策略,但是当我尝试添加passport-github时,它打破了所有三个策略,给了我完全相同的“无法设置属性'用户'未定义”错误每当我点击三条回调路线中的任何一条。

Any help would be greatly appreciated! 任何帮助将不胜感激!

This looks like a compatibility issue, for a quick fix set passport version to: 这看起来像兼容性问题,快速修复设置护照版本:

"passport": "0.2.2"

Long term fix probably involves more work: updating your code so it works with latest version, and/or creating an issue in the repo, or sending a PR with a fix (if you can add backward compatibily on this part to the lib). 长期修复可能涉及更多工作:更新代码以使其适用于最新版本,和/或在仓库中创建问题,或者发送带有修复的PR(如果可以在此部分向后兼容地向lib添加)。

How we guessed this was the problem?: tests started to fail when there was no changes related to authentication + the stacktrace + "latest" in packages.json 我们如何猜到这是问题?:当没有与认证相关的更改+ packages.json中的stacktrace +“latest”时,测试开始失败

This Issue on Github Github上的这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM