护照当地策略不起作用

时间:2018-07-17 14:12:32

标签: node.js passport.js

我正在尝试整合护照本地化策略,并且似乎对登录部分感到震惊。我正在使用express并且有多个端点(为了使代码简洁而删除了大多数端点)。我已经分别测试了护照专用代码,但没有太大问题。当我在邮递员中打开'/ login'端点时,收到一条错误消息,指出“ 无法获取/ login ”。我对此感到震惊。邮递员已设置为发送POST请求。

var express = require("express");
var cors = require("cors");
var fs = require("fs");
var path = require("path");

var bodyParser = require("body-parser");

var cluster = require("cluster");
var os = require("os");

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

var mongoose = require("mongoose");

var database = require("../config/database");

const session = require("express-session");
const MongoStore = require("connect-mongo")(session);

var data = require("./data.js");
var dateHelper = require("./utils/dateHelper.js");

var admin = require("./routes/admin.js");
var user = require("./routes/user.js");

var expressapp = express();

expressapp.use(bodyParser.urlencoded({ extended: true }));
expressapp.use(bodyParser.json());

var url = path.normalize(__dirname + "/../");
expressapp.use(express.static(url + data.getPublicFolderName()));

expressapp.use(cors());

mongoose.Promise = global.Promise;
mongoose
  .connect(database.url)
  .then(() => console.log("Connection with DB Successful"))
  .catch(err => console.error(err));



expressapp.use(
  session({
    secret: "AKDSHkdjshjdkasd0-asdasdnhkwe-qwejwqhje",
    saveUninitialized: false,
    resave: false,
    store: new MongoStore({
      mongooseConnection: mongoose.connection
    })
  })
);

passport.use(new LocalStrategy(
  function(username, password, cb) {
    console.log("Inside local strategy....");
    db.users.findByUsername(username, function(err, user) {
      if (err) { return cb(err); }
      if (!user) { return cb(null, false); }
      if (user.password != password) { return cb(null, false); }
      return cb(null, user);
    });
  }));

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

  passport.deserializeUser(function(id, cb) {
    db.users.findById(id, function (err, user) {
      if (err) { return cb(err); }
      cb(null, user);
    });
  });

expressapp.use(passport.initialize());
expressapp.use(passport.session());


var startExpressServer = function() {
  var cpucount = os.cpus().length;

  if (cluster.isMaster) {
    for (var i = 0; i < cpucount; i++) {
      var worker = cluster.fork();
      worker.on("message", function(msg) {
        console.log("Worker: " + msg.pid);
      });
    }

    cluster.on("exit", function(worker) {
      console.log("Worker: " + worker.process.pid + " died");
      cluster.fork();
    });
  } else {
    cluster.worker.send({ pid: process.pid });

    server = expressapp.listen(
      process.env.PORT || data.getPortNumber(),
      function() {
        var host = server.address().address;
        var port = server.address().port;
        console.log("Web server listening at http://%s:%s", host, port);
      }
    );
  }
};

startExpressServer();

expressapp.get("/", function(req, res) {
  res.send("Welcome to the app " + cluster.worker.id + "   " + Math.random());
});


expressapp.post('/login', 
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
  //res.redirect('/');
  res.send("Authenticated....");
});

下面我有package.json的快照

"body-parser": "^1.18.3",
"connect-mongo": "^2.0.1",
"cors": "2.8.4",
"express": "4.15.5",
"express-session": "^1.15.6",
"mongoose": "^5.1.7",
"morgan": "1.8.2",
"passport": "^0.4.0",
"passport-local": "^1.0.0"

1 个答案:

答案 0 :(得分:0)

代码中的

表示如果失败,将重定向到/ login。那就是为什么您输入错误的密码/用户名的原因 参见文档http://www.passportjs.org/docs/username-password/