gpt4 book ai didi

javascript - 错误: passport.初始化()中间件未使用什么顺序进行调用?

转载 作者:行者123 更新时间:2023-12-03 08:44:51 25 4
gpt4 key购买 nike

我有以下内容 - 最初由 Visual Studio Node 工具生成,使用 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 的原因相同。但看不到我的代码中调用的顺序。

谁能告诉我代码各个部分的放置顺序。

谢谢

最佳答案

尝试更改这部分

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

关于javascript - 错误: passport.初始化()中间件未使用什么顺序进行调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32936193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com