freeCodeCamp/curriculum/challenges/arabic/06-information-security-and.../advanced-node-and-express/create-new-middleware.arabi...

3.6 KiB

id title challengeType videoUrl localeTitle
5895f70df9fc0f352b528e6a Create New Middleware 2

Description

وللتذكير ، يجري بناء هذا المشروع على المشروع المبدئي التالي في Glitch ، أو مستنسخ من GitHub . كما هو الحال في ، يمكن لأي مستخدم الانتقال إلى / profile ما إذا كان قد تمت مصادقته أم لا عن طريق كتابة عنوان url. نريد منع ذلك من خلال التحقق مما إذا كان المستخدم قد تمت مصادقته أولاً قبل عرض صفحة الملف الشخصي. هذا هو المثال المثالي على موعد إنشاء الوسيطة. ويتمثل التحدي هنا في إنشاء وظيفة الوسيطة التي يتم ensureAuthenticated(req, res, next) ، والتي ستتحقق مما إذا كان المستخدم قد تم توثيقه عن طريق الاتصال بجوازات السفر ، تم التصديق عليه بناءً على الطلب الذي يتم تحديده للتحقق من req.user . إذا كان يجب أن يتم استدعاء التالي () ، وإلا يمكننا فقط الرد على الطلب مع إعادة توجيه إلى صفحتنا الرئيسية لتسجيل الدخول. تنفيذ هذه الوسيطة هو:
 تضمن وظيفة تم التصديق عليها (req، res، next) {
  if (req.isAuthenticated ()) {
      العودة المقبل () ؛
  }
  res.redirect ( '/')؛
}؛ 
الآن أضف makeAuthenticated as a middleware to request for the profile page before the argument to the get request contains the function that viewing the page.
 app.route ( '/ ملف')
  .get (ensureAuthenticated، (req، res) => {
       res.render (process.cwd () + '/ views / pug / profile') ؛
  })؛ 
أرسل صفحتك عندما تظن أنك على صواب.

Instructions

Tests

tests:
  - text: يجب أن يتم تنفيذ الوسيطة التي تم التأكد من صحتها وعلى طريقنا / ملفنا الشخصي
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /ensureAuthenticated[^]*req.isAuthenticated/gi, "Your ensureAuthenticated middleware should be defined and utilize the req.isAuthenticated function"); assert.match(data, /profile[^]*get[^]*ensureAuthenticated/gi, "Your ensureAuthenticated middleware should be attached to the /profile route"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: طلب الحصول على / إعادة التوصيف بشكل صحيح يعيد التوجيه إلى / بما أننا غير مصادقين
    testString: 'getUserInput => $.get(getUserInput("url")+ "/profile") .then(data => { assert.match(data, /Home page/gi, "An attempt to go to the profile at this point should redirect to the homepage since we are not logged in"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required