freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/how-to-use-passport-strateg...

3.4 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
5895f70df9fc0f352b528e69 How to Use Passport Strategies 2 如何使用Passport策略

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。在提供的index.pug文件中实际上有一个登录表单。它之前已被隐藏因为内联javascript if showLogin的形式缩进后。在showLogin作为变量从未定义之前它从未呈现包含该表单的代码块。继续在该页面的res.render上向对象showLogin: true添加一个新变量。刷新页面时,您应该看到表单!此表单设置为POST on / login因此我们应该设置此接受POST并验证用户身份。对于此挑战您应添加路由/登录以接受POST请求。要在此路由上进行身份验证您需要添加中间件才能发送响应。这是通过在您的function(req,res)之前使用中间件传递另一个参数来完成的!要使用的中间件是passport.authenticate('local')passport.authenticate也可以将一些选项作为参数,例如: { failureRedirect: '/' }这非常有用,所以一定要添加它。作为使用中间件后的响应(只有在身份验证中间件通过时才会调用)应该是将用户重定向到/ profile并且该路由应该呈现视图'profile.pug'。如果身份验证成功,则用户对象将保存在req.user中 。现在,如果您在表单中输入用户名和密码,它应该重定向到主页/并且在服务器的控制台中应该是'用户{USERNAME}尝试登录'。因为我们目前无法登录未注册的用户。当您认为自己已经做对时,请提交您的页面。如果您遇到错误,可以在这里查看到目前为止完成的项目。

Instructions

Tests

tests:
  - text: 所有步骤都在server.js中正确实现
    testString: ' getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /showLogin:( |)true/gi, "You should be passing the variable "showLogin" as true to your render function for the homepage"); assert.match(data, /failureRedirect:( |)("|")\/("|")/gi, "Your code should include a failureRedirect to the "/" route"); assert.match(data, /login[^]*post[^]*local/gi, "You should have a route for login which accepts a POST and passport.authenticates local"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 对/ login的POST请求正确重定向到/
    testString: 'getUserInput => $.post(getUserInput("url")+ "/login") .then(data => { assert.match(data, /Looks like this page is being rendered from Pug into HTML!/gi, "A login attempt at this point should redirect to the homepage since we do not have any registered users"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required