'TypeError: Cannot read property 'authenticated' of undefined
I'm currently having this problem as I'm not sure whats the problem here. Either my brain doesn't work since I've worked on this project for more than 5 hours right now and when I tried to run this, it shows this error
TypeError: Cannot read property 'authenticated' of undefined
at module.exports (my directory\api\policies\userInfo.js:6:21)
I'm not sure what part should I share here but here the main thing.
userInfo.js
/*jslint node: true, unparam: true, nomen: true*/
/*global User*/
"use strict";
module.exports = function (req, res, next){
if (req.session.authenticated && req.session.userid && req.session.userid > 0) {
User.findOne({
id: req.session.userid
}, function (err, user) {
res.locals.__user = user;
next();
});
} else {
next();
}
};
authController
/*jslint node: true, unparam: true*/
/*global User, LogService*/
"use strict";
module.exports = {
login: function (req, res) {
var username = req.param('username'),
password = req.param('password');
User.findOne({
Name: username
}, function (err, user) {
if (err || !user) {
LogService.login(req.ip, 'hat versucht sich als ' + username + ' anzumelden. Der Benutzername existiert nicht.');
res.view('auth/message', {
active: 'login',
title: 'Live Your Dream - Anmelden',
error: 'Der angegebene Benutzername existiert nicht.',
});
} else {
if (user.validPassword(password)) {
req.session.authenticated = true;
req.session.userid = user.id;
LogService.login(req.ip, 'hat sich erfolgreich als ' + username + ' angemeldet.');
//res.redirect('/login/success');
res.redirect('/user/' + username);
} else {
LogService.login(req.ip, 'hat versucht sich als ' + username + ' anzumelden. Das passwort war falsch.');
req.session.loginAttempts = (req.session.loginAttempts || 0) + 1;
if (req.session.loginAttempts >= 3) {
req.session.restrictLogin = new Date().getTime() + (20 * 60 * 1000); //20 Minuten in Millisekunden
}
res.view('auth/message', {
active: 'login',
title: 'Live Your Dream - Anmelden',
error: 'Das angegebene Passwort ist falsch.',
});
}
}
});
},
successLogin: function (req, res) {
res.view('auth/message', {
active: 'login',
title: 'Live Your Dream - Anmelden',
success: 'Du wurdest erfolgreich angemeldet.'
});
},
logout: function (req, res) {
if (req.session.authenticated) {
req.session.authenticated = false;
delete req.session.authenticated;
delete req.session.userid;
res.redirect('/logout/success');
} else {
res.view('auth/message', {
active: 'login',
title: 'Live Your Dream - Abmelden',
error: 'Du bist nicht angemeldet.'
});
}
},
succesLogout: function (req, res) {
res.view('auth/message', {
active: 'login',
title: 'Live Your Dream - Anmelden',
success: 'Du wurdest erfolgreich abgemeldet.'
});
},
view: function (req, res) {
if (req.session.restrictLogin && req.session.restrictLogin > new Date().getTime()) {
res.view('auth/restricted', {
active: 'login',
title: 'Live Your Dream - Anmelden'
});
} else {
if (req.session.restrictLogin) {
delete req.session.restrictLogin;
delete req.session.loginAttempts;
}
res.view('auth/login', {
active: 'login',
title: 'Live Your Dream - Anmelden'
});
}
},
};
Solution 1:[1]
You must be calling this userInfo.js with a req that has an uninitialized session. One way to solve this is to handle the case where req.session is undefined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Derek |
