Como recordatorio, este proyecto se está construyendo sobre el siguiente proyecto de inicio en Glitch , o clonado desde GitHub . Ahora debemos permitir que un nuevo usuario en nuestro sitio registre una cuenta. En el res.render para la página de inicio, agregue una nueva variable al objeto pasado a lo showRegistration: true
. Cuando actualice su página, debería ver el formulario de registro que ya se creó en su archivo index.pug. Este formulario está configurado para POST on / register, de modo que aquí es donde debemos configurar para aceptar el POST y crear el objeto de usuario en la base de datos. La lógica de la ruta de registro debe ser la siguiente: Registrar el nuevo usuario> Autenticar el nuevo usuario> Redirigir a / perfil La lógica del paso 1, registrar al nuevo usuario, debe ser la siguiente: Consultar la base de datos con un comando findOne> if user se devuelve, entonces existe y se redirige a casa O si el usuario no está definido y no se produce ningún error, 'insertOne' en la base de datos con el nombre de usuario y la contraseña, y mientras no se produzcan errores, llame al siguiente paso para autenticar el nuevo usuario, para lo cual ya hemos escrito la lógica en nuestra ruta POST / login. app.route ('/ register')
.post ((req, res, next) => {
db.collection ('usuarios'). findOne ({username: req.body.username}, función (err, usuario) {
if (err) {
siguiente (err);
} else if (usuario) {
res.redirect ('/');
} else {
db.collection ('usuarios'). insertOne (
{username: req.body.username,
contraseña: req.body.password},
(err, doc) => {
if (err) {
res.redirect ('/');
} else {
siguiente (nulo, usuario);
}
}
)
}
})},
passport.authenticate ('local', {failureRedirect: '/'}),
(req, res, next) => {
res.redirect ('/ profile');
}
);
Envía tu página cuando creas que lo has hecho bien. Si está teniendo errores, puede consultar el proyecto completado hasta este punto aquí .
## Instructions