Answer the question
In order to leave comments, you need to log in
How can I implement checking if the user is logged in Express.js?
Good afternoon, I got such a check, but I don’t like it at all, because this check must be specified in each route. Is there any way to take this out to another file and correct it. I do everything through the passport module. Maybe you can somehow display this in passport serialize?
router.get('/', async(req, res) => {
if (req.user !== undefined) {
authenticate = true
} else {
authenticate = false
}
res.render('index', {
titlePage: 'Asd',
auth: authenticate,
user: req.user
})
})
passport.serializeUser(function(user, done) {
done(null, user);
})
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next() }
res.redirect('/')
console.log('Для того, чтобы зайти в профиль авторизируйтесь')
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question