提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。策略是一种验证用户的方法。您可以使用策略允许用户根据本地保存的信息(如果您首先注册)或从Google或Github等各种提供商进行身份验证。对于这个项目,我们将制定一个本地战略。要查看的策略100的列表,请访问网站护照这里 。将passport-local添加为依赖项并将其添加到服务器,如下所示: const LocalStrategy = require('passport-local');
现在,您必须告诉护照使用实例化的LocalStartegy对象,并定义一些设置。确保这一点以及此时的所有内容都封装在数据库连接中,因为它依赖于它! passport.use(新的LocalStrategy(
function(用户名,密码,已完成){
db.collection('users')。findOne({username:username},function(err,user){
console.log('用户'+用户名+'试图登录。');
if(err){return done(err); }
if(!user){return done(null,false); }
if(password!== user.password){return done(null,false); }
return done(null,user);
});
}
));
这是我们尝试在本地验证某人时要采取的过程。首先,它尝试使用输入的用户名在我们的数据库中查找用户,然后检查要匹配的密码,最后如果没有弹出我们检查过的错误,如错误的密码,则返回用户对象,它们是认证。许多策略都是使用不同的设置设置的,一般来说,根据该策略库中的README很容易设置它。一个很好的例子是Github策略,我们不需要担心用户名或密码,因为用户将被发送到Github的auth页面进行身份验证,只要他们登录并同意,然后Github返回他们的个人资料我们用。在下一步中,我们将设置如何实际调用身份验证策略以根据表单数据验证用户!如果您认为自己已经完成了这一点,请提交您的页面。
## Instructions