gpt4 book ai didi

javascript - express 和 Passport 错误 : Unknown authentication strategy "login"

转载 作者:可可西里 更新时间:2023-11-01 10:19:58 28 4
gpt4 key购买 nike

我目前正在使用在线教程运行一个简单的测试 MEAN 堆栈:

  • Node.js
  • Express.js
  • Passport .js

这是应用程序文件:

app.js

var express = require("express");
var mongoose = require("mongoose");
var path = require("path");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var session = require("express-session");
var flash = require("connect-flash");
var ejs = require("ejs");

var routes = require("./routes");

var app = express();

mongoose.connect("mongodb://localhost:27017/testdb");

app.set("port", process.env.PORT || 3000);

app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: "secret",
resave: true,
saveUninitialized: true
}));

app.use(flash());

app.use(routes);

app.listen(app.get("port"), function() {
console.log("MEAN social media server started on port " + app.get("port"));
});

虽然这个app.js 文件中有 View ,但这里的重点是routes 变量所需的路由:

routes.js

var express = require("express");
var passport = require("passport");

var User = require("./models/user");

var router = express.Router();

router.use(function(req, res, next) {
res.locals.currentUser = req.user;
res.locals.errors = req.flash("error");
res.locals.infos = req.flash("info");
next();
});

router.get("/", function(req, res,next) {
User.find()
.sort({ createdAt: "descending" })
.exec(function(err, users) {
if (err) {
return next(err);
}
res.render("index", { users: users });
});
});

router.get("/signup", function(req, res) {
res.render("signup");
});

router.post("/signup", function(req, res, next) {
var username = req.body.username;
var password = req.body.password;

User.findOne({ username: username }, function(err, user) {
if (err) {
return next(err);
}
if (user) {
req.flash("error", "User already exists");
return res.redirect("/signup");
}

var newUser = new User({
username: username,
password: password
});
newUser.save(next);
});
}, passport.authenticate("login", {
successRedirect: "/",
failureRedirect: "/signup",
failureFlash: true
}));

router.get("/users/:username", function(req, res, next) {
User.findOne({ username: req.params.username }, function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return next(404);
}
res.render("profile", {
user: user
});
});
});

module.exports = router;

这是 views/signup.ejs 文件:

views/signup.ejs

<% include _header %>

<h1>Sign up</h1>

<form action="/signup" method="post">
<input name="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name="password" type="password" class="form-control" placeholder="Password" required>
<input type="submit" value="Sign up" class="btn btn-primary btn-block">
</form>

<% include _footer %>

当点击views/signup.ejs 文件的提交按钮时,'User' 记录确实被提交到 MongoDB 数据库,但是插入后在“/注册”发布方法:

localhost:3000/signup (POST)

Error: Unknown authentication strategy "login"
at attempt (/<APP_PATH>/node_modules/passport/lib/middleware/authenticate.js:166:37)
at authenticate (/<APP_PATH>/node_modules/passport/lib/middleware/authenticate.js:342:7)
at Layer.handle [as handle_request] (/<APP_PATH>/node_modules/express/lib/router/layer.js:95:5)
at next (/<APP_PATH>/node_modules/express/lib/router/route.js:131:13)
at model.<anonymous> (/<APP_PATH>/node_modules/mongoose/lib/document.js:1731:20)
at next_ (/<APP_PATH>/node_modules/hooks-fixed/hooks.js:89:34)
at fnWrapper (/<APP_PATH>/node_modules/hooks-fixed/hooks.js:186:18)
at /<APP_PATH>/node_modules/mongoose/lib/model.js:295:13
at /<APP_PATH>/node_modules/mongoose/lib/model.js:230:5
at /<APP_PATH>/node_modules/mongoose/lib/model.js:139:7
at /<APP_PATH>/node_modules/mongoose/node_modules/mongodb/lib/collection.js:479:5
at /<APP_PATH>/node_modules/mongoose/node_modules/mongodb/lib/collection.js:633:5
at /<APP_PATH>/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:469:9
at resultHandler (/<APP_PATH>/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:416:5)
at /<APP_PATH>/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:778:13
at Callbacks.emit (/<APP_PATH>/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:95:3)

我确实注意到 routes.js 中的 passport.authenticate 函数没有引用 "login" 事件文件。

有人收到类似的“错误:未知身份验证策略“登录””错误吗?

最佳答案

我会选择删除这个问题,但因为我真诚地熬夜解决这个问题,我至少应该发布错误是什么。

在构建此应用程序时,创建了以下 View :

  • index.ejs
  • signup.ejs

如果您查看上面的 routes.js 文件,当有人发布注册表单时 (router.post("/signup"..), passport.authenticate 将发送用户自动注册“登录” View 并将用户发送到根端点。

调用的“登录”状态不存在导致错误,因此出现以下消息:

“错误:未知身份验证策略“登录””错误

谢谢大家的帮助。

关于javascript - express 和 Passport 错误 : Unknown authentication strategy "login",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147267/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com