Building Authentication with passport.js Chapter 6
const route = require('./controllers/' + file);
route.controller(app);
}
})
app.use(history());
app.use(serveStatic(__dirname + "/dist"));
router.get('/api/current_user', isLoggedIn, function(req, res) {
if(req.user) {
res.send({ current_user: req.user })
} else {
res.status(403).send({ success: false, msg: 'Unauthorized.' });
}
})
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
console.log('error! auth failed')
}
router.get('/api/logout', function(req, res){
req.logout();
res.send();
});
router.get('/', function(req, res) {
res.json({ message: 'API Initialized!'});
});
const port = process.env.API_PORT || 8081;
app.use('/', router);
var server = app.listen(port, function() {
console.log(`api running on port ${port}`);
});
module.exports = server
Here, we added the configuration for express-session with the following code block:
app.use(session({
secret: config.SECRET,
resave: true,
saveUninitialized: true,
cookie: { httpOnly: false }
}))