错误:未使用Passport.initialize()中间件|在数据库中创建用户,但出现此错误

时间:2018-08-05 16:29:37

标签: node.js express passport.js passport-local

我正在尝试向用户注册我的应用程序,这样做会给我Error: passport.initialize() middleware not in use控制台错误,但我确实已初始化了护照。但是,它确实在数据库中正确创建了用户。看来最初的护照设置是错误的。

我已经包括了我认为是应用程序的重要部分,以解释正在发生的事情。我在堆栈跟踪指向的那一行中添加了注释。

App.js

app.use(session({
    secret: config.sessionSecret,
    // create new redis store.
    store: new RedisStore({ host: 'localhost', port: 6379, client: client, auto_reconnect: true}),
    saveUninitialized: false,
    resave: false,
    cookie: {
        maxAge: 60 * 1000 * 60 * 24 * 30 // 30 days
    },
}));

// PASSPORT CONFIGURATION
require('./controllers/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());

// Routes
require('./routes/index.js')(app, passport);

controllers / passport.js

passport.use('signup', new LocalStrategy({
      usernameField: 'email',
      passReqToCallback : true
    },
    function(req, email, password, done) {
        User.findOne({ email: req.body.email }, function(err, existingUser) {
          if(err){
            console.log(err);
          }
          if (existingUser) {
            req.flash('form', {
              email: req.body.email
            });
            return done(null, false, req.flash('error', 'An account with that email address already exists.'));
          }

          var user = new User({
            email: req.body.email,
            password: req.body.password 
          });

          user.save(function(err) {
            if (err) return done(err, false, req.flash('error', 'Error saving user.'));

            var token = new Token({ _userId: user._id, token: crypto.randomBytes(16).toString('hex') });
            token.save(function (err) {
            if (err) return done(null, false, req.flash('error', err.message));
            var email = req.body.email;
            // Send the email for the token
            var message = 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + req.headers.host + '\/confirmation\/' + token.token + '\/' + email + '\n';
            sendEmail('"APPNme" noreply@example.com', user.email, 'Account Verification Token', message);
            });
            var time = 14 * 24 * 3600000;
            req.session.cookie.maxAge = time; //2 weeks
            req.session.cookie.expires = new Date(Date.now() + time);
            req.session.touch();
            return done(null, user, req.flash('success', 'A verification email has been sent to ' + user.email + '.')); // stack trace points to this line as the error line. 
          });
        });

    })
  );

routes / index.js

app.post('/signup', passport.authenticate('signup', {
    successRedirect : '/profile', section
    failureRedirect : '/signup',
    failureFlash : true 
}));

1 个答案:

答案 0 :(得分:1)

您需要添加passport.initialize中间件。
请参阅:https://scotch.io/tutorials/easy-node-authentication-setup-and-local

// required for passport
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions

需要维护订单:

文档

  • bodyParser
  • 快速会议
  • 需要通行证.js
  • passport.initialize
  • passport.session
  • 要求路线