错误:passport.initialize() 中间件未使用 以啥顺序进行调用?
Posted
技术标签:
【中文标题】错误:passport.initialize() 中间件未使用 以啥顺序进行调用?【英文标题】:Error: passport.initialize() middleware not in use what order to make calls?错误:passport.initialize() 中间件未使用 以什么顺序进行调用? 【发布时间】:2016-01-01 09:03:12 【问题描述】:我有以下内容 - 最初由 Visual Studio 节点工具生成,使用 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 的原因相同,但看不到我的代码中调用的顺序。
那么谁能告诉我放置代码各个部分的顺序。
谢谢
【问题讨论】:
几年前在这里回答:***.com/questions/16781294/… 你的问题很可能是你如何排序你的应用表达式。 这是我在原始问题中链接到的问题,但我不确定我应该把我的电话的顺序 - 我见过的大多数例子都使用 app.confgure 但代码由Visual Studio 不会让所有人都参与其中 - 所以我不确定我应该使用什么顺序。 app.configure 已弃用。重新阅读链接或查看快速主页以获取文档expressjs.com/guide/routing.html 【参考方案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
【讨论】:
以上是关于错误:passport.initialize() 中间件未使用 以啥顺序进行调用?的主要内容,如果未能解决你的问题,请参考以下文章
什么是passport.initialize()? (nodejs快递)
Google oauth20 的 Passport,未使用中间件,passport.initialize()