我知道有一个类似的问题要问这个问题,但它从来没有得到明确回答(NodeJS Express Router, pass decoded object between middleware and route?)。
我尝试在favorites.js路由中使用VerifyOrdinaryUser函数作为中间件。此函数应在请求中创建一个名为req.decoded的字段,其中包含用户信息。我需要在favorites.js中访问此用户信息,但在我尝试登录时它显示为未定义。有关异步代码的问题吗?我不知道如何解决这个问题。谢谢!
Verify.js:
//verify.js
var User = require('../models/users');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var config = require('../config.js');
exports.getToken = function (user) {
return jwt.sign(user, config.secretKey, {
expiresIn: 3600
});
};
exports.verifyOrdinaryUser = function (req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, config.secretKey, function (err, decoded) {
if (err) {
var err = new Error('You are not authenticated!');
err.status = 401;
return next(err);
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
req.params.id = req.decoded._doc_id;
next();
}
});
} else {
// if there is no token
// return an error
var err = new Error('No token provided!');
err.status = 403;
return next(err);
}
};
exports.verifyAdmin = function (req, res, next) {
if (req.decoded._doc.admin == false) {
next();
} else {
// if the user is not admin
// return an error
var err = new Error('You are not authorized to perform this operation!');
err.status = 403;
return next(err);
}
};
Favorites.js:
var express = require('express');
var router = express.Router();
var passport = require('passport');
var User = require('../models/users');
var Favorite = require('../models/favorites');
var Verify = require('./verify');
router.get('/', Verify.verifyOrdinaryUser, function(res, req, next){
console.log(req.decoded);
});
App.js :(无需查看)
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var index = require('./routes/index');
var users = require('./routes/users');
var dishes = require('./routes/dishes');
var favorites = require('./routes/favorites');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
var User = require('./models/users');
app.use(passport.initialize());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(express.static(path.join(__dirname, 'public')));
var url = 'mongodb://localhost:27017/conFusion';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
});
app.use('/', index);
app.use('/dishes', dishes);
app.use('/users', users);
app.use('/favorites', favorites);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
答案 0 :(得分:1)
是否存在异步代码问题?
没有。你在回调中调用next(),因此在异步完成时调用下一个中间件。
问题是因为你弄乱了参数的顺序。尝试用{替换function(res, req, next)
function(req, res, next)
。