req.isAuthenticated 为 false 且 req.user 未定义。我该如何解决这个问题?
Posted
技术标签:
【中文标题】req.isAuthenticated 为 false 且 req.user 未定义。我该如何解决这个问题?【英文标题】:req.isAuthenticated is false and req.user is undefined. How can I solve this? 【发布时间】:2019-05-21 21:30:31 【问题描述】: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。
感谢您的关注
【问题讨论】:
【参考方案1】:您必须先从 req.body 对象获取用户数据,然后才能使用它。因为它的 url 编码数据。
【讨论】:
以上是关于req.isAuthenticated 为 false 且 req.user 未定义。我该如何解决这个问题?的主要内容,如果未能解决你的问题,请参考以下文章
Passport.js - req.isAuthenticated 不是一个函数
Mean.js req.isAuthenticated 显示失败?