2 passport.authenticate 方法之间的区别
Difference between the 2 passport.authenticate methods
同一个登录路径。
只有两种不同的方法。
第一条路线
router.post('/login', passport.authenticate('local',{session:false}),async (req,res) => {
console.log("\n\n\n ------------------------222222")
console.log(req.user);
});
并且请求对象有用户 & 显示。
而在第二条路线中
router.post('/login', (req, res, next) => {
passport.authenticate('local', async (err, user, info) =>{
if(err){
console.log(err);
}
console.log("\n\n\n ------------------------")
console.log(req.user); //undefined
if(user){
// it works here
}
else{
res.status(422).json(info);
}
})(req, res, next);
});
console.log(req.user);
显示未定义。
但是用户从 mongo 数据库中获取了用户详细信息。
谁能解释一下。
在第二个函数中,您在 user
键中获取用户。但是,您必须将其添加到 req
对象。可以这样做
if(user){
// it works here
req.user = user;
}
在第一种情况下,它已经添加到 req
对象中,因为它在进入下一个异步函数之前已经执行了 passport.authenticate
。
如果您在 if else 之后打印 req.user
,它将为您提供确切的详细信息。
同一个登录路径。 只有两种不同的方法。
第一条路线
router.post('/login', passport.authenticate('local',{session:false}),async (req,res) => {
console.log("\n\n\n ------------------------222222")
console.log(req.user);
});
并且请求对象有用户 & 显示。
而在第二条路线中
router.post('/login', (req, res, next) => {
passport.authenticate('local', async (err, user, info) =>{
if(err){
console.log(err);
}
console.log("\n\n\n ------------------------")
console.log(req.user); //undefined
if(user){
// it works here
}
else{
res.status(422).json(info);
}
})(req, res, next);
});
console.log(req.user);
显示未定义。
但是用户从 mongo 数据库中获取了用户详细信息。
谁能解释一下。
在第二个函数中,您在 user
键中获取用户。但是,您必须将其添加到 req
对象。可以这样做
if(user){
// it works here
req.user = user;
}
在第一种情况下,它已经添加到 req
对象中,因为它在进入下一个异步函数之前已经执行了 passport.authenticate
。
如果您在 if else 之后打印 req.user
,它将为您提供确切的详细信息。