app.post("/register", urlencodedParser, async(req, res, next) => {
let name = req.body.username;
let email = req.body.email;
let password = req.body.password;
const [row] = await dbConnection.query( // Здесь мы запрашиваем данные, существует ли введенный пользователем логин или нет.
"SELECT * FROM `users` WHERE `email`=?", [email]
);
if (req.body.password == 123456 || req.body.password == 098765 || req.body.password == 987654 || req.body.password == 111111 || req.body.password == 222222 || req.body.password == 333333 || req.body.password == 444444 || req.body.password == 555555 || req.body.password == 666666 || req.body.password == 777777 || req.body.password == 888888 || req.body.password == 999999 || req.body.password == 000000 || req.body.password == 123123) {
res.send("Ваш пароль слишком простой, вам нужно придумать посложнее.");
} // Checking password. If password is easy, server send error.
else {
if (row.length === 0) { // Checking users, if username already registered, server will send error
if (password.length >= 6) { // Checking length password. If less than 6 - server will send error.
res.render('verification-email')
const code = makeCode(6);
await transporter.sendMail({
from: '"Verification email" <support@galaktika-sluha.ml>',
to: `${email}`,
subject: 'Подтвердите свой E-mail',
html: `<h1>Ваш код:<h1> <h3>${code}</h3>`,
})
req.app.locals = {
name: name,
email: email,
password: password,
code: code
}
next()
} else {
res.send('Пароль должен быть минимум из 6 символов')
}
} else {
res.send("Введённая электронная почта уже зарегистрирована");
}
}
});
app.post('/email-verification', async(req, res) => {
console.log(req.app.locals.email) // debug
if (req.body.code === app.locals.code) {
bcrypt.genSalt(saltRounds, (err, salt) => {
bcrypt.hash(req.app.locals.password, salt, async(err, hash) => {
// Store hash in database here
console.log(hash) // debug
let post = {
name: `${req.app.locals.name}`,
email: `${req.app.locals.email}`,
password: `${hash}`
} // Структура данных для сервера
let sql = "INSERT INTO users SET ?" // Writing data to the DB
let query = await dbConnection.query(sql, post, err => {});
});
});
res.send(`Вы зарегистрированы!`);
} else {
res.send('вы ввели неверный код')
}
})