gpt4 book ai didi

node.js - req.isAuthenticated为false,req.user未定义。我该如何解决?

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:05 28 4
gpt4 key购买 nike

Req.isAuthenticated()在登录后以及注册新用户后返回false。我需要做什么来修复它?

我有一个具有两个不同登录名的应用程序。一个用于“用户”,另一个用于“雇员”。 “用户”用户是我创建的第一个用户,并且在注册和登录过程中都运行良好。
但是“雇员”却没有。我已经将其代码作为“用户”的镜像版本,因为“用户”可以正常工作,但是此用户无法正常工作。

我为每个用户(用户和员工)创建了两个不同的策略,并创建了两个护照实例,但是它不起作用。

这是我的代码:

passport, models and express-session vars config
var passportUser = require("passport");
var passportEmployee = require("passport");
var LocalStrategyUser = require("passport-local").Strategy;
var LocalStrategyEmployee = require("passport-local").Strategy;
var User = require("./models/user");
var Employee = require("./models/employee");
var session = require('express-session');


护照配置

//PASSPORT CONFIGURATION
app.use(session({
secret: "Mar do macaco",
resave: false,
saveUninitialized:false,
cookie : {
secure:false,
expires: false,
}
}));


app.use(cookieParser());

//USER
app.use(passportUser.initialize());
app.use(passportUser.session());

passportUser.use('user',new LocalStrategyUser(User.authenticate()));
passportUser.serializeUser(function(user, cb) {
cb(null, user.id);
});

passportUser.deserializeUser(function(id, cb) {
User.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});



//EMPLOYEE
app.use(passportEmployee.initialize());
app.use(passportEmployee.session());

passportEmployee.use('employee',new
LocalStrategyEmployee(Employee.authenticate()));
Employee.serializeUser(function(user, cb) {
cb(null, user.id);
});

passportEmployee.deserializeUser(function(id, cb) {
User.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});


员工ROUTES进行注册和登录(无法正常工作)

//REGISTER NEW EMPLOYEE
app.get("/registernew", function(req,res){
res.render("employeeform")
})

//New Employee register logic
app.post("/registernew", async function(req,res){
var newEmployee = new Employee({
username:req.body.username,
firstname:req.body.firstname,
lastname:req.body.lastname,
birth:req.body.birth,
adress:req.body.adress,
education:req.body.education,
experience:req.body.experience,
role:req.body.role
});
Employee.register(newEmployee, req.body.password,async function(err,employee){
if(err){
console.log(err);
res.render("employeeform");
} else{

console.log("employee registered with success");
}
await passportEmployee.authenticate("employee")(req,res,function(){
console.log("employee logged in");
console.log(req.user.username);
res.redirect("/");
});
})
})

//Employee Login

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

app.post("/employeelogin", passportEmployee.authenticate("employee",{successRedirect:"/",failureRedirect:"/employeelogin",failureFlash:true,successFlash: 'Welcome!'}),function(req,res){
console.log(req.user)
})


用户注册和登录的路线(正在正常运行)

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

//handle sign up logifc
app.post("/register", function(req,res){
var newUser = new User({username:req.body.username});
if(req.body.adminCode === 'secret') {
newUser.isAdmin = true;
}
User.register(newUser, req.body.password, function(err,user){
if(err){
console.log(err);
res.render("register");
}
passportUser.authenticate("user")(req,res, function(){
res.redirect("/");
});
});
});



//SHOW LOGIN FORM
app.get("/login", function(req,res){
res.render("login");
})

//handling login logic
app.post("/login", passportUser.authenticate("user",{successRedirect:"/",failureRedirect:"/login"}),function(req,res){
})


这样,我只想解决使用员工登录名时无法通过身份验证的问题。它实际上进行登录,但是req.user是未定义的,而req.isauthenticated是false。

谢谢您的关注

最佳答案

您必须先从req.body对象获取用户数据。因为它的网址编码数据。

关于node.js - req.isAuthenticated为false,req.user未定义。我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53884279/

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