Commit cc3ae6c5 authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

delete loggedInUser (global variable) and add getLoggedInUserData(email)

parent 698c4aea
This commit is part of merge request !73. Comments created here will be created in the context of that merge request.
Showing with 46 additions and 40 deletions
+46 -40
...@@ -21,8 +21,6 @@ const projectRepo = require('../classes/repo') ...@@ -21,8 +21,6 @@ const projectRepo = require('../classes/repo')
module.exports = function (app, config, passport, i18n) { module.exports = function (app, config, passport, i18n) {
var loggedInUser
// =========== PASSPORT ======= // =========== PASSPORT =======
passport.serializeUser(function (user, done) { passport.serializeUser(function (user, done) {
done(null, user); done(null, user);
...@@ -112,28 +110,27 @@ module.exports = function (app, config, passport, i18n) { ...@@ -112,28 +110,27 @@ module.exports = function (app, config, passport, i18n) {
var updatePasswordMailSubject = "Ihr Passwort für das Transferportal wurde gespeichert." var updatePasswordMailSubject = "Ihr Passwort für das Transferportal wurde gespeichert."
var updatePasswordMailContent = '<div>Lieber Nutzer,<br/><br/>Ihr Passwort wurde erfolgreich geändert.<br/><br/>' + mailSignature + '</div>'; var updatePasswordMailContent = '<div>Lieber Nutzer,<br/><br/>Ihr Passwort wurde erfolgreich geändert.<br/><br/>' + mailSignature + '</div>';
app.get('/', function (req, res) { async function getLoggedInUserData(email) {
let user = await methods.getUserByEmail(email)
let loggedInUser = new portalUser(
user.id, email, user.salutation, user.title, user.firstname, user.lastname, user.industry, user.organisation, user.speciality, user.m4lab_idp, null, user.verificationStatus
)
let userGitlabId = await methods.getGitlabId(loggedInUser.id)
loggedInUser.setGitlabUserId(userGitlabId)
return loggedInUser
}
app.get('/', async function (req, res) {
if ( !req.isAuthenticated() ) { if ( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
methods.getUserByEmail(req.user.email, function(data, err){ let loggedInUser = await getLoggedInUserData(req.user.email)
if (!err) {
// Initialize user res.render(lang+'/account/home', {
if (!loggedInUser) { user: loggedInUser
loggedInUser = new portalUser( });
data.id, req.user.email, data.salutation, data.title, data.firstname, data.lastname, data.industry, data.organisation, data.speciality, data.m4lab_idp, null, data.verificationStatus
)
methods.getGitlabId(data.id, function(gitlabUserId, err){
if(!err) {
loggedInUser.setGitlabUserId(gitlabUserId)
}
})
}
res.render(lang+'/account/home', {
user: loggedInUser
});
}
})
} }
}); });
...@@ -166,10 +163,11 @@ module.exports = function (app, config, passport, i18n) { ...@@ -166,10 +163,11 @@ module.exports = function (app, config, passport, i18n) {
}); });
}); });
app.get('/profile', function (req, res) { app.get('/profile', async function (req, res) {
if(!req.isAuthenticated() && !loggedInUser) { if ( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if(loggedInUser.getVerificationStatus() != 1) { if(loggedInUser.getVerificationStatus() != 1) {
res.redirect('/account/') res.redirect('/account/')
} else { } else {
...@@ -181,9 +179,10 @@ module.exports = function (app, config, passport, i18n) { ...@@ -181,9 +179,10 @@ module.exports = function (app, config, passport, i18n) {
}) })
app.get('/services', async function(req, res){ app.get('/services', async function(req, res){
if(!req.isAuthenticated() && !loggedInUser) { if( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if(loggedInUser.getVerificationStatus() != 1) { // unverified users if(loggedInUser.getVerificationStatus() != 1) { // unverified users
res.redirect('/account/') res.redirect('/account/')
} else { } else {
...@@ -235,10 +234,11 @@ module.exports = function (app, config, passport, i18n) { ...@@ -235,10 +234,11 @@ module.exports = function (app, config, passport, i18n) {
} }
}) })
app.get('/security', function (req, res) { app.get('/security', async function (req, res) {
if (!req.isAuthenticated() && !loggedInUser) { if ( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if(loggedInUser.getVerificationStatus() == 1 && loggedInUser.getIdpStatus() == 1) { if(loggedInUser.getVerificationStatus() == 1 && loggedInUser.getIdpStatus() == 1) {
res.render(lang+'/account/security', { res.render(lang+'/account/security', {
user: loggedInUser user: loggedInUser
...@@ -249,7 +249,7 @@ module.exports = function (app, config, passport, i18n) { ...@@ -249,7 +249,7 @@ module.exports = function (app, config, passport, i18n) {
} }
}) })
app.post('/updateProfile', function (req, res) { app.post('/updateProfile', async function (req, res) {
var userData = { var userData = {
salutation: req.body.inputSalutation, salutation: req.body.inputSalutation,
title: req.body.inputTitle, title: req.body.inputTitle,
...@@ -261,9 +261,10 @@ module.exports = function (app, config, passport, i18n) { ...@@ -261,9 +261,10 @@ module.exports = function (app, config, passport, i18n) {
speciality: req.body.inputSpeciality, speciality: req.body.inputSpeciality,
} }
if (!req.isAuthenticated() && !loggedInUser) { if ( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if (userData.email) { if (userData.email) {
dbconn.user.query('UPDATE user SET ? WHERE email = "' +userData.email+'"', userData, function (err, rows, fields) { dbconn.user.query('UPDATE user SET ? WHERE email = "' +userData.email+'"', userData, function (err, rows, fields) {
if (err) { if (err) {
...@@ -280,10 +281,12 @@ module.exports = function (app, config, passport, i18n) { ...@@ -280,10 +281,12 @@ module.exports = function (app, config, passport, i18n) {
} }
}); });
app.post('/changePwd', function (req, res) { app.post('/changePwd', async function (req, res) {
if(!req.isAuthenticated() && !loggedInUser) { if( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
var currPwd = req.body.inputCurrPwd var currPwd = req.body.inputCurrPwd
var newPwd = req.body.inputNewPwd var newPwd = req.body.inputNewPwd
var retypePwd = req.body.inputConfirm var retypePwd = req.body.inputConfirm
...@@ -461,9 +464,10 @@ module.exports = function (app, config, passport, i18n) { ...@@ -461,9 +464,10 @@ module.exports = function (app, config, passport, i18n) {
// ============= NEW GITLAB PAGES =========================== // ============= NEW GITLAB PAGES ===========================
app.get('/newInformation', async function(req, res){ app.get('/newInformation', async function(req, res){
if (!req.isAuthenticated() && !loggedInUser) { if ( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail())
if (!gitlabUser) { // no user found if (!gitlabUser) { // no user found
res.redirect('/account/service') res.redirect('/account/service')
...@@ -475,10 +479,12 @@ module.exports = function (app, config, passport, i18n) { ...@@ -475,10 +479,12 @@ module.exports = function (app, config, passport, i18n) {
} }
} }
}) })
app.post('/newInformation', function(req, res) { app.post('/newInformation', async function(req, res) {
if(!req.isAuthenticated() && !loggedInUser) { if( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if (!req.body.name && !req.body.description) { if (!req.body.name && !req.body.description) {
res.flash('error', 'Bitte geben Sie die benötigten Daten ein') res.flash('error', 'Bitte geben Sie die benötigten Daten ein')
res.redirect('/account/newInformation') res.redirect('/account/newInformation')
...@@ -531,9 +537,11 @@ module.exports = function (app, config, passport, i18n) { ...@@ -531,9 +537,11 @@ module.exports = function (app, config, passport, i18n) {
}) })
app.get('/updateInformation', async function(req, res){ app.get('/updateInformation', async function(req, res){
if(!req.isAuthenticated() && !loggedInUser) { if( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if(!req.query.id) { if(!req.query.id) {
res.redirect('/account/services') res.redirect('/account/services')
} else { } else {
...@@ -557,10 +565,12 @@ module.exports = function (app, config, passport, i18n) { ...@@ -557,10 +565,12 @@ module.exports = function (app, config, passport, i18n) {
} }
}) })
app.post('/updateInformation', function(req, res){ app.post('/updateInformation', async function(req, res){
if(!req.isAuthenticated() && !loggedInUser) { if( !req.isAuthenticated() ) {
res.redirect('/login') res.redirect('/login')
} else { } else {
let loggedInUser = await getLoggedInUserData(req.user.email)
if (!req.body.name && !req.body.description) { if (!req.body.name && !req.body.description) {
res.flash('error', 'Bitte geben Sie die benötigten Daten ein') res.flash('error', 'Bitte geben Sie die benötigten Daten ein')
res.redirect('/account/updateInformation') res.redirect('/account/updateInformation')
...@@ -705,7 +715,6 @@ module.exports = function (app, config, passport, i18n) { ...@@ -705,7 +715,6 @@ module.exports = function (app, config, passport, i18n) {
// ============= USER VERIFICATION ================================ // ============= USER VERIFICATION ================================
app.get("/verifyAccount", function(req, res){ app.get("/verifyAccount", function(req, res){
console.log(req.query)
methods.getUserIdByVerificationToken(req.query.token, function(userId, err){ methods.getUserIdByVerificationToken(req.query.token, function(userId, err){
if (userId) { if (userId) {
let userData = { let userData = {
...@@ -743,9 +752,6 @@ module.exports = function (app, config, passport, i18n) { ...@@ -743,9 +752,6 @@ module.exports = function (app, config, passport, i18n) {
} }
}) })
if(!loggedInUser) {
loggedInUser.setVerificationStatus(userData.verificationStatus)
}
res.render(lang+'/account/verification', { res.render(lang+'/account/verification', {
status: true status: true
}); });
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment