按照教程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
,我无法找出原因。