无法读取null + passport.js的属性'body'

时间:2016-07-23 19:57:00

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

我正在尝试在Node / Express / Sequelize应用程序中实现passport.js。我想尽可能忠实于the official documentationScotch.io tutorial,并且目前在我的脚手架中有以下相关代码段:

app.js

<link rel="import" href="components/google-map/google-map.html">

<google-map latitude="53.213613" longitude="-1.673780"></google-map>

./配置/ passport.js

const express = require('express');
const http = require('http');
const https = require('https');
const sequelize = require('sequelize');
const db = require('./config/sequelize');
const config = require('./config/config');
const passport = require('./config/passport');

const app = express();

const port = 3000;

app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'ejs');

app.listen(port);

app.post('/signup', passport.authenticate('local', function(req, res, next, err){
    if (err) { console.log(err); }
    res.json(req.body);
})); 

./视图/ signup.ejs

const db = require('./sequelize');

const passport = require('passport'), 
LocalStrategy = require('passport-local').Strategy;

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

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

passport.use('local', new LocalStrategy({
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true  
    }, 
    function(email, password, done){
        process.nextTick(function() {
            db.User.findOne({ email : 'local.email' }, function(err, user) {
                if (err)
                    return done(err);
                if (user) {
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                } 
                else {
                    db.User.create({
                        username: 'local.email',
                        password: 'local.password'
                    });
                }
            });    
         });
      }
 ));

 module.exports = passport;

有了这个实现,我启动应用程序,输入电子邮件地址和密码以注册./views/signup.ejs,并收到以下错误:

<!-- LOGIN FORM -->
<form action="/signup" method="post">
    <div class="form-group">
        <label>Email</label>
        <input type="text" name="username">
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" name="password">
    </div>

    <button type="submit">Signup</button>
</form>

这表明req.body被返回为null,我怀疑它与./config/passport.js中定义deserializeUser和serializeUser函数的方式有关,但我怎么能得到详细的错误输出,以确定究竟原因是什么?此外,我已经使用我的Sequelize数据库进行了完整的CRUD检查,因此我省略了该文件,但是如果您认为有任何人可以解决此问题,则会将其作为编辑提供。

1 个答案:

答案 0 :(得分:1)

请在app.js

中试试
app.post('/signup', passport.authenticate('local'), function(req, res, next, err){
                                               //^ close
    // If this function gets called, authentication was successful.
    res.redirect('/users/' + req.user.username);
}); 

如果您需要自定义回调:

app.post('/signup', function(req, res, next){
    passport.authenticate('local', function(err, user, info) {
       //Your code here       
    })(req, res, next);
}); 

有关详细信息,请参阅http://passportjs.org/docs/authenticate