我知道这可能是重复的,但是我在任何地方都找不到答案。无论我在哪个中间件上使用路线,isAuthenticated总是返回错误。这是我的服务器代码的一部分:
[one, two, three]
我的身份验证中间件:
samplesizes <- as.vector(c(50, 150, 50^3, 50^4, 50^5))
我的护照配置代码:
const createError = require("http-errors");
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const session = require("express-session");
const passport = require("passport");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const indexRouter = require("./routes/user");
const productRouter = require("./routes/product");
const orderRouter = require("./routes/order");
var cors = require("cors");
const app = express();
dotenv.config();
// require("./middleware/passport")(passport);
app.use(function (req, res, next) {
req.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
require("./middleware/passport")(passport)
mongoose.connect(
process.env.DB_CONNECT,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: true,
},
() => {
console.log("connected to db");
}
);
app.use(cookieParser())
app.use(
session({
secret: process.env.EXPRESS_SESSION,
resave: false,
saveUninitialized: true,
cookie: { maxAge: 20000000000, secure: false },
})
);
app.use(passport.initialize());
app.use(passport.session());
最后是我要保护的示例路由:
const jwt = require("jsonwebtoken");
const User = require("../models/User");
// middleware function to add to routes you wanna protect
// ensure authentication
// this is your middleware
module.exports = {
ensureAuthenticated: function (req, res, next) {
if (req.isAuthenticated()) {
console.log("auth");
return next();
}
console.log("error");
res.redirect("back");
}
}
我正在React中构建这个项目。最初,我只是使用节点node和EJS来完成的,并且它可以正常工作。由于某种原因,我现在无法使其正常工作。我已经做了很多修改,但仍然唯一可行的方法是删除了sureAuthenticated中间件。
对新手反应开发人员有何建议?