【发布时间】:2018-01-28 00:12:34
【问题描述】:
我正在创建一个简单的网站,用户必须在其中注册并订阅一些已激活的挑战。我正在使用护照进行注册和登录表格,并将用户电子邮件和密码保存到我的数据库中。问题是当我尝试在另一个页面中使用用户电子邮件时。 一旦用户完成登录,我的应用程序会将他重定向到个人资料页面,在那里我可以从数据库中检索数据,但是当我尝试在另一个页面中使用数据时我不能。 有人知道如何解决这个问题吗?
我的护照文件
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
}));
这是我的重定向页面
app.get('/login', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('login.ejs', { message: req.flash('loginMessage') });
});
// process the login form
// app.post('/login', do all our passport stuff here);
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/partecipant', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
我的参与者页面,在这里我可以使用此变量检索数据
<p>id: <%= user.id %></p>
<p>email:<%= user.local.email %></p>
<p>password: <%= user.local.password %></p>
这是另一个页面,我尝试使用与参与者页面中相同的变量,但它不起作用
<section id="wrapPartecipant1">
<p>email:<%= user.local.email %></p>
</section>
【问题讨论】:
标签: javascript node.js mongodb mongoose passport.js