I'm trying to check if a user is logged in to display specifc content. I have this:
app.get('/profile', isLoggedIn, (req, res) => {
res.render('profile', {
user: req.user, isLoggedIn: isLoggedIn()
});
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next(null, true);
}
res.redirect('/');
}
And as template:
<%if (isLoggedIn) { %>
<div>Content 1</div>
<% } %>
But I get this error:
Cannot read property 'isAuthenticated' of undefined
What am I doing wrong?
isAuthenticated
of undefined meaning req is undefined
.
here you are doing mistake.
user: req.user, isLoggedIn: isLoggedIn()
You can not call middleware function like this. isLoggedIn()
What you can do is:
app.get('/profile', isLoggedIn, (req, res) => {
res.render('profile', {
user: req.user, isLoggedIn: req.isLogged
});
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
req.isLogged = true
return next();
}
res.redirect('/');
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments