passport.authenticate回调丢失凭据

时间:2018-02-02 13:49:18

标签: javascript node.js mongodb express passport.js

有谁知道为什么这段代码会返回“Missing Credentials”,当我尝试使用电子邮件和密码注册时,电子邮件和密码未定义,我认为问题不在于bodyParser,因为如果我不使用passport.authenticate in邮寄路线邮件和密码都没问题。我正在使用快递4.15 这是我的代码: 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 expressHbs = require('express-handlebars');
var mongoose = require('mongoose');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');

var routes = require('./routes/index');

var app = express();

mongoose.connect('mongodb://localhost:27017/gibuy');
require('./config/passport');

// view engine setup
app.engine('.hbs',expressHbs({defaultLayout: 'layout', extname: '.hbs'} ));
app.set('view engine', '.hbs');

// 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());
app.use(session({secret: 'mysupersecret', resave: false, saveUninitialized: false}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

// 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;

passsport.js

var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;

console.log("passport.js");

passport.serializeUser(function(user, done) {

   done(null, user.id);
});

passport.deserializeUser(function(id, done) {
   User.findById(id, function(err, user) {
      done(err, user);
   });
});

passport.use('local-signup', new LocalStrategy({
   usernameField: 'email',
   passwordField: 'passowrd',
   passReqToCallback: true
}, function(req, email, passowrd, done) {
   console.log("passport email " ,emai);
   console.log("passport password", password);
   User.findOne({'email': email}, function(err, user) {
      if(err) {
         return done(err);
      }
      if(user) {
         return done(null, false, {message: 'Email is already in use'})
      }
      var newUser = new User();
      newUser.email = email;
      newUser.passowrd = newUser.encryptPassword(password);
       console.log("object", newUser);
      newUser.save(function(err, result) {
         if(err) {
            return done(err);
         }
         return done(null, newUser);
      });
   });
}));

index.js

var express = require('express');
var router = express.Router();
var csrf = require('csurf');
var passport = require('passport');

var Product = require('../models/product');

var csrfProtection = csrf();
router.use(csrfProtection);

/* GET home page. */
router.get('/', function(req, res, next) {
  Product.find(function(err, docs) {
    var productChunks = [];
    var chunkSize = 3;
    for(let i  = 0; i<docs.length; i += chunkSize) {
      productChunks.push(docs.slice(i,i + chunkSize));
    }
    res.render('shop/index', { title: 'Express', products: productChunks });
  });
});

router.get('/user/signup', function(req, res, next) {

  var messages = req.flash('error');
  res.render('user/signup', {csrfToken: req.csrfToken(), messages: messages, hasErrors: messages.length > 0 });
});

router.post('/user/signup', passport.authenticate('local-signup', {
  successRedirect: '/user/profile',
  failureRedirect: '/user/signup',
  failureFlash: true,
  session:true
}));

// router.post('/user/signup', function(req, res, next) {
//   console.log("post route", req.body); // logs  email and passowrd
// });

router.get('/user/profile', function(req, res, next) {
  res.render('user/profile');
});

module.exports = router;

0 个答案:

没有答案