错误:passport.initialize()中间件没有使用什么命令来打电话?

时间:2015-10-04 17:21:01

标签: javascript node.js express

我有以下内容 - 最初由Visual Studio Node Tools生成,使用Express和Jade作为客户端

/**
 * Module dependencies.
 */

var express         = require('express');
var fs              = require('fs');
var routes          = require('./routes');
var user            = require('./routes/user');
var http            = require('http');
var https           = require('https');
var path            = require('path');
var passport        = require('passport');
var googleStrategy  = require('passport-google-oauth').OAuth2Strategy;
var loginHandler    = require('./routes/Login.js');
var auth            = require('./config/auth.js');
var googleSupport   = require('./googleSupport.js');

var googleAuthority = auth.googleAuth;
var googleScopes = '';


// retrieve google scopes
googleScopes = googleSupport.discoverServiceScopes(auth.googleAuth);

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

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

passport.use(new googleStrategy({
    clientID: googleAuthority.clientId,
    clientSecret: googleAuthority.clientSecret,
    callbackURL: googleAuthority.callbackUrl
},
function (accessToken, refreshToken, profile, done) {
    return done(null, profile);
}
));
var request = require('request');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}

console.log('extracting service scopes');

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

app.get('/', routes.index);
app.get('/index', routes.index);

// google login support

// go to login page.
app.get('/googleLogin', passport.authenticate('google', { scope: [googleScopes] }), 
    function (req, res) {
        res.redirect('/');
    }
);

app.get('/AuthorizeGoogle', passport.authenticate('google', { failureRedirect: '/'}), 
    function (req, res) { 
        res.redirect('/');
    });


http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});

事情就是当它验证用户时我得到以下

500 Error: passport.initialize() middleware not in use
at IncomingMessage.req.login.req.logIn (C:\Node\YourLivesN\YourLivesN\node_modules\passport\lib\http\request.js:44:34)
at Strategy.module.exports.strategy.success (C:\Node\YourLivesN\YourLivesN\node_modules\passport\lib\middleware\authenticate.js:228:13)
at verified (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:179:18)
at Strategy._verify (C:\Node\YourLivesN\YourLivesN\app.js:41:12)
at C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:195:22
at C:\Node\YourLivesN\YourLivesN\node_modules\passport-google-oauth\lib\passport-google-oauth\oauth2.js:115:7
at passBackControl (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:125:9)
at IncomingMessage. (C:\Node\YourLivesN\YourLivesN\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16

现在我假设这与此问题passport.js passport.initialize() middleware not in use的原因相同,但无法查看我的代码中调用的顺序。

所以任何人都可以告诉我放置代码的各个部分的顺序。

由于

1 个答案:

答案 0 :(得分:1)

尝试更改此部分

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

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

进一步阅读主题:Difference between app.use and app.get in express.js ,我建议你在一个独立的文件中分开护照配置,并在需要认证的路径上要求它。示例教程:https://www.youtube.com/watch?v=zbfet_-Z5UQ&index=12&list=PLZm85UZQLd2Q946FgnllFFMa0mfQLrYDL