freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/authentication-strategies.c...

3.5 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
5895f70df9fc0f352b528e68 Authentication Strategies 2 认证策略

Description

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

Instructions

Tests

tests:
  - text: Passport-local是一种依赖
    testString: ' getUserInput => $.get(getUserInput("url")+ "/_api/package.json") .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, "passport-local", "Your project should list "passport-local " as a dependency"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: Passport-local正确需要和设置
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /require.*("|")passport-local("|")/gi, "You should have required passport-local"); assert.match(data, /new LocalStrategy/gi, "You should have told passport to use a new strategy"); assert.match(data, /findOne/gi, "Your new local strategy should use the findOne query to find a username based on the inputs"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required