freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/registration-of-new-users.c...

5.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
58966a17f9fc0f352b528e6d Registration of New Users 2 新用户注册

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。现在我们需要允许我们网站上的新用户注册一个帐户。在主页的res.render上向传递的对象添加一个新变量 - showRegistration: true 。刷新页面时您应该会看到已在index.pug文件中创建的注册表单此表单设置为POST on / register因此我们应该设置此接受POST并在数据库中创建用户对象。注册路由的逻辑应如下所示注册新用户>验证新用户>重定向到/配置文件步骤1的逻辑注册新用户应如下所示使用findOne命令查询数据库>如果用户返回然后它存在并重定向回到主页或者如果用户未定义且没有发生错误则使用用户名和密码将“insertOne”输入数据库只要没有错误发生然后调用next转到步骤2验证新的user我们已经在POST / login路由中编写了逻辑。
 app.route '/寄存器'
  .postreqresnext=> {
      db.collection'users'。findOne{usernamereq.body.username}functionerruser{
          if错误{
              下一个ERR;
          } else ifuser{
              res.redirect '/';
          } else {
              db.collection '用户'。insertOne
                {usernamereq.body.username
                 密码req.body.password}
                错误doc=> {
                    if错误{
                        res.redirect '/';
                    } else {
                        nextnulluser;
                    }
                }
              
          }
      }}
    passport.authenticate'local'{failureRedirect'/'}
    reqresnext=> {
        res.redirect '/简档';
    }
; 
当您认为自己已经做对时,请提交您的页面。如果您遇到错误,可以在这里查看到目前为止完成的项目。

Instructions

Tests

tests:
  - text: 注册路线并在家中显示
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /showRegistration:( |)true/gi, "You should be passing the variable "showRegistration" as true to your render function for the homepage"); assert.match(data, /register[^]*post[^]*findOne[^]*username:( |)req.body.username/gi, "You should have a route accepted a post request on register that querys the db with findone and the query being "username: req.body.username""); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 注册应该工作
    testString: 'getUserInput => $.ajax({url: getUserInput("url")+ "/register",data: {username: "freeCodeCampTester", password: "freeCodeCampTester"},crossDomain: true, type: "POST", xhrFields: { withCredentials: true }}) .then(data => { assert.match(data, /Profile/gi, "I should be able to register and it direct me to my profile. CLEAR YOUR DATABASE if this test fails (each time until its right!)"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 登录应该工作
    testString: 'getUserInput => $.ajax({url: getUserInput("url")+ "/login",data: {username: "freeCodeCampTester", password: "freeCodeCampTester"}, type: "POST", xhrFields: { withCredentials: true }}) .then(data => { assert.match(data, /Profile/gi, "Login should work if previous test was done successfully and redirect successfully to the profile. Check your work and clear your DB"); assert.match(data, /freeCodeCampTester/gi, "The profile should properly display the welcome to the user logged in"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 注销应该有效
    testString: 'getUserInput => $.ajax({url: getUserInput("url")+ "/logout", type: "GET", xhrFields: { withCredentials: true }}) .then(data => { assert.match(data, /Home/gi, "Logout should redirect to home"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 注销后配置文件不再有效
    testString: 'getUserInput => $.ajax({url: getUserInput("url")+ "/profile", type: "GET", crossDomain: true, xhrFields: { withCredentials: true }}) .then(data => { assert.match(data, /Home/gi, "Profile should redirect to home when we are logged out now again"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required