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