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);
});
});
员工进行注册和登录的路线(工作不正常)
//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。
感谢您的关注
答案 0 :(得分:0)
必须先从req.body对象获取用户数据。因为它的网址编码数据。