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

6.2 KiB

id title challengeType videoUrl localeTitle
58966a17f9fc0f352b528e6d Registration of New Users 2 تسجيل المستخدمين الجدد

Description

وللتذكير ، يجري بناء هذا المشروع على المشروع المبدئي التالي في Glitch ، أو مستنسخ من GitHub . الآن نحن بحاجة إلى السماح لمستخدم جديد على موقعنا بتسجيل حساب. على res.render للصفحة الرئيسية ، أضف متغيرًا جديدًا إلى الكائن الذي تم تمريره - showRegistration: true . عندما تقوم بتحديث صفحتك ، يجب أن تشاهد نموذج التسجيل الذي تم إنشاؤه بالفعل في ملف index.pug الخاص بك! تم إعداد هذا النموذج على POST على / تسجيل حتى يكون هذا هو المكان الذي يجب أن نعده لقبول POST وإنشاء كائن المستخدم في قاعدة البيانات. يجب أن يكون منطق مسار التسجيل كما يلي: تسجيل المستخدم الجديد> مصادقة المستخدم الجديد> إعادة التوجيه إلى / الملف الشخصي يجب أن يكون منطق الخطوة 1 ، تسجيل المستخدم الجديد ، كما يلي: قاعدة بيانات الاستعلام بأمر findOne> إذا كان المستخدم يتم إرجاعها ثم إعادة توجيهها إلى المنزل أو إذا كان المستخدم غير معروف ولا يحدث أي خطأ ثم "insertOne" في قاعدة البيانات باستخدام اسم المستخدم وكلمة المرور وطالما لم تحدث أي أخطاء ثم اتصل التالي للانتقال إلى الخطوة 2 ، مصادقة الجديد المستخدم ، الذي قمنا بالفعل بكتابة المنطق له في مسار POST / login الخاص بنا.
 app.route ( '/ تسجيل')
  .post ((req، res، next) => {
      db.collection ('users'). findOne ({اسم المستخدم: req.body.username} ، الوظيفة (err، user) {
          if (err) {
              بجانب (يخطئ)؛
          } آخر في حالة (المستخدم) {
              res.redirect ( '/')؛
          } آخر {
              db.collection ( 'المستخدمين). insertOne (
                {اسم المستخدم: req.body.username ،
                 كلمة المرور: req.body.password} ،
                (يخطئ ، مستند) => {
                    if (err) {
                        res.redirect ( '/')؛
                    } آخر {
                        التالي (فارغ ، مستخدم) ؛
                    }
                }
              )
          }
      })}،
    passport.authenticate ('local'، {failureRedirect: '/'})،
    (req، res، next) => {
        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