Passport本地策略登录/注册不会重定向并显示“错误请求”

时间:2019-05-29 19:16:54

标签: node.js mongodb express passport.js passport-local

我正在尝试使用Passport本地策略对用户进行身份验证。注册用户时,将在数据库中创建该用户,但该用户未正确重定向。它只是在浏览器中发送“错误请求”。 登录也会发生同样的事情-只是“错误请求”。 Cookie仍会创建,并且用户实际上已登录,只是未重定向。

大多数代码来自Udemy课程,并且在我尝试过的另一个项目中都可以完美地工作。它实际上是相同的代码。

所有npm软件包都安装在Package.json中。

这是我的app.js:

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const app = express();


app.use(express.static("public"));
app.set("view engine", "ejs");
app.set('views', [__dirname + '/views/admin', __dirname + '/views/admin/partials', __dirname + '/views/login', __dirname + '/views/reshape']);
app.use(bodyParser.urlencoded({
  extended: true
}));


app.use(session({
  secret: "secret",
  resave: false,
  saveUninitialized: false
}));

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

mongoose.connect("mongodb://localhost:27017/userDB", {
  useNewUrlParser: true
});
mongoose.set("useCreateIndex", true);

const userSchema = new mongoose.Schema({
  name: String,
  email: String,
  password: String,
  role: String,
});

userSchema.plugin(passportLocalMongoose);

const User = new mongoose.model("User", userSchema);

passport.use(User.createStrategy());

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());



app.get("/", function(req, res) {
  res.render("home");
});

app.get("/login", function(req, res) {
  res.render("pages-login");
});

app.get("/logout", function(req, res) {
  res.render("pages-logout");
});

app.get("/register", function(req, res) {
  res.render("pages-register");
});

app.get("/forgot_password", function(req, res) {
  res.render("pages-recoverpw");
});

app.get("/admin", function(req, res) {
  if (req.isAuthenticated()) {
    res.render("index");
  } else
    res.redirect("/login");
});

app.get("/product-keys", function(req, res) {
  res.render("product-keys");
});


app.post("/register", function(req, res, next) {

  User.register({
      username: req.body.registerEmail,
      name: req.body.registerName
    }, req.body.registerPassword, function(err, user) {
      if (err) {
        console.log(err);
        res.redirect("/register");
      } else {
        passport.authenticate("local")(req, res, function() {
  console.log(err);
          res.redirect("/admin");
        });
      }

    });
});

app.post("/login", function(req, res) {

  const user = new User({
    username: req.body.loginEmail,
    password: req.body.loginPassword
  });

  req.login(user, function(err) {
    if (err) {
      console.log(err);
    } else {
      passport.authenticate("local")(req, res, function() {

        res.redirect("/admin");
      });
    }
  });



});




app.listen(3000, function() {
  console.log("Server started on 3000");
});

我将不胜感激。谢谢!

0 个答案:

没有答案