Passport回调500服务器错误

时间:2017-08-22 12:58:06

标签: javascript node.js passport.js passport-github2

我一直在努力让护照工作。我正在使用passport-github2,但在尝试解决问题的同时我也尝试了护照-facebook和twitter,现在它们似乎都没有。

我有一个简单的应用程序正在使用facebook / twitter登录,现在他们不再工作了,所以这似乎与较新版本的Passport有关。

以下是我在github.js文件中的内容,该文件可通过网址api/auth/github访问

var express = require('express'),
     router = express.Router(),
     config = require('config'),
     githubAuth = config.get('github'),
     githubStrategy = require('passport-github2').Strategy,
     flash = require('connect-flash'),

     GITHUB_CLIENT_ID = githubAuth.githubClientID,
     GITHUB_CLIENT_SECRET = githubAuth.githubClientSecret,

     app = express();

 //passport vars
 var passport = require('passport'),
     session = require('express-session'),
     methodOverride = require('method-override'),
     bodyParser = require("body-parser"),
     cookieParser = require("cookie-parser");



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

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

 passport.use(new githubStrategy({
         clientID: GITHUB_CLIENT_ID,
         clientSecret: GITHUB_CLIENT_SECRET,
         callbackURL: 'http://localhost:3000/api/auth/github/callback'
     },
     function (token, tokenSecret, profile, done) {
         // asynchronous verification, for effect...
         return done(null, profile);
     }
 ));

 app.use(cookieParser());
 app.use(bodyParser());
 app.use(methodOverride());
 app.use(session({
     secret: 'keyboard cat',
     resave: true,
     saveUninitialized: true
 }));
 app.use(passport.initialize());
 app.use(passport.session()); // persistent login sessions
 app.use(flash())

 router.get('/', passport.authenticate('github', {
     scope: ['user']
 }));


 router.get('/callback',
     passport.authenticate('github', {
         failureRedirect: '/',
         successRedirect: '/'
     }),
     function (req, res) {
         res.send('Hello World!!');
     }
 );

 module.exports = router;

当我去http://localhost:3000/api/auth/github时 github让我进行身份验证,然后点击绿色按钮,将我带回回调网址,并且所有内容都会爆炸,我看到此消息:

Error
   at /Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/passport-github2/lib/strategy.js:96:19
   at passBackControl (/Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/oauth/lib/oauth2.js:132:9)
   at IncomingMessage.<anonymous> (/Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/oauth/lib/oauth2.js:157:7)
   at emitNone (events.js:110:20)
   at IncomingMessage.emit (events.js:207:7)
   at endReadableNT (_stream_readable.js:1059:12)
   at _combinedTickCallback (internal/process/next_tick.js:138:11)
   at process._tickCallback (internal/process/next_tick.js:180:9)

控制台:

GET http://localhost:3000/api/auth/github/callback?code=60a130c5c54a6fc30266 500 (Internal Server Error)

我已经在网上搜索过,我找不到适合我的解决方案。 github中有一个线程,但尚未正式提供解决方案:

https://github.com/jaredhanson/passport-facebook/issues/100

任何帮助将不胜感激!!

1 个答案:

答案 0 :(得分:0)

解决。问题是我必须将所有内容保存在与app.js相同的文件中

我仍然想知道是否有更好/更整洁的方法来分离这个文件而不会破坏代码。