IsAuthenticated 不能作为使用带有护照本地的 nodejs 的函数
IsAuthenticated not working as a function using nodejs with passport-local
我是 node js 的初学者,我正在尝试检查用户在注册后是否通过了身份验证。
我正在使用 passport、passport-local 和 passport-local-mongoose 将用户保存在 mongoDB 数据库中。
用户就是这样注册的:
app.post("/register", function (req, res) {
const username = req.body.username;
const password = req.body.password;
User.register({username: username}, password, function (err, user) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function () {
res.redirect("/drive");
});
}
});
});
这就是我尝试检查用户是否已通过身份验证的方式:
app.get("/drive", function (req, res) {
if (req.isAuthenticated()) {
res.render("drive");
} else {
res.redirect("/login");
}
});
如果我使用 isAuthenticated() 作为函数调用,则此逻辑将不起作用,如果我使用isAuthenticated(不是函数调用),这个逻辑会起作用。
我不明白这两者之间有什么区别(在护照包上下文中)以及为什么一个有效而另一个无效。
设法解决了这个问题,这是一个非常简单的问题。
当我声明我的 session 选项时:
app.use(session({
secret: 'Our little secret.',
resave: false,
saveUninitialized: false,
cookie: {secure: true}
}))
我正在使用 cookie: {secure: true}
,并且由于我在没有 HTTPS 的本地环境中工作,所以一旦我进行身份验证,我就会取消身份验证,因为应该是generated 只能在 HTTPS 下工作。
解决方案是将 cookie 选项设置为 false 并调用 isAuthenticated() .
我是 node js 的初学者,我正在尝试检查用户在注册后是否通过了身份验证。
我正在使用 passport、passport-local 和 passport-local-mongoose 将用户保存在 mongoDB 数据库中。
用户就是这样注册的:
app.post("/register", function (req, res) {
const username = req.body.username;
const password = req.body.password;
User.register({username: username}, password, function (err, user) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function () {
res.redirect("/drive");
});
}
});
});
这就是我尝试检查用户是否已通过身份验证的方式:
app.get("/drive", function (req, res) {
if (req.isAuthenticated()) {
res.render("drive");
} else {
res.redirect("/login");
}
});
如果我使用 isAuthenticated() 作为函数调用,则此逻辑将不起作用,如果我使用isAuthenticated(不是函数调用),这个逻辑会起作用。
我不明白这两者之间有什么区别(在护照包上下文中)以及为什么一个有效而另一个无效。
设法解决了这个问题,这是一个非常简单的问题。
当我声明我的 session 选项时:
app.use(session({
secret: 'Our little secret.',
resave: false,
saveUninitialized: false,
cookie: {secure: true}
}))
我正在使用 cookie: {secure: true}
,并且由于我在没有 HTTPS 的本地环境中工作,所以一旦我进行身份验证,我就会取消身份验证,因为应该是generated 只能在 HTTPS 下工作。
解决方案是将 cookie 选项设置为 false 并调用 isAuthenticated() .