如何解决护照认证产生的错误
Posted
技术标签:
【中文标题】如何解决护照认证产生的错误【英文标题】:How to fix error arising from passport authentication 【发布时间】:2021-01-03 20:41:25 【问题描述】:我一直在试图弄清楚为什么我在尝试登录时总是在浏览器上显示错误localhost没有发送任何数据 使用注册的用户名和密码。请问,这个问题的原因是什么?
设置护照
const passport = require("passport");
const localStrategy = require("passport-local").Strategy;
const Passport = require("passport/lib");
架构文件的内容
const mongoose = require("mongoose");
const passportLocalMongoose = require("passport-local-mongoose");
const userSchema = mongoose.Schema(
username:
type: String,
required: true
,
password: String,
);
userSchema.plugin(passportLocalMongoose);
const User = mongoose.model("User", userSchema);
module.exports = User;
护照配置
router.use(require("express-session")(
secret: "It's all about coding!",
resave: false,
saveUninitialized: false
));
router.use(passport.initialize());
router.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
注册路线
router.get("/register", async(req, res)=>
res.render("register");
)
router.post("/register", async (req, res)=>
req.body.username
req.body.password
var newUser = new User (username: req.body.username)
User.register(newUser, req.body.password, (err, user)=>
if(err)
console.log(err)
return res.render("register")
passport.authenticate("local")(req, res, ()=>
res.redirect("login");
)
)
)
登录路径:
router.get("/login", (req, res)=>
res.render("login");
)
router.post("/login", passport.authenticate("local",
successRedirect: "new",
failureRedirect: "login"
)
我应该被重定向到的页面的路由是:
router.get("/new", isLoggedIn, (req, res) =>
console.log(req.params)
res.render("new", post: new Post());
)
【问题讨论】:
【参考方案1】:router.post("/register", async (req, res)=>
User.register(username: req.body.username, req.body.password, (err, user)=>
if(err)
console.log(err)
return res.render("register")
passport.authenticate("local")(req, res, ()=>
res.redirect("/login");
)
)
)
【讨论】:
您好,欢迎来到 ***,这里有一些指导原则:不建议发布纯代码答案,答案应提供有关代码的更多解释,使其成为答案更有用,更有可能吸引投票以上是关于如何解决护照认证产生的错误的主要内容,如果未能解决你的问题,请参考以下文章