Anwärter Email an admin und Freischaltung

This commit is contained in:
Klaas 2024-05-29 21:25:27 +02:00
parent 97a119efe0
commit 9b96f960b2
1 changed files with 33 additions and 0 deletions

33
app.js
View File

@ -214,7 +214,23 @@ app.post('/register', async (req, res) => {
const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10);
await pool.query('INSERT INTO users (username, password) VALUES ($1, $2)', [username, hashedPassword]); await pool.query('INSERT INTO users (username, password) VALUES ($1, $2)', [username, hashedPassword]);
const message = 'Registrierung erfolgreich. Ein Admin wird dich in kürze freischalten'; const message = 'Registrierung erfolgreich. Ein Admin wird dich in kürze freischalten';
const mailOptions = {
to: 'admin@boergmann.it',
from: 'admin@boergmann.it',
subject: 'Neue Registrierung',
text: `${username} hat sich registriert`
};
transporter.sendMail(mailOptions, (error) => {
if (error) {
console.error('Error sending email:', error);
const message = 'Error sending Mail:' + error;
res.render('error', {session: req.session, message});
}
});
res.render('error', {session: req.session, message}); res.render('error', {session: req.session, message});
} catch (error) { } catch (error) {
console.error('Error registering user:', error); console.error('Error registering user:', error);
const message = 'Error registering user:' + error; const message = 'Error registering user:' + error;
@ -279,6 +295,23 @@ app.post('/userrights', requireAuth, requireAdmin, async (req, res) => {
try { try {
if (type === 'activate'){ if (type === 'activate'){
await pool.query('UPDATE users SET is_active = TRUE WHERE id = $1', [userId]); await pool.query('UPDATE users SET is_active = TRUE WHERE id = $1', [userId]);
const userResult = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
if (userResult.rows.length > 0) {
if (userResult.rows[0].email){
const mailOptions = {
to: userResult.rows[0].email,
from: 'admin@boergmann.it',
subject: 'Freischaltung',
text: `Hallo ${userResult.rows[0].username}, du wurdest soeben freigeschaltet.`
};
transporter.sendMail(mailOptions, (error) => {
if (error) {
console.error('Error sending email:', error);
const message = 'Error sending Mail:' + error;
res.render('error', {session: req.session, message});
}
})}}
} else if (type === 'admin') { } else if (type === 'admin') {
await pool.query('UPDATE users SET role = $1 WHERE id = $2', ['admin', userId]); await pool.query('UPDATE users SET role = $1 WHERE id = $2', ['admin', userId]);
} }