gpt4 book ai didi

javascript - passportjs 本地策略没有被调用

转载 作者:搜寻专家 更新时间:2023-11-01 00:12:54 24 4
gpt4 key购买 nike

我正在使用 express 和 passport 构建一个 rest api 后端,我的 localStrategy 似乎没有按要求被调用。

我的应用程序的入口点如下所示:

app.js

var fs = require('fs');
var express = require('express');
var mongoose = require('mongoose');
var passport = require('passport');
var config = require('./config/config');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();

app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({extended:true}));

app.use(passport.initialize());

//connect to mongodb
mongoose.connect(config.db, options);

//load models (shorten forEach)
...
require(__dirname + '/models/' + file)(mongoose);

//load passport config
require('./config/passport')(mongoose, passport);

//load routes
require('./config/routes')(app, passport);

//start server
var server = app.listen(3000, ....

routes.js

...
app.post('/auth', function(req, res){
console.log("reached auth endpoint");
console.log(req.body);
passport.authenticate('local', { session: false}, function(err, user, info){
console.log("Test:"+user);
if(err) {
console.log("Error1");
return next(err)}
if(!user){
console.log("Error2");
return res.json(401, {error: 'Auth Error!'});
}
console.log("Error3");
var token = jwt.encode({ username: user.email }, "hanswurst");
res.json({token: token});
}),
function(req, res){
console.log("passport user", req.user);
};
});

passport.js

...
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(email, password, done){
console.log("TEST");
User.findOne({'email': email}, function(err, user){
if(err){
console.log("Unknown error");
return done(err);
}
if(!user){
console.log("No User found");
return done(null, false);
}
if(!user.validPassword(password)){
console.log("Password was incorrect");
return done(null, false);
}
console.log("User was found");
return done(null, user);
});
}
));

我在表单请求中得到的唯一结果是

reached auth endpoint
{ email: 'test@mail.com', password: 'secret' }
POST /auth - - ms - -

对我来说,请求正文看起来不错,它应该进入我的 LocalStrategy。我有点无助,因为我没有从这一点得到任何其他控制台输出。

最佳答案

首先,routes.js 中存在一些基本的 javascript 语法错误。此处的这一部分(为清楚起见删除了大量代码)已损坏:

passport.authenticate(/*...*/), function(req, res) {/*..*/};

可能是您刚刚在错误的位置添加了一些 console.log 调用。为了消除混淆,passport.authenticate() 不会立即执行身份验证,它所做的是为您返回一个中间件。例如,您可以像这样使用它:

var middleware = passport.authenticate(...);
app.post('/auth', middleware);

因此,要解决您的问题,请尝试立即调用 authenticate 返回的中间件,如下所示:

app.post('/auth', function(req, res, next) {
console.log("reached auth endpoint");
console.log(req.body);
passport.authenticate('local', {
session: false
}, function(err, user, info) {
console.log("Test:" + user);
if (err) {
console.log("Error1");
return next(err);
}
if (!user) {
console.log("Error2");
return res.json(401, {
error: 'Auth Error!'
});
}
console.log("Error3");
var token = jwt.encode({
username: user.email
}, "hanswurst");
res.json({
token: token
});
})(req, res, next);
});

另外,我必须告诉你 require 缓存模块。要让 config/passport.js 知道 mongoose 和 passport,你不应该像这样将它们作为参数提供:

require('./config/passport')(mongoose, passport);

只需在 config/passport.js 中再次要求它们,如下所示:

// (in config/passport.js)
// Both of these vars point to the same thing you require'd in app.js
var mongoose = require('mongoose');
var passport = require('passport');

关于javascript - passportjs 本地策略没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558786/

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