assport.authenticate文档指定以下post请求,该请求可用于使用本地策略启动身份验证过程。
app.post('/login', passport.authenticate('local'), function(req, res) {
res.send(res.userName)
})
我希望能够直接从服务器启动这个相同的请求,而不是由客户端启动。 如何从服务器直接拨打此电话?
谢谢。 j
我相信这就是你想要的。 从分离passport回调函数开始
// login.js
const login = (username, password, done = undefined) => {
const user = getUser(username); // implement this function
if (done !== undefined) { // called from passport middleware
if (verifyPassword(password, user.password)) { // implement this function
return done(null, user);
} else {
return done(null, false);
}
} else { // called manually without `done` callback function
if (verifyPassword(password, user.password)) { // implement this function
return user;
} else {
return null;
}
}
}
export default login;
// You can also put this on your other file and import the login function
passport.use(new LocalStrategy(login));
然后在您的另一个文件上,您可以导入login函数
const login = require("path/to/login.js");
const user = login("username", "password"); // make sure to not put the 3rd parameter
但是既然您是在服务器端运行这个函数,我看不出为什么要放password,所以我认为使用getuser
函数而不是login
会更好。