From 3000c7680ebfbf966d136ec63cee67cd49fecc0a Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Wed, 5 May 2021 16:46:54 +0200 Subject: [PATCH] add a new route --- app.js | 2 + routes/public.js | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 routes/public.js diff --git a/app.js b/app.js index 5680690e..cd5b45e1 100644 --- a/app.js +++ b/app.js @@ -18,6 +18,7 @@ i18n.configure({ locales:['de', 'en'], directory: './locales' }); +const lang = 'DE' var env = process.env.NODE_ENV || 'testing'; const config = require('./config/config')[env]; @@ -69,6 +70,7 @@ app.use(function(req, res, next) { }); require('./routes/routes-account')(app, config, passport, i18n); +require('./routes/public')(app, config, lang); // Handle 404 app.use(function (req, res) { diff --git a/routes/public.js b/routes/public.js new file mode 100644 index 00000000..9ebc2255 --- /dev/null +++ b/routes/public.js @@ -0,0 +1,136 @@ +const methods = require('../functions/methods') +const async = require('async') +const mailer = require('../config/mailer') +const constants = require('../config/const') +// pwd encryption +const crypto = require('crypto') +const bcrypt = require('bcryptjs') +const saltRounds = 10 +const salt = 64 + +module.exports = function (app, config, lang) { + + // ============= NEW USERS REGISTRATION =========================== + + app.get('/registration', function(req, res) { + res.render(lang+'/account/registration') + }) + app.post('/registration', function(req, res) { + // user data + var curDate = new Date() + var userData = { + salutation: req.body.inputSalutation, + title: req.body.inputTitle, + firstname: req.body.inputFirstname, + lastname: req.body.inputLastname, + email: req.body.inputEmail, + organisation: req.body.inputOrganisation, + industry: req.body.inputIndustry, + speciality: req.body.inputSpeciality, + createdDate: curDate.toISOString().slice(0,10) + } + + var userEmail = userData.email + var pos = userEmail.indexOf('@') + var emailLength = userEmail.length + var emailDomain = userEmail.slice(pos, emailLength); + + if ( emailDomain.toLowerCase() == "@hft-stuttgart.de") { + res.flash('error', "Fehlgeschlagen: HFT-Account") + res.redirect('/account/registration') + } else { + let token + async.waterfall([ + function(done) { + crypto.randomBytes(20, function(err, buf) { + token = buf.toString('hex'); + done(err, token); + }); + }, + // encrypt password + function(token, done) { + bcrypt.genSalt(saltRounds, function(err, salt) { + bcrypt.hash(req.body.inputPassword, salt, function(err, hash) { + var newAccount = { + profile: userData, + password: hash, + verificationToken: token + } + done(err, newAccount) + }); + }); + }, + // save data + function(newAccount, err) { + methods.registerNewUser(newAccount, function(err){ + if (err) { + res.flash('error', "Fehlgeschlagen") + } + else { + // send email + var emailSubject = "Bitte bestätigen Sie Ihr M4_LAB Benutzerkonto" + var emailContent = '<div>Lieber Nutzer,<br/><br/>' + + '<p>vielen Dank für Ihre Anmeldung am Transferportal der HFT Stuttgart. <br/>' + + 'Um Ihre Anmeldung zu bestätigen, klicken Sie bitte <a href='+config.app.host+'/verifyAccount?token='+token+'>diesen Link</a> ' + + '<br/><br/>' + + 'Ohne Bestätigung Ihres Kontos müssen wir Ihr Konto leider nach 7 Tagen löschen.</p><br/>' + constants.mailSignature + + '</div>'; + mailer.options.to = req.body.inputEmail; + mailer.options.subject = emailSubject; + mailer.options.html = emailContent; + mailer.transport.sendMail(mailer.options, function(err) { + if (err) { + console.log('cannot send email') + throw err + } + }) + // user feedback + res.flash('success', 'Vielen Dank für Ihre Registrierung!'+'\r\n\r\n'+ + 'Wir haben Ihnen eine E-Mail an Ihre verwendete Adresse gesendet. Diese enthält einen Link zur Bestätigung Ihres Accounts.'+'\r\n'+ + 'Wenn Sie die Mail nicht in ihrem Postfach vorfinden, prüfen Sie bitte auch Ihren Spam-Ordner.') + } + res.redirect('/account/registration') + }) + } + ]) + } + }) + + // ============= CONTACT FORM =========================== + + app.get('/contact', function (req, res) { + res.render(lang+'/account/contact', { + user: req.user + }) + }) + app.post('/contact', function(req, res, next) { + //methods.currentDate(); + let emailAddress = req.body.inputEmail; + let supportAddress = "support-transfer@hft-stuttgart.de"; + let inputName = req.body.name; + let inputContent = req.body.message; + let emailContent = "Es wurde eine Anfrage an das Transferportal gestellt: \n\n NAME: " + inputName + "\n NACHRICHT:\n "+ inputContent; + let emailSubject = "Ihre Anfrage an das Transferportal"; + async.waterfall([ + function(done) { + // send email + mailer.options.to = supportAddress; + mailer.options.cc = emailAddress; + mailer.options.subject = emailSubject; + mailer.options.text = emailContent; + mailer.transport.sendMail(mailer.options, function(err) { + done(err, 'done'); + }); + } + ], function(err) { + if (err) { + res.flash('error', 'Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.'); + } + else { + res.flash('success', 'Vielen Dank für Ihre Anfrage. Wir melden uns baldmöglichst bei Ihnen. Eine Kopie Ihrer Anfrage wurde an ' + emailAddress + ' versandt.'); + } + res.redirect('/account/contact') + }) + }) + +} \ No newline at end of file -- GitLab