Nodejs护照,没有获得路由中的用户对象

时间:2015-08-30 08:04:10

标签: javascript node.js express passport.js

按照教程here

Auth.js

var passport = require("passport"),
    LocalStrategy = require("passport-local").Strategy;

passport.use(new LocalStrategy(
    function(username, password, done){
        console.log("----", username, password, "----"); //this works fine, get the proper values here.
        if(username === "admin" && password === "sw") {
            return done(null, {username: "admin"});
        }

        return done(null, false);
    }
));

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

passport.deserializeUser(function(username, done){
    done(null, {username: username});
});

module.exports = passport;

app.js(相关位)

var passport = require("./auth")
var users = require('./routes/users');
.
.
.
app.use('/users', users);
app.post('/', passport.authenticate("local", {
    failureRedirect: "/",
    successRedirect: "/users"
}));

用户路线

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
  console.log(req.user);
  res.render('users', {
    title: 'Routed !'
  });
});

module.exports = router;

大多数代码运行正常,它正确发布和路由,但是req.user在用户路由中是undefined,我无法找出原因。

0 个答案:

没有答案