类型错误:无法读取未定义的属性“authenticated”
2021-07-02
821
我目前遇到了这个问题,因为我不确定这里的问题是什么。要么是我的大脑不工作,因为我现在已经在这个项目上工作了 5 个多小时,当我尝试运行它时,它显示此错误
TypeError: Cannot read property 'authenticated' of undefined
at module.exports (my directory\api\policies\userInfo.js:6:21)
我不确定我应该在这里分享哪一部分,但这里是主要内容。
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'
});
}
},
};
1个回答
您必须使用具有未初始化会话的请求来调用此 userInfo.js。解决此问题的一种方法是处理 req.session 未定义的情况。
Derek
2021-07-02