Como lembrete, este projeto está sendo construído sobre o seguinte projeto inicial no Glitch , ou clonado a partir do GitHub . Agora precisamos permitir que um novo usuário em nosso site registre uma conta. No res.render da página inicial, adicione uma nova variável ao objeto transmitido ao showRegistration: true
. Quando você atualiza sua página, você deve ver o formulário de registro que já foi criado no seu arquivo index.pug! Este formulário é configurado para POST on / register, então é nesse local que devemos configurar para aceitar o POST e criar o objeto de usuário no banco de dados. A lógica da rota de registro deve ser a seguinte: Registrar o novo usuário> Autenticar o novo usuário> Redirecionar para / profile A lógica da etapa 1, registrando o novo usuário, deve ser a seguinte: Consultar banco de dados com um comando findOne> se usuário é retornado então ele existe e redirecionar de volta para casa ou se o usuário é indefinido e não ocorre nenhum erro, em seguida, 'insertOne' no banco de dados com o nome de usuário e senha e, enquanto não ocorrer erros, em seguida, chamar próximo a ir para o passo 2, autenticando a nova usuário, para o qual já escrevemos a lógica em nossa rota de POST / login. app.route ('/ register')
.post ((req, res, next) => {
db.collection ('users'). findOne ({username: req.body.username}, função (err, user) {
if (err) {
próximo (err);
} else if (usuário) {
res.redirect ('/');
} outro {
db.collection ('users'). insertOne (
{username: req.body.username,
password: req.body.password},
(err, doc) => {
if (err) {
res.redirect ('/');
} outro {
next (null, usuário);
}
}
)
}
})},
passport.authenticate ('local', {failureRedirect: '/'}),
(req, res, next) => {
res.redirect ('/ profile');
}
);
Envie sua página quando achar que está certo. Se você estiver com erros, confira o projeto concluído até este ponto aqui .
## Instructions
undefined
## Tests