【问题标题】:Passport: Error: passport.initialize() middleware not in use;Passport:错误:passport.initialize() 中间件未使用;
【发布时间】:2018-08-25 02:57:59
【问题描述】:

我有一个带有 MongoDB 和 Mongoose 的快速服务器,并使用护照通过 JWT 进行身份验证,但出现标题中的错误。

我正在关注 passport-jwt 文档,但仍然收到错误消息。我做错了什么?

以下是使用有效 JWT 在 localhost3090 上进行 GET 调用时的错误消息:

::1 - - [16/Mar/2018:05:35:47 +0000] "GET / HTTP/1.1" 500 1677 "-" "PostmanRuntime/7.1.1"
Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn (/Users/okadachikara/react-courses/projects/server/node_modules/passport/lib/http/request.js:46:34)
    at JwtStrategy.strategy.success (/Users/okadachikara/react-courses/projects/server/node_modules/passport/lib/middleware/authenticate.js:248:13)
    at verified (/Users/okadachikara/react-courses/projects/server/node_modules/passport-jwt/lib/strategy.js:115:41)
    at /Users/okadachikara/react-courses/projects/server/services/passport.js:34:7
    at /Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/model.js:3930:16
    at _init (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:2007:5)
    at model.Document.init (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/document.js:393:5)
    at completeOne (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:1993:12)
    at Immediate.<anonymous> (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:1520:11)
    at Immediate._onImmediate (/Users/okadachikara/react-courses/projects/server/node_modules/mquery/lib/utils.js:119:16)
    at runCallback (timers.js:773:18)
    at tryOnImmediate (timers.js:734:5)
    at processImmediate [as _immediateCallback] (timers.js:711:5)

我的服务器/控制器/authentication.js:

const User = require('../models/user');
const jwt = require('jwt-simple');

const config = require('../config');

function tokenForUser(user) {
  const timestamp = new Date().getTime();
  return jwt.encode({ sub: user.id, iat: timestamp }, config.secret);
 }

exports.signup = function (req, res, next) {
  const email = req.body.email;
  const password = req.body.password;

  if (!email || !password) {
    return res.status(422).send({ error: 'You must provide an email and 
  password' });
  }

  // see if user with the given email exists
User.findOne({ email: email }, function (err, existingUser) {
  if (err) { return next(err); }
  if (existingUser) {
    return res.status(422).send({ error: 'A user with that email 
 already exists' });
  }
  const user = new User({
    email: email,
    password: password
});

  user.save(function (err) {
    if (err) { return next(err); }
    res.json({ token: tokenForUser(user), iat: jwt.iat });
  });
});
};

我的服务器/services/passport.js

const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config');

const jwtOptions = {
  jwtFromRequest: ExtractJwt.fromHeader('authorization'),
  secretOrKey: config.secret
};

const jwtLogin = new JwtStrategy(jwtOptions, function (payload, done) {
  User.findById(payload.sub, function (err, user) {
    if (err) { return done(err, false); }
    if (user) {
      done(null, user);
    } else {
      done(null, false);
    }
  });
});

passport.use(jwtLogin);

我的服务器/router.js

const passport = require('passport');
const Authentication = require('./controllers/authentication');
const passportService = require('./services/passport');

const requireAuth = passport.authenticate('jwt', { sesssion: false });

module.exports = function (app) {
  app.get('/', requireAuth, function (req, res) {
    res.send({ hi: 'there' });
  });

  app.post('/signup', Authentication.signup);
};

【问题讨论】:

  • 这在插入以下内容后起作用:在 index.js 中:app.use(passport.initialize());在 const app = express(); 之后然后在passport.js中:passport.serializeUser(function(user, done) { done(null, user); }); passport.deserializeUser(function(user, done) { done(null, user); }); // 不使用

标签: mongodb express mongoose passport.js jwt-simple


【解决方案1】:

使用前需要初始化护照模块:

let app = express();
app.use(passport.initialize());

【讨论】:

  • 谢谢。不知何故,我所遵循的示例没有。也许这是去年实施的一个新步骤。无论如何,它现在可以工作了。
猜你喜欢
  • 2018-07-04
  • 2019-12-26
  • 2019-01-26
  • 2021-08-25
  • 2013-05-22
  • 2016-01-01
  • 1970-01-01
  • 2019-10-28
  • 2016-07-29
相关资源
最近更新 更多