وللتذكير ، يجري بناء هذا المشروع على المشروع المبدئي التالي في 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